日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

java對(duì)上傳圖片縮放裁剪

 青_春 2016-03-18
  一般上傳圖片分三步:

第一步:將原始圖片上傳到服務(wù)器端保存,不妨命名為src.jpg

第二步:在瀏覽器端將src.jpg顯示出來(lái),然后使用jQuery,對(duì)其進(jìn)行“客戶端剪切”。

              所謂客戶端剪切就是根據(jù)用戶在界面中對(duì)原始圖片放大,移動(dòng),剪切時(shí),獲得一些參數(shù)。

             具體需要六個(gè)參數(shù)(srcWidth, srcHeight,  rect.x,  rect.y  , rect.width, rect.height)參數(shù)。

             其中,srcWidth 和srcHeight表明原始圖片在客戶端放大后的寬和高

                       rect.x和rect.y表明剪切部分相對(duì)(srcWidth, srcHeight)的起始坐標(biāo)

                       rect.width和rect.height表示需要剪切的圖片的大小。

第三步:獲取第二步得到的參數(shù),對(duì)內(nèi)存中原始圖片先進(jìn)行預(yù)處理(按照srcWidth和srcHeight進(jìn)行縮放)、剪切。

             然后對(duì)預(yù)處理后的內(nèi)存圖像剪切,打印出來(lái)。



第一步實(shí)現(xiàn)比較簡(jiǎn)單,第二步需要學(xué)習(xí)jQuery,網(wǎng)上例子很多。我們重點(diǎn)討論第三步, java切圖。

1.main函數(shù)
Java代碼  收藏代碼
  1. package syj.main;  
  2.   
  3. import java.awt.Rectangle;  
  4. import java.io.File;  
  5. import java.io.IOException;  
  6.   
  7. import syj.util.ImageHepler;  
  8.   
  9. public class Test {  
  10.  public static void main(String[] args) throws IOException {  
  11.   String picPath = "img/src.jpg";  
  12.   String destPath = "img/result.jpg";  
  13.   Rectangle rect = new Rectangle(0, 0, 2000, 1200);  
  14.   ImageHepler.cut(picPath, destPath, 1440, 900, rect);  
  15.  }  
  16. }  


2.關(guān)鍵圖像操作函數(shù)
Java代碼  收藏代碼
  1. package syj.util;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Graphics;  
  5. import java.awt.Image;  
  6. import java.awt.Rectangle;  
  7. import java.awt.image.BufferedImage;  
  8. import java.io.File;  
  9. import java.io.IOException;  
  10. import java.io.PrintStream;  
  11. import javax.imageio.ImageIO;  
  12.   
  13. public class ImageHepler {   
  14.   
  15.  /** 
  16.   * @Description: 將srcImageFile裁剪后生成destImageFile 
  17.   * @param srcImageFile  原始圖 
  18.   * @param destImageFile  目標(biāo)圖 
  19.   * @param width          原始圖預(yù)處理后width 
  20.   * @param height         原始圖預(yù)處理后height 
  21.   * @param rect           目標(biāo)圖輸出的格式(rect.x, rect.y, rect.width, rect.height) 
  22.   * @throws IOException 
  23.   * @author Sun Yanjun 
  24.   */  
  25.  public static void cut(String srcImageFile, String destImageFile, int width, int height, Rectangle rect) throws IOException {  
  26.   Image image = ImageIO.read(new File(srcImageFile));  
  27.   BufferedImage bImage = makeThumbnail(image, width, height);  
  28.   
  29.   //把原始圖片輸出  
  30.   ImageIO.write(bImage, "jpg",new File("img/src2.jpg"));  
  31.     
  32.   saveSubImage(bImage, rect, new File(destImageFile));  
  33.  }  
  34.   
  35.    
  36.  /** 
  37.   * @Description: 將srcImageFile裁剪后生成destImageFile 
  38.   * @param srcImageFile  原始圖 
  39.   * @param destImageFile  目標(biāo)圖 
  40.   * @param width          原始圖預(yù)處理后width 
  41.   * @param height         原始圖預(yù)處理后height 
  42.   * @param rect           目標(biāo)圖輸出的格式(rect.x, rect.y, rect.width, rect.height) 
  43.   * @throws IOException 
  44.   * @author Sun Yanjun 
  45.   */  
  46.  public static void cut(File srcImageFile, File destImageFile, int width, int height, Rectangle rect) throws IOException {  
  47.   Image image = ImageIO.read(srcImageFile);  
  48.   BufferedImage bImage = makeThumbnail(image, width, height);  
  49.   
  50.     
  51.   saveSubImage(bImage, rect, destImageFile);  
  52.  }  
  53.    
  54.  /** 
  55.   * @Description: 對(duì)原始圖片根據(jù)(x, y, width, height) = (0, 0, width, height)進(jìn)行縮放,生成BufferImage 
  56.   * @param img 
  57.   * @param width 預(yù)處理后圖片的寬度 
  58.   * @param height 預(yù)處理后圖片高度 
  59.   * @return 
  60.   * @author Sun Yanjun 
  61.   * @throws IOException 
  62.   */  
  63.  private static BufferedImage makeThumbnail(Image img, int width, int height) throws IOException {  
  64.   BufferedImage tag = new BufferedImage(width, height, 1);  
  65.   Graphics g = tag.getGraphics();  
  66.   g.drawImage(img.getScaledInstance(width, height, 4), 0, 0, null);  
  67.     
  68.   g.dispose();  
  69.   return tag;  
  70.  }  
  71.   
  72.  /** 
  73.   * @Description: 對(duì)BufferImage按照(x, y, width, height) = (subImageBounds.x, subImageBounds.y, subImageBounds.width, subImageBounds.height)進(jìn)行裁剪 
  74.   *               如果subImageBounds范圍過(guò)大,則用空白填充周圍的區(qū)域。 
  75.   *               
  76.   * @param image 
  77.   * @param subImageBounds 
  78.   * @param destImageFile 
  79.   * @throws IOException 
  80.   * @author Sun Yanjun 
  81.   */  
  82.  private static void saveSubImage(BufferedImage image, Rectangle subImageBounds, File destImageFile) throws IOException {  
  83.   String fileName = destImageFile.getName();  
  84.   String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);  
  85.   BufferedImage subImage = new BufferedImage(subImageBounds.width, subImageBounds.height, 1);  
  86.   Graphics g = subImage.getGraphics();  
  87.     
  88.     
  89.   if ((subImageBounds.width > image.getWidth())  
  90.     || (subImageBounds.height > image.getHeight())) {  
  91.    int left = subImageBounds.x;  
  92.    int top = subImageBounds.y;  
  93.    if (image.getWidth() < subImageBounds.width)  
  94.     left = (subImageBounds.width - image.getWidth()) / 2;  
  95.    if (image.getHeight() < subImageBounds.height)  
  96.     top = (subImageBounds.height - image.getHeight()) / 2;  
  97.    g.setColor(Color.white);  
  98.    g.fillRect(0, 0, subImageBounds.width, subImageBounds.height);  
  99.    g.drawImage(image, left, top, null);  
  100.    ImageIO.write(image, formatName, destImageFile);  
  101.   } else {  
  102.    g.drawImage(image.getSubimage(subImageBounds.x, subImageBounds.y,  
  103.      subImageBounds.width, subImageBounds.height), 0, 0, null);  
  104.   }  
  105.   g.dispose();  
  106.   ImageIO.write(subImage, formatName, destImageFile);  
  107.  }  
  108. }  
  109.   
  110.  


BufferedImage subImage = new BufferedImage(subImageBounds.width, subImageBounds.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = subImage.createGraphics();
// 解決圖片背景透明問(wèn)題--且只針對(duì)png
subImage = g.getDeviceConfiguration().createCompatibleImage(subImageBounds.width, subImageBounds.height, Transparency.TRANSLUCENT);
g.dispose();
g = subImage.createGraphics();


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多