Java 使用Thumbnails對(duì)大圖片壓縮
引言
在最近的項(xiàng)目開發(fā)中,經(jīng)常會(huì)使用到圖片上傳,但是過大的圖片在查看的時(shí)候會(huì)影響打開速度,浪費(fèi)流量以及服務(wù)器存儲(chǔ)空間,所以需要在后端處理完圖片再上傳,這里我們用到了Thumbnails圖片處理工具類。
Thumbnails主要支持以下一些功能
1、指定大小進(jìn)行縮放
2、按照比例進(jìn)行縮放
3、不按照比例,指定大小進(jìn)行縮放
4、旋轉(zhuǎn)
5、水印
6、裁剪
7、轉(zhuǎn)化圖片格式
8、輸出到OutputStream
9、輸出到BufferedImage
使用步驟:
一、添加Maven
<dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency>
二、具體操作
1:指定大小進(jìn)行縮放
/**
* 指定大小進(jìn)行縮放
*
* @throws IOException
*/
private void test1() throws IOException {
/*
* size(width,height) 若圖片橫比200小,高比300小,不變
* 若圖片橫比200小,高比300大,高縮小到300,圖片比例不變 若圖片橫比200大,高比300小,橫縮小到200,圖片比例不變
* 若圖片橫比200大,高比300大,圖片按比例縮小,橫為200或高為300
*/
Thumbnails.of("images/test.jpg").size(200, 300).toFile("C:/image_200x300.jpg");
Thumbnails.of("images/test.jpg").size(2560, 2048).toFile("C:/image_2560x2048.jpg");
}
2:按照比例進(jìn)行縮放
/**
* 按照比例進(jìn)行縮放
* scale 圖片的壓縮比例 值在0-1之間,1f就是原圖,0.5就是原圖的一半大小 * outputQuality 圖片壓縮的質(zhì)量 值在0-1 之間,越接近1質(zhì)量越好,越接近0 質(zhì)量越差
* @throws IOException
*/
private void test2() throws IOException {
/**
* scale(比例)
*/
Thumbnails.of("images/test.jpg").scale(0.25f).outputQuality(0.8f).toFile("C:/image_25%.jpg");
Thumbnails.of("images/test.jpg").scale(0.75f).outputQuality(0.8f).toFile("C:/image_110%.jpg"); }
3:不按照比例,指定大小進(jìn)行縮放
/**
* 不按照比例,指定大小進(jìn)行縮放
*
* @throws IOException
*/
private void test3() throws IOException {
/**
* keepAspectRatio(false) 默認(rèn)是按照比例縮放的
*/
Thumbnails.of("images/test.jpg").size(120, 120).keepAspectRatio(false).toFile("C:/image_120x120.jpg"); }
4:旋轉(zhuǎn)
/**
* 旋轉(zhuǎn)
*
* @throws IOException
*/
private void test4() throws IOException {
/**
* rotate(角度),正數(shù):順時(shí)針 負(fù)數(shù):逆時(shí)針
*/
Thumbnails.of("images/test.jpg").size(1280, 1024).rotate(90).toFile("C:/image+90.jpg");
Thumbnails.of("images/test.jpg").size(1280, 1024).rotate(-90).toFile("C:/iamge-90.jpg");
}
5:水印
/**
* 水印
*
* @throws IOException
*/
private void test5() throws IOException {
/**
* watermark(位置,水印圖,透明度)
*/
Thumbnails.of("images/test.jpg").size(1280, 1024).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("images/watermark.png")), 0.5f)
.outputQuality(0.8f).toFile("C:/image_watermark_bottom_right.jpg");
Thumbnails.of("images/test.jpg").size(1280, 1024).watermark(Positions.CENTER, ImageIO.read(new File("images/watermark.png")), 0.5f)
.outputQuality(0.8f).toFile("C:/image_watermark_center.jpg");
}
6:裁剪
/**
* 裁剪
*
* @throws IOException
*/
private void test6() throws IOException {
/**
* 圖片中心400*400的區(qū)域
*/
Thumbnails.of("images/test.jpg").sourceRegion(Positions.CENTER, 400, 400).size(200, 200).keepAspectRatio(false)
.toFile("C:/image_region_center.jpg");
/**
* 圖片右下400*400的區(qū)域
*/
Thumbnails.of("images/test.jpg").sourceRegion(Positions.BOTTOM_RIGHT, 400, 400).size(200, 200).keepAspectRatio(false)
.toFile("C:/image_region_bootom_right.jpg");
/**
* 指定坐標(biāo)
*/
Thumbnails.of("images/test.jpg").sourceRegion(600, 500, 400, 400).size(200, 200).keepAspectRatio(false).toFile("C:/image_region_coord.jpg");
}
7:轉(zhuǎn)化圖片格式
/**
* 轉(zhuǎn)化圖片格式
*
* @throws IOException
*/
private void test7() throws IOException {
/**
* outputFormat(圖像格式)
*/
Thumbnails.of("images/test.jpg").size(1280, 1024).outputFormat("png").toFile("C:/image_1280x1024.png");
Thumbnails.of("images/test.jpg").size(1280, 1024).outputFormat("gif").toFile("C:/image_1280x1024.gif");
}
8:輸出到OutputStream
/**
* 輸出到OutputStream
*
* @throws IOException
*/
private void test8() throws IOException {
/**
* toOutputStream(流對(duì)象)
*/
OutputStream os = new FileOutputStream("C:/image_1280x1024_OutputStream.png");
Thumbnails.of("images/test.jpg").size(1280, 1024).toOutputStream(os);
}
9:輸出到BufferedImage
/**
* 輸出到BufferedImage
*
* @throws IOException
*/
private void test9() throws IOException {
/**
* asBufferedImage() 返回BufferedImage
*/
BufferedImage thumbnail = Thumbnails.of("images/test.jpg").size(1280, 1024).asBufferedImage();
ImageIO.write(thumbnail, "jpg", new File("C:/image_1280x1024_BufferedImage.jpg"));
}
三、對(duì)圖片文件進(jìn)行Base64操作
/**
* 對(duì)內(nèi)存中的圖片文件進(jìn)行Base64處理
*
* @throws IOException
*/
public String Base64ImageByMemory(BufferedImage pic) {
String imgString = "";
ByteArrayOutputStream newBaos = new ByteArrayOutputStream();//io流
try {
ImageIO.write(pic, "jpg", newBaos);//寫入流中
byte[] bytes = newBaos.toByteArray();//轉(zhuǎn)換成字節(jié)
imgString = URLEncoder.encode(new BASE64Encoder().encode(bytes), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return imgString;
}
四、獲取服務(wù)器圖片文件大小
/**
* 輸出到OutputStream
*
* @throws IOException
*/
public void getImageFileSize(){
int size;
URLConnection conn;
try {
String path="";
path="https://bkimg.cdn.bcebos.com/pic/a8773912b31bb051c36044e93b7adab44bede0af";//世界地圖
//path="http://10.30.23.217:9017/image/0c09ca36-abea-4efa-85b0-99b6d261f66c"; //服務(wù)器上圖片
URL url = new URL(path);
conn = url.openConnection();
size = conn.getContentLength();
if (size < 0){
System.out.println("Could not determine file size.");
}else{
System.out.println("The size of file is = " + size + " bytes");
BigDecimal filesize = new BigDecimal(size);
BigDecimal megabyte = new BigDecimal(1024 * 1024);
float returnValue = filesize.divide(megabyte, 2, BigDecimal.ROUND_UP).floatValue();
System.out.println("The size of file is = "+returnValue+"M");
}
conn.getInputStream().close();
} catch (IOException e) {
e.printStackTrace();
}
}

至此,圖片壓縮的相關(guān)處理和Base64以及獲取服務(wù)器文件大小的功能就總結(jié)完了!
以上就是Java 使用Thumbnails對(duì)大圖片壓縮的詳細(xì)內(nèi)容,更多關(guān)于java 大圖片壓縮的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java編程中靜態(tài)內(nèi)部類與同步類的寫法示例
這篇文章主要介紹了Java編程中靜態(tài)內(nèi)部類與同步類的寫法示例,用于構(gòu)建靜態(tài)對(duì)象以及實(shí)現(xiàn)線程同步等,需要的朋友可以參考下2015-09-09
java實(shí)現(xiàn)微信企業(yè)付款到個(gè)人功能
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)微信企業(yè)付款到個(gè)人功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
基于SpringBoot開機(jī)啟動(dòng)與@Order注解
這篇文章主要介紹了SpringBoot開機(jī)啟動(dòng)與@Order注解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
java定時(shí)任務(wù)的實(shí)現(xiàn)方式
這篇文章主要介紹了java定時(shí)任務(wù)的實(shí)現(xiàn)方式,在應(yīng)用里經(jīng)常都有用到在后臺(tái)跑定時(shí)任務(wù)的需求,如何進(jìn)行java定時(shí)任務(wù),本文為大家進(jìn)行講解,感興趣的小伙伴們可以參考一下2016-02-02
SpringBoot整合mybatis-generator-maven-plugin的方法
這篇文章主要介紹了SpringBoot整合mybatis-generator-maven-plugin,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
spring boot在啟動(dòng)項(xiàng)目之后執(zhí)行的實(shí)現(xiàn)方法
在開發(fā)時(shí)有時(shí)候需要在整個(gè)應(yīng)用開始運(yùn)行時(shí)執(zhí)行一些特定代碼,比如初始化環(huán)境,下面這篇文章就來給大家介紹了關(guān)于spring boot在啟動(dòng)項(xiàng)目之后執(zhí)行自己要執(zhí)行的東西的實(shí)現(xiàn)方法,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考下。2017-09-09
Plugin ‘org.springframework.boot:spring-boot-maven-plug
這篇文章給大家介紹了Plugin ‘org.springframework.boot:spring-boot-maven-plugin:‘ not found的解決方案,親測(cè)可用,文中給出了兩種解決方法,需要的朋友可以參考下2024-01-01
Java?面向?qū)ο笸ㄟ^new揭開對(duì)象實(shí)例化
各位鐵汁們大家好呀,我們上次博客講了,通過?Student?student1?=?new?Student();就可以實(shí)例化一個(gè)對(duì)象,這個(gè)對(duì)象就有Student類中的所以成員變量??墒?對(duì)象student1?和?類Student到底是怎樣建立聯(lián)系的,在內(nèi)存中到底發(fā)生了什么2022-04-04

