java对上传图片缩放裁剪

一般上传图片分三步:

第一步:将原始图片上传到服务器端保存,不妨命名为src.jpg

第二步:在浏览器端将src.jpg显示出来,然后使用jQuery,对其进行“客户端剪切”。

              所谓客户端剪切就是根据用户在界面中对原始图片放大,移动,剪切时,获得一些参数。

             具体需要六个参数(srcWidth, srcHeight,  rect.x,  rect.y  , rect.width, rect.height)参数。

             其中,srcWidth 和srcHeight表明原始图片在客户端放大后的宽和高

                       rect.x和rect.y表明剪切部分相对(srcWidth, srcHeight)的起始坐标

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

第三步:获取第二步得到的参数,对内存中原始图片先进行预处理(按照srcWidth和srcHeight进行缩放)、剪切。

             然后对预处理后的内存图像剪切,打印出来。



第一步实现比较简单,第二步需要学习jQuery,网上例子很多。我们重点讨论第三步, java切图。

1.main函数
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(0020001200);  
  14.   ImageHepler.cut(picPath, destPath, 1440900, rect);  
  15.  }  
  16. }  


2.关键图像操作函数
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  目标图 
  19.   * @param width          原始图预处理后width 
  20.   * @param height         原始图预处理后height 
  21.   * @param rect           目标图输出的格式(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  目标图 
  40.   * @param width          原始图预处理后width 
  41.   * @param height         原始图预处理后height 
  42.   * @param rect           目标图输出的格式(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: 对原始图片根据(x, y, width, height) = (0, 0, width, height)进行缩放,生成BufferImage 
  56.   * @param img 
  57.   * @param width 预处理后图片的宽度 
  58.   * @param height 预处理后图片高度 
  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), 00null);  
  67.     
  68.   g.dispose();  
  69.   return tag;  
  70.  }  
  71.   
  72.  /** 
  73.   * @Description: 对BufferImage按照(x, y, width, height) = (subImageBounds.x, subImageBounds.y, subImageBounds.width, subImageBounds.height)进行裁剪 
  74.   *               如果subImageBounds范围过大,则用空白填充周围的区域。 
  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(00, 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), 00null);  
  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();
// 解决图片背景透明问题--且只针对png
subImage = g.getDeviceConfiguration().createCompatibleImage(subImageBounds.width, subImageBounds.height, Transparency.TRANSLUCENT);
g.dispose();
g = subImage.createGraphics();


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值