欢迎来到天天文库
浏览记录
ID:34504524
大小:43.38 KB
页数:4页
时间:2019-03-07
《黑马程序员java教程java图片缩放》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、用Java实现图片的缩放。1.packagecom.test;2.3.importcom.sun.image.codec.jpeg.JPEGImageEncoder;4.importcom.sun.image.codec.jpeg.JPEGCodec;5.importcom.sun.image.codec.jpeg.JPEGEncodeParam;6.importjavax.swing.*;7.importjava.io.File;8.importjava.io.FileOutputStream;9.importjava.io.IOException;10.importjava.awt.
2、*;11.importjava.awt.image.BufferedImage;12.importjava.awt.image.Kernel;13.importjava.awt.image.ConvolveOp;14.15.publicclassImageUtil{16.17. publicstaticvoidresize(FileoriginalFile,FileresizedFile,18. intnewWidth,floatquality)throwsIOException{19.20. if(quality>1){21.
3、 thrownewIllegalArgumentException(22. "Qualityhastobebetween0and1");23. }24.25. ImageIconii=newImageIcon(originalFile.getCanonicalPath());26. Imagei=ii.getImage();27. ImageresizedImage=null;28.29. intiWidth=i.getW
4、idth(null);30. intiHeight=i.getHeight(null);1.2. if(iWidth>iHeight){3. resizedImage=i.getScaledInstance(newWidth,(newWidth*iHeight)4. /iWidth,Image.SCALE_SMOOTH);5. }else{6. resizedImage=i.getScaledInstance((newWidt
5、h*iWidth)/iHeight,7. newWidth,Image.SCALE_SMOOTH);8. }9.10. //Thiscodeensuresthatallthepixelsintheimageareloaded.11. Imagetemp=newImageIcon(resizedImage).getImage();12.13. //Createthebufferedimage.14. BufferedImagebufferedImage
6、=newBufferedImage(temp.getWidth(null),15. temp.getHeight(null),BufferedImage.TYPE_INT_RGB);16.17. //Copyimagetobufferedimage.18. Graphicsg=bufferedImage.createGraphics();19.20. //Clearbackgroundandpainttheimage.21. g.setColor(Color.white);2
7、2. g.fillRect(0,0,temp.getWidth(null),temp.getHeight(null));23. g.drawImage(temp,0,0,null);24. g.dispose();25.26. //Soften.27. floatsoftenFactor=0.05f;28. float[]so
此文档下载收益归作者所有