Java圖片批量壓縮像素的實現(xiàn)方法
圖片壓縮大法
為了防止用戶流量的丟失,即使在5g 即將來臨的情況下,壓縮算法依舊是很有必要的,額跑題了,不好意思,今天介紹的不是壓縮算法,講啥呢?主要講講如何通過 java 將圖片進行壓縮,盡可能的控制壓縮損比,不僅僅是為了減少存儲,其目的是快速呈現(xiàn)給用戶,只有良好的體驗,才會在當(dāng)今這個急躁的年代減少流量的損失。
最近因為公司要需要xxx認(rèn)證上傳測試用例功能的具體截圖、發(fā)現(xiàn)有大小限制、所以就進行了圖片壓縮,簡單記錄一下。
壓縮前大?。?/h2>

壓縮后大小:

具體代碼實現(xiàn):
main方法測試:
public static void main(String[] args) throws IOException {
String modpath = "C:\\Users\\Administrator\\Desktop\\鯤鵬認(rèn)證\\test\\";
getFiles("C:\\Users\\Administrator\\Desktop\\鯤鵬認(rèn)證\\測試用例清單", 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修改的圖片會變色
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());
}
控制臺目錄壓縮成功保存到盤符:

附:利用Graphics類如何進行壓縮圖像
Graphics類提供基本繪圖方法,Graphics類提供基本的幾何圖形繪制方法,主要有:畫線段、畫矩形、畫圓、畫帶顏色的圖形、畫橢圓、畫圓弧、畫多邊形、畫字符串等。 這里不做一一贅述, 進重點介紹一下,利用Graphics類如何進行壓縮圖像。不多說直接上代碼。
/**
* 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("");
}
}
其實,關(guā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圖片批量壓縮像素實現(xiàn)的文章就介紹到這了,更多相關(guān)Java圖片批量壓縮像素內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實現(xiàn)的properties文件動態(tài)修改并自動保存工具類
這篇文章主要介紹了Java實現(xiàn)的properties文件動態(tài)修改并自動保存工具類,可實現(xiàn)針對properties配置文件的相關(guān)修改與保存功能,需要的朋友可以參考下2017-11-11
Java數(shù)據(jù)脫敏實現(xiàn)的方法總結(jié)
數(shù)據(jù)脫敏,指的是對某些敏感信息通過脫敏規(guī)則進行數(shù)據(jù)的變形,實現(xiàn)敏感隱私數(shù)據(jù)的可靠保護,本文主要是對后端數(shù)據(jù)脫敏實現(xiàn)的簡單總結(jié),希望對大家有所幫助2023-07-07
詳解springboot使用異步注解@Async獲取執(zhí)行結(jié)果的坑
本文主要介紹了springboot使用異步注解@Async獲取執(zhí)行結(jié)果的坑,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
Spring Security基本架構(gòu)與初始化操作流程詳解
這篇文章主要介紹了Spring Security基本架構(gòu)與初始化操作流程,Spring Security是一個能夠為基于Spring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架2023-03-03
java注解結(jié)合aspectj AOP進行日志打印的操作
這篇文章主要介紹了java注解結(jié)合aspectj AOP進行日志打印的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02

