Java圖片批量壓縮像素的實(shí)現(xiàn)方法
圖片壓縮大法
為了防止用戶流量的丟失,即使在5g 即將來(lái)臨的情況下,壓縮算法依舊是很有必要的,額跑題了,不好意思,今天介紹的不是壓縮算法,講啥呢?主要講講如何通過(guò) java 將圖片進(jìn)行壓縮,盡可能的控制壓縮損比,不僅僅是為了減少存儲(chǔ),其目的是快速呈現(xiàn)給用戶,只有良好的體驗(yàn),才會(huì)在當(dāng)今這個(gè)急躁的年代減少流量的損失。
最近因?yàn)楣疽枰獂xx認(rèn)證上傳測(cè)試用例功能的具體截圖、發(fā)現(xiàn)有大小限制、所以就進(jìn)行了圖片壓縮,簡(jiǎn)單記錄一下。
壓縮前大小:
壓縮后大?。?/h2>
具體代碼實(shí)現(xiàn):
main方法測(cè)試:
public static void main(String[] args) throws IOException { String modpath = "C:\\Users\\Administrator\\Desktop\\鯤鵬認(rèn)證\\test\\"; getFiles("C:\\Users\\Administrator\\Desktop\\鯤鵬認(rèn)證\\測(cè)試用例清單", modpath, 160);//將圖片壓縮至100寬 }
文件大小處理
/** * @param srcPath 原圖片路徑 * @param desPath 轉(zhuǎn)換大小后圖片路徑 * @param width 轉(zhuǎn)換后圖片寬度 * @param height 轉(zhuǎn)換后圖片高度 */ public static void resizeImage(String srcPath, String desPath, int width, int height) throws IOException { File srcFile = new File(srcPath); Image srcImg = ImageIO.read(srcFile); BufferedImage buffImg = null; buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); //使用TYPE_INT_RGB修改的圖片會(huì)變色 buffImg.getGraphics().drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null); String filePath=""; if (srcFile.getName().contains("#")) { filePath = srcFile.getName().replace("#", ""); }else{ filePath=srcFile.getName(); } ImageIO.write(buffImg, "PNG", new File(desPath + filePath)); }
獲取目錄文件信息
/** * @param scaleSize 圖片的修改比例,目標(biāo)寬度 */ public static void getFiles(String path, String modPath, int scaleSize) throws IOException { ArrayList<String> files = new ArrayList<String>(); File file = new File(path); File[] tempList = file.listFiles(); //循環(huán)讀取目錄下圖片 for (int i = 0; i < tempList.length; i++) { String filePath = tempList[i].getName(); if (tempList[i].isFile()) { System.out.println("文件:" + filePath + "-" + tempList[i].getAbsolutePath().replaceAll("\\\\", "/")); String[] imagePath = tempList[i].getAbsolutePath().replaceAll("\\\\", "/").split("/"); String imageNumber = null; FileUtil.resizeImage(tempList[i].getAbsolutePath().replaceAll("\\\\", "/"), modPath, 160, 160); files.add(tempList[i].toString()); } if (tempList[i].isDirectory()) { System.out.println("文件夾:" + tempList[i]); } } System.out.println(path + "下文件數(shù)量:" + files.size()); }
控制臺(tái)目錄壓縮成功保存到盤符:
附:利用Graphics類如何進(jìn)行壓縮圖像
Graphics類提供基本繪圖方法,Graphics類提供基本的幾何圖形繪制方法,主要有:畫線段、畫矩形、畫圓、畫帶顏色的圖形、畫橢圓、畫圓弧、畫多邊形、畫字符串等。 這里不做一一贅述, 進(jìn)重點(diǎn)介紹一下,利用Graphics類如何進(jìn)行壓縮圖像。不多說(shuō)直接上代碼。
/** * compressImage * * @param imageByte * Image source array * @param ppi * @return */ public static byte[] compressImage(byte[] imageByte, int ppi) { byte[] smallImage = null; int width = 0, height = 0; if (imageByte == null) return null; ByteArrayInputStream byteInput = new ByteArrayInputStream(imageByte); try { Image image = ImageIO.read(byteInput); int w = image.getWidth(null); int h = image.getHeight(null); // adjust weight and height to avoid image distortion double scale = 0; scale = Math.min((float) ppi / w, (float) ppi / h); width = (int) (w * scale); width -= width % 4; height = (int) (h * scale); if (scale >= (double) 1) return imageByte; BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); buffImg.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(buffImg, "png", out); smallImage = out.toByteArray(); return smallImage; } catch (IOException e) { log.error(e.getMessage()); throw new RSServerInternalException(""); } }
其實(shí),關(guān)鍵點(diǎn)就兩處
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); buffImg.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
總結(jié)
到此這篇關(guān)于Java圖片批量壓縮像素實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java圖片批量壓縮像素內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)的properties文件動(dòng)態(tài)修改并自動(dòng)保存工具類
這篇文章主要介紹了Java實(shí)現(xiàn)的properties文件動(dòng)態(tài)修改并自動(dòng)保存工具類,可實(shí)現(xiàn)針對(duì)properties配置文件的相關(guān)修改與保存功能,需要的朋友可以參考下2017-11-11java多線程數(shù)據(jù)分頁(yè)處理實(shí)例講解
在本篇內(nèi)容里小編給大家分享了一篇關(guān)于java多線程數(shù)據(jù)分頁(yè)處理實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-01-01Java數(shù)據(jù)脫敏實(shí)現(xiàn)的方法總結(jié)
數(shù)據(jù)脫敏,指的是對(duì)某些敏感信息通過(guò)脫敏規(guī)則進(jìn)行數(shù)據(jù)的變形,實(shí)現(xiàn)敏感隱私數(shù)據(jù)的可靠保護(hù),本文主要是對(duì)后端數(shù)據(jù)脫敏實(shí)現(xiàn)的簡(jiǎn)單總結(jié),希望對(duì)大家有所幫助2023-07-07詳解springboot使用異步注解@Async獲取執(zhí)行結(jié)果的坑
本文主要介紹了springboot使用異步注解@Async獲取執(zhí)行結(jié)果的坑,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Spring Security基本架構(gòu)與初始化操作流程詳解
這篇文章主要介紹了Spring Security基本架構(gòu)與初始化操作流程,Spring Security是一個(gè)能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架2023-03-03java注解結(jié)合aspectj AOP進(jìn)行日志打印的操作
這篇文章主要介紹了java注解結(jié)合aspectj AOP進(jìn)行日志打印的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02