Java調(diào)整圖片大小的3種方式小結(jié)
1:使用Thumbnailator
Thumbnailator是Java的開源圖像大小調(diào)整庫,它使用漸進(jìn)式雙線性縮放。它支持JPG,BMP,JPEG,WBMP,PNG和GIF。
通過將以下Maven依賴項(xiàng)添加到我們的pom.xml中,將其包括在我們的項(xiàng)目中:
pom.xml
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.11</version>
</dependency>
工具類ThumbnailsUtils
import net.coobird.thumbnailator.Thumbnails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ThumbnailsUtils{
private static final Logger logger = LoggerFactory.getLogger(ThumbnailsUtils.class);
/**
* 通過BufferedImage圖片流調(diào)整圖片大小
*/
public static BufferedImage resizeImageOne(BufferedImage originalImage, int targetWidth, int targetHeight) throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Thumbnails.of(originalImage)
.size(targetWidth, targetHeight)
.outputFormat("JPEG")
.outputQuality(1)
.toOutputStream(outputStream);
byte[] data = outputStream.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
return ImageIO.read(inputStream);
}
/**
* BufferedImage圖片流轉(zhuǎn)byte[]數(shù)組
*/
public static byte[] imageToBytes(BufferedImage bImage) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(bImage, "jpg", out);
} catch (IOException e) {
logger.error("錯(cuò)誤信息: ", e);
}
return out.toByteArray();
}
/**
* byte[]數(shù)組轉(zhuǎn)BufferedImage圖片流
*/
private static BufferedImage bytesToBufferedImage(byte[] ImageByte) {
ByteArrayInputStream in = new ByteArrayInputStream(ImageByte);
BufferedImage image = null;
try {
image = ImageIO.read(in);
} catch (IOException e) {
logger.error("錯(cuò)誤信息: ", e);
}
return image;
}
}
2:Graphics2D 自帶的方法
public static BufferedImage scaleImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
BufferedImage scaledImage = new BufferedImage(targetWidth, targetHeight, type);
Graphics2D g = scaledImage.createGraphics();
// Calculate the ratio between the original and scaled image size
double scaleX = (double) targetWidth / originalImage.getWidth();
double scaleY = (double) targetHeight / originalImage.getHeight();
double scale = Math.min(scaleX, scaleY);
// Now we perform the actual scaling
int newWidth = (int) (originalImage.getWidth() * scale);
int newHeight = (int) (originalImage.getHeight() * scale);
int x = (targetWidth - newWidth) / 2;
int y = (targetHeight - newHeight) / 2;
g.drawImage(originalImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), x, y, null);
g.dispose();
return scaledImage;
} public static void main(String[] args) throws Exception {
// 讀取原始圖片
File originalFile = new File("D:\\test1\\schoolLogo.png");
BufferedImage originalImage = ImageIO.read(originalFile);
// 設(shè)定目標(biāo)寬高
int targetWidth = 1000; // 兩倍放大
int targetHeight = 1000; // 兩倍放大
// 調(diào)用 resizeImageOne 方法進(jìn)行放大
BufferedImage resizedImage = ThumbnailsUtils.scaleImage(originalImage, targetWidth, targetHeight);
// 將放大后的圖片保存到文件
File outputFile = new File("D:\\test1\\big.png");
ImageIO.write(resizedImage, "png", outputFile);
}3:前兩種我在使用 ImageCombiner 項(xiàng)目的時(shí)候 不生效;
// 創(chuàng)建新的 BufferedImage,并設(shè)置繪制質(zhì)量
BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = resizedImage.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
// 繪制原始圖像到新的 BufferedImage,并進(jìn)行縮放
Image scaledImage = image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
g2d.drawImage(scaledImage, 0, 0, null);
g2d.dispose();
// 保存新的圖片
String outputImagePath = "C:\\Users\\up1.jpg"; // 替換為實(shí)際的輸出路徑
ImageIO.write(resizedImage, "jpg", new File(outputImagePath));
System.out.println("圖片尺寸調(diào)整完成!");總結(jié)
到此這篇關(guān)于Java調(diào)整圖片大小的3種方式小結(jié)的文章就介紹到這了,更多相關(guān)Java調(diào)整圖片大小內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用位運(yùn)算實(shí)現(xiàn)加減乘除詳解
這篇文章主要為大家詳細(xì)介紹了Java如何使用位運(yùn)算實(shí)現(xiàn)加減乘除,文中的示例代碼講解詳細(xì),對我們深入了解Java有一定的幫助,感興趣的可以了解一下2023-05-05
Spring Boot集成MyBatis-Plus 自定義攔截器實(shí)現(xiàn)動態(tài)表名切換功能
本文介紹了如何在SpringBoot項(xiàng)目中集成MyBatis-Plus,并通過自定義攔截器實(shí)現(xiàn)動態(tài)表名切換,此外,還探討了MyBatis攔截器在其他場景中的應(yīng)用,如SQL日志記錄、多租戶數(shù)據(jù)隔離、數(shù)據(jù)權(quán)限控制等,感興趣的朋友跟隨小編一起看看吧2024-11-11
java http連接池的實(shí)現(xiàn)方式(帶有失敗重試等高級功能)
這篇文章主要介紹了java http連接池的實(shí)現(xiàn)方式(帶有失敗重試等高級功能),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
Spring SpringMVC,Spring整合MyBatis 事務(wù)配置的詳細(xì)流程
這篇文章給大家介紹SSM整合詳細(xì)流程步驟 Spring SpringMVC,Spring整合MyBatis 事務(wù)配置,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-10-10
Java中的MarkerFilter的應(yīng)用場景及使用示例詳解
這篇文章主要介紹了Java中的MarkerFilter的應(yīng)用場景及使用示例詳解,使用log4j2,負(fù)責(zé)從消息隊(duì)列收集日志的,現(xiàn)在系統(tǒng)收集到的日志能和這個(gè)系統(tǒng)本身的日志分開,需要的朋友可以參考下2024-01-01
基于mybatis注解動態(tài)sql中foreach工具的方法
這篇文章主要介紹了mybatis注解動態(tài)sql中foreach工具方法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Springboot整合redis實(shí)現(xiàn)發(fā)布訂閱功能介紹步驟
發(fā)布訂閱作為一種設(shè)計(jì)思想在很多開源組件中都有體現(xiàn),比如大家熟知的消息中間件等,可謂把發(fā)布訂閱這一思想體現(xiàn)的淋漓盡致了2022-09-09

