Java修改圖片大小尺寸的簡(jiǎn)單實(shí)現(xiàn)
方式一:使用Image.getScaledInstance
使用jdk的awt包下的Image.getScaledInstance實(shí)現(xiàn)圖片的縮放。好處是無需引入第三方j(luò)ar,缺點(diǎn)是會(huì)稍微有點(diǎn)模糊。
工具類ImageUtils:
package utils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ImageUtils {
/**
* 通過BufferedImage圖片流調(diào)整圖片大小
*/
public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws IOException {
Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_AREA_AVERAGING);
BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);
return outputImage;
}
/**
* BufferedImage圖片流轉(zhuǎn)byte[]數(shù)組
*/
public static byte[] imageToBytes(BufferedImage bImage) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(bImage, "jpg", out);
} catch (IOException e) {
e.printStackTrace();
}
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) {
e.printStackTrace();
}
return image;
}
}
測(cè)試
1.通過 url 獲取圖片并調(diào)整大小后保存:
import utils.ImageUtils;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class Test {
public static void main(String[] args) {
try {
//通過url獲取BufferedImage圖像緩沖區(qū)
URL img = new URL("https://img1.360buyimg.com/image/jfs/t1/38591/20/3737/314695/5cc69c01E1838df09/dd6dce681bd23031.jpg");
BufferedImage image = ImageIO.read(img);
//獲取圖片的寬、高
System.out.println("Width: " + image.getWidth());
System.out.println("Height: " + image.getHeight());
//調(diào)整圖片大小為 400X400尺寸
BufferedImage newImage = ImageUtils.resizeImage(image,400,400);
//將緩沖區(qū)圖片保存到 F:/test/pic1.jpg (文件不存在會(huì)自動(dòng)創(chuàng)建文件保存,文件存在會(huì)覆蓋原文件保存)
ImageIO.write(newImage, "jpg", new File("F:/test/pic1.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.通過讀取圖片文件調(diào)整大小再保存:
import utils.ImageUtils;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
try {
//讀取原始圖片
BufferedImage image = ImageIO.read(new FileInputStream("F:/test/pic1.jpg"));
System.out.println("Width: " + image.getWidth());
System.out.println("Height: " + image.getHeight());
//調(diào)整圖片大小
BufferedImage newImage = ImageUtils.resizeImage(image,200,200);
//圖像緩沖區(qū)圖片保存為圖片文件(文件不存在會(huì)自動(dòng)創(chuàng)建文件保存,文件存在會(huì)覆蓋原文件保存)
ImageIO.write(newImage, "jpg", new File("F:/test/pic2.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.MultipartFile類型的圖片文件調(diào)整大小再保存:
public JSONObject imageSizeAdjustment(MultipartFile file) {
JSONObject result = new JSONObject();
try {
//從MultipartFile 中獲取 byte[]
byte[] bytes = file.getBytes();
//byte[]轉(zhuǎn) InputStream
InputStream in = new ByteArrayInputStream(bytes);
//讀取圖片輸入流為 BufferedImage
BufferedImage image = ImageIO.read(in);
System.out.println("Width: " + image.getWidth());
System.out.println("Height: " + image.getHeight());
//調(diào)整圖片大小
BufferedImage newImage = ImageUtils.resizeImage(image, 200, 200);
//圖像緩沖區(qū)圖片保存為圖片文件(文件不存在會(huì)自動(dòng)創(chuàng)建文件保存,文件存在會(huì)覆蓋原文件保存)
ImageIO.write(newImage, "jpg", new File("F:/test/pic2.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
result.put("code", 1);
result.put("note", "成功");
return result;
}
方式二:使用Thumbnailator
Thumbnailator是Java的開源圖像大小調(diào)整庫(kù),它使用漸進(jìn)式雙線性縮放。它支持JPG,BMP,JPEG,WBMP,PNG和GIF。
通過將以下Maven依賴項(xiàng)添加到我們的pom.xml中,將其包括在我們的項(xiàng)目中:
<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;
}
}
測(cè)試
和上面測(cè)試基本一樣只不過 ImageUtils.resizeImage換成 ThumbnailsUtils.resizeImageOne即可。
最終效果:
原圖:

長(zhǎng)寬縮為原來的三分之一后的圖:

如果需要將 BufferedImage 轉(zhuǎn)換為 MultipartFile,請(qǐng)參考我另一篇文章:
BufferedImage轉(zhuǎn)換為MultipartFile
其他圖片大小調(diào)整方法請(qǐng)參考:
How Can I Resize an Image Using Java
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
finally 一定會(huì)執(zhí)行(實(shí)例代碼)
下面小編就為大家?guī)硪黄猣inally 一定會(huì)執(zhí)行(實(shí)例代碼)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
Springboot遷移到Micronaut實(shí)現(xiàn)過程詳解
這篇文章主要為大家?介紹了Springboot遷移到Micronaut實(shí)現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Spring Security使用單點(diǎn)登錄的權(quán)限功能
本文主要介紹了Spring Security使用單點(diǎn)登錄的權(quán)限功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
mybatis-plus分頁(yè)查詢的實(shí)現(xiàn)實(shí)例
頁(yè)查詢是一項(xiàng)常用的數(shù)據(jù)庫(kù)查詢方法,本文主要介紹了mybatis-plus分頁(yè)查詢的實(shí)現(xiàn)實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06
一次由Lombok的@AllArgsConstructor注解引發(fā)的錯(cuò)誤及解決
這篇文章主要介紹了一次由Lombok的@AllArgsConstructor注解引發(fā)的錯(cuò)誤及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
idea注解參數(shù)換行時(shí)間日期格式設(shè)置方法
這篇文章主要介紹了idea注解參數(shù)換行時(shí)間日期格式設(shè)置方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05

