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. packagesyj.main;
  2. importjava.awt.Rectangle;
  3. importjava.io.File;
  4. importjava.io.IOException;
  5. importsyj.util.ImageHepler;
  6. publicclassTest{
  7. publicstaticvoidmain(String[]args)throwsIOException{
  8. StringpicPath="img/src.jpg";
  9. StringdestPath="img/result.jpg";
  10. Rectanglerect=newRectangle(0,0,2000,1200);
  11. ImageHepler.cut(picPath,destPath,1440,900,rect);
  12. }
  13. }



2.关键图像操作函数

Java代码 收藏代码
  1. packagesyj.util;
  2. importjava.awt.Color;
  3. importjava.awt.Graphics;
  4. importjava.awt.Image;
  5. importjava.awt.Rectangle;
  6. importjava.awt.image.BufferedImage;
  7. importjava.io.File;
  8. importjava.io.IOException;
  9. importjava.io.PrintStream;
  10. importjavax.imageio.ImageIO;
  11. publicclassImageHepler{
  12. /**
  13. *@Description:将srcImageFile裁剪后生成destImageFile
  14. *@paramsrcImageFile原始图
  15. *@paramdestImageFile目标图
  16. *@paramwidth原始图预处理后width
  17. *@paramheight原始图预处理后height
  18. *@paramrect目标图输出的格式(rect.x,rect.y,rect.width,rect.height)
  19. *@throwsIOException
  20. *@authorSunYanjun
  21. */
  22. publicstaticvoidcut(StringsrcImageFile,StringdestImageFile,intwidth,intheight,Rectanglerect)throwsIOException{
  23. Imageimage=ImageIO.read(newFile(srcImageFile));
  24. BufferedImagebImage=makeThumbnail(image,width,height);
  25. //把原始图片输出
  26. ImageIO.write(bImage,"jpg",newFile("img/src2.jpg"));
  27. saveSubImage(bImage,rect,newFile(destImageFile));
  28. }
  29. /**
  30. *@Description:将srcImageFile裁剪后生成destImageFile
  31. *@paramsrcImageFile原始图
  32. *@paramdestImageFile目标图
  33. *@paramwidth原始图预处理后width
  34. *@paramheight原始图预处理后height
  35. *@paramrect目标图输出的格式(rect.x,rect.y,rect.width,rect.height)
  36. *@throwsIOException
  37. *@authorSunYanjun
  38. */
  39. publicstaticvoidcut(FilesrcImageFile,FiledestImageFile,intwidth,intheight,Rectanglerect)throwsIOException{
  40. Imageimage=ImageIO.read(srcImageFile);
  41. BufferedImagebImage=makeThumbnail(image,width,height);
  42. saveSubImage(bImage,rect,destImageFile);
  43. }
  44. /**
  45. *@Description:对原始图片根据(x,y,width,height)=(0,0,width,height)进行缩放,生成BufferImage
  46. *@paramimg
  47. *@paramwidth预处理后图片的宽度
  48. *@paramheight预处理后图片高度
  49. *@return
  50. *@authorSunYanjun
  51. *@throwsIOException
  52. */
  53. privatestaticBufferedImagemakeThumbnail(Imageimg,intwidth,intheight)throwsIOException{
  54. BufferedImagetag=newBufferedImage(width,height,1);
  55. Graphicsg=tag.getGraphics();
  56. g.drawImage(img.getScaledInstance(width,height,4),0,0,null);
  57. g.dispose();
  58. returntag;
  59. }
  60. /**
  61. *@Description:对BufferImage按照(x,y,width,height)=(subImageBounds.x,subImageBounds.y,subImageBounds.width,subImageBounds.height)进行裁剪
  62. *如果subImageBounds范围过大,则用空白填充周围的区域。
  63. *
  64. *@paramimage
  65. *@paramsubImageBounds
  66. *@paramdestImageFile
  67. *@throwsIOException
  68. *@authorSunYanjun
  69. */
  70. privatestaticvoidsaveSubImage(BufferedImageimage,RectanglesubImageBounds,FiledestImageFile)throwsIOException{
  71. StringfileName=destImageFile.getName();
  72. StringformatName=fileName.substring(fileName.lastIndexOf('.')+1);
  73. BufferedImagesubImage=newBufferedImage(subImageBounds.width,subImageBounds.height,1);
  74. Graphicsg=subImage.getGraphics();
  75. if((subImageBounds.width>image.getWidth())
  76. ||(subImageBounds.height>image.getHeight())){
  77. intleft=subImageBounds.x;
  78. inttop=subImageBounds.y;
  79. if(image.getWidth()<subImageBounds.width)
  80. left=(subImageBounds.width-image.getWidth())/2;
  81. if(image.getHeight()<subImageBounds.height)
  82. top=(subImageBounds.height-image.getHeight())/2;
  83. g.setColor(Color.white);
  84. g.fillRect(0,0,subImageBounds.width,subImageBounds.height);
  85. g.drawImage(image,left,top,null);
  86. ImageIO.write(image,formatName,destImageFile);
  87. }else{
  88. g.drawImage(image.getSubimage(subImageBounds.x,subImageBounds.y,
  89. subImageBounds.width,subImageBounds.height),0,0,null);
  90. }
  91. g.dispose();
  92. ImageIO.write(subImage,formatName,destImageFile);
  93. }
  94. }
  95.  
 
 
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();

猜你喜欢

转载自495142450.iteye.com/blog/2265546