Java實(shí)現(xiàn)的上傳并壓縮圖片功能【可等比例壓縮或原尺寸壓縮】
本文實(shí)例講述了Java實(shí)現(xiàn)的上傳并壓縮圖片功能。分享給大家供大家參考,具體如下:
先看效果:
原圖:1.33M
處理后:27.4kb
關(guān)鍵代碼:
package codeGenerate.util; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import javax.imageio.ImageIO; public class ImageZipUtil { public static void main(String[] args) { zipWidthHeightImageFile(new File("C:\\spider\\3.png"),new File("C:\\spider\\3-1.jpg"),425,638,0.7f); //zipImageFile(new File("C:\\spider\\2.JPG"),new File("C:\\spider\\2-2.JPG"),425,638,0.7f); //zipImageFile(new File("C:\\spider\\3.jpg"),new File("C:\\spider\\3-3.jpg"),425,638,0.7f); System.out.println("ok"); } /** * 根據(jù)設(shè)置的寬高等比例壓縮圖片文件<br> 先保存原文件,再壓縮、上傳 * @param oldFile 要進(jìn)行壓縮的文件 * @param newFile 新文件 * @param width 寬度 //設(shè)置寬度時(shí)(高度傳入0,等比例縮放) * @param height 高度 //設(shè)置高度時(shí)(寬度傳入0,等比例縮放) * @param quality 質(zhì)量 * @return 返回壓縮后的文件的全路徑 */ public static String zipImageFile(File oldFile,File newFile, int width, int height,float quality) { if (oldFile == null) { return null; } try { /** 對服務(wù)器上的臨時(shí)文件進(jìn)行處理 */ Image srcFile = ImageIO.read(oldFile); int w = srcFile.getWidth(null); int h = srcFile.getHeight(null); double bili; if(width>0){ bili=width/(double)w; height = (int) (h*bili); }else{ if(height>0){ bili=height/(double)h; width = (int) (w*bili); } } String srcImgPath = newFile.getAbsoluteFile().toString(); System.out.println(srcImgPath); String subfix = "jpg"; subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".")+1,srcImgPath.length()); BufferedImage buffImg = null; if(subfix.equals("png")){ buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); }else{ buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); } Graphics2D graphics = buffImg.createGraphics(); graphics.setBackground(new Color(255,255,255)); graphics.setColor(new Color(255,255,255)); graphics.fillRect(0, 0, width, height); graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null); ImageIO.write(buffImg, subfix, new File(srcImgPath)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return newFile.getAbsolutePath(); } /** * 按設(shè)置的寬度高度壓縮圖片文件<br> 先保存原文件,再壓縮、上傳 * @param oldFile 要進(jìn)行壓縮的文件全路徑 * @param newFile 新文件 * @param width 寬度 * @param height 高度 * @param quality 質(zhì)量 * @return 返回壓縮后的文件的全路徑 */ public static String zipWidthHeightImageFile(File oldFile,File newFile, int width, int height,float quality) { if (oldFile == null) { return null; } String newImage = null; try { /** 對服務(wù)器上的臨時(shí)文件進(jìn)行處理 */ Image srcFile = ImageIO.read(oldFile); String srcImgPath = newFile.getAbsoluteFile().toString(); System.out.println(srcImgPath); String subfix = "jpg"; subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".")+1,srcImgPath.length()); BufferedImage buffImg = null; if(subfix.equals("png")){ buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); }else{ buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); } Graphics2D graphics = buffImg.createGraphics(); graphics.setBackground(new Color(255,255,255)); graphics.setColor(new Color(255,255,255)); graphics.fillRect(0, 0, width, height); graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null); ImageIO.write(buffImg, subfix, new File(srcImgPath)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return newImage; } }
說明:
1、根據(jù)需求大家可以自行設(shè)置質(zhì)量參數(shù)quality,到底設(shè)置成多少,可以先看下效果在取值;
2、網(wǎng)上通用的方法用的是jdk自帶jar包中方法,我這里衍生了一下:用Graphics2D
,能夠同時(shí)處理jpg和png格式;
3、new Color(255,255,255)
是白色,等同于WHITE,但是用WHITE 的話,Linux下某些圖片會有其它色值;
4、main中的寬425和高638可以根據(jù)自己的需求自行設(shè)置,但是對于長和寬一樣的,按照400(小值的值425)*400來處理;
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java圖片操作技巧匯總》、《java日期與時(shí)間操作技巧匯總》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》及《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》。
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java實(shí)現(xiàn)五子棋的基礎(chǔ)方法
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)五子棋的基礎(chǔ)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09Java+opencv3.2.0實(shí)現(xiàn)輪廓檢測
這篇文章主要為大家詳細(xì)介紹了Java+opencv3.2.0實(shí)現(xiàn)輪廓檢測,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07如何使用IntelliJ IDEA的HTTP Client進(jìn)行接口驗(yàn)證
這篇文章主要介紹了如何使用IntelliJ IDEA的HTTP Client進(jìn)行接口驗(yàn)證,本文給大家分享最新完美解決方案,感興趣的朋友跟隨小編一起看看吧2024-06-06Java實(shí)現(xiàn)入?yún)?shù)據(jù)批量數(shù)據(jù)校驗(yàn)詳解
在業(yè)務(wù)處理中一般入?yún)⑹菃螚l數(shù)據(jù),這樣數(shù)據(jù)校驗(yàn)比較容易,但是這種方法對于集合數(shù)據(jù)的校驗(yàn)不適用,下面我們就來看看如何對入?yún)?shù)據(jù)進(jìn)行批量數(shù)據(jù)校驗(yàn)吧2024-02-02詳解SpringCloud Finchley Gateway 統(tǒng)一異常處理
這篇文章主要介紹了詳解SpringCloud Finchley Gateway 統(tǒng)一異常處理,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-10-10