Java調(diào)整圖片大小的3種方式小結(jié)
1:使用Thumbnailator
Thumbnailator是Java的開源圖像大小調(diào)整庫,它使用漸進式雙線性縮放。它支持JPG,BMP,JPEG,WBMP,PNG和GIF。
通過將以下Maven依賴項添加到我們的pom.xml中,將其包括在我們的項目中:
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("錯誤信息: ", 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("錯誤信息: ", 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);
// 設定目標寬高
int targetWidth = 1000; // 兩倍放大
int targetHeight = 1000; // 兩倍放大
// 調(diào)用 resizeImageOne 方法進行放大
BufferedImage resizedImage = ThumbnailsUtils.scaleImage(originalImage, targetWidth, targetHeight);
// 將放大后的圖片保存到文件
File outputFile = new File("D:\\test1\\big.png");
ImageIO.write(resizedImage, "png", outputFile);
}3:前兩種我在使用 ImageCombiner 項目的時候 不生效;
// 創(chuàng)建新的 BufferedImage,并設置繪制質(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,并進行縮放
Image scaledImage = image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
g2d.drawImage(scaledImage, 0, 0, null);
g2d.dispose();
// 保存新的圖片
String outputImagePath = "C:\\Users\\up1.jpg"; // 替換為實際的輸出路徑
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)文章
SpringBoot下獲取resources目錄下文件的常用方法
本文詳細介紹了SpringBoot獲取resources目錄下文件的常用方法,包括使用this.getClass()方法、ClassPathResource獲取以及hutool工具類ResourceUtil獲取,感興趣的可以了解一下2024-10-10
Druid關(guān)閉監(jiān)控頁面關(guān)閉不了的問題及解決
這篇文章主要介紹了Druid關(guān)閉監(jiān)控頁面關(guān)閉不了的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
SpringBoot框架DataSource多數(shù)據(jù)源配置方式
這篇文章主要介紹了SpringBoot框架DataSource多數(shù)據(jù)源配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
使用Cloud?Studio構(gòu)建SpringSecurity權(quán)限框架(騰訊云?Cloud?Studio?實戰(zhàn)訓練
隨著云計算技術(shù)的成熟和普及,傳統(tǒng)編程能力和資源以云服務的形式開放出來,從中間件、數(shù)據(jù)庫等水平能力服務組件到人臉識別、鑒權(quán)服務等基本業(yè)務服務組件很容易的在云端獲取,本文介紹使用Cloud?Studio構(gòu)建SpringSecurity權(quán)限框架的相關(guān)知識,感興趣的朋友一起看看吧2023-08-08
詳解Intellij IDEA中.properties文件中文顯示亂碼問題的解決
這篇文章主要介紹了詳解Intellij IDEA中.properties文件中文顯示亂碼問題的解決,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11

