SpringBoot如何獲取圖片的寬高
最近在做圖片處理,這里介紹一下Java中常見的獲取圖片寬高尺寸的方法。
使用BufferedImage(推薦)
適用于常見圖片格式,如果是處理本地文件系統(tǒng)中的圖片文件,可以使用 Java 的ImageIO類結(jié)合BufferedImage來獲取寬高,示例代碼如下:
/**
* 使用BufferedImage(適用于常見圖片格式)
* 如果是處理本地文件系統(tǒng)中的圖片文件,可以使用 Java 的ImageIO類結(jié)合BufferedImage來獲取寬高。
* @param imagePath 圖片path
* @return key是寬,value是高
*/
public static ImmutablePair<Integer, Integer> bufferedImageMeasure(String imagePath){
try {
// 讀取圖片文件,將其轉(zhuǎn)換為BufferedImage對(duì)象
File imageFile = new File(imagePath);
BufferedImage image = ImageIO.read(imageFile);
// 獲取圖片的寬度和高度
int width = image.getWidth();
int height = image.getHeight();
return new ImmutablePair<>(width, height);
} catch (IOException e) {
log.error("error", e);
}
return new ImmutablePair<>(0, 0);
}上述代碼中:
- 首先通過ImageIO.read()方法讀取指定路徑的圖片文件,將其轉(zhuǎn)換為BufferedImage對(duì)象。
- 然后利用BufferedImage的getWidth()和getHeight()方法分別獲取圖片的寬度和高度,并進(jìn)行輸出。
使用Image類(推薦)
使用Image類的getWidth()和getHeight()方法(更通用一些情況)。這種方式可以用于處理不僅僅是本地文件的圖片資源,比如網(wǎng)絡(luò)圖片等(不過需要先獲取圖片的輸入流等額外操作),示例代碼片段如下:
/**
* 用于處理不僅僅是本地文件的圖片資源,比如網(wǎng)絡(luò)圖片等(不過需要先獲取圖片的輸入流等額外操作)
* @param imageUrl 圖片url
* @return key是寬,value是高
*/
public static ImmutablePair<Integer, Integer> imageMeasure(String imageUrl){
try {
URL url = new URL(imageUrl);
ImageIcon icon = new ImageIcon(url);
Image image = icon.getImage();
// 獲取寬度和高度
int width = image.getWidth(null);
int height = image.getHeight(null);
return new ImmutablePair<>(width, height);
} catch (IOException e) {
e.printStackTrace();
}
return new ImmutablePair<>(0, 0);
}這段代碼先通過URL對(duì)象指定圖片的來源(這里假設(shè)是網(wǎng)絡(luò)圖片,若是本地文件可以通過FileInputStream等構(gòu)建相應(yīng)的輸入流再轉(zhuǎn)換為Image對(duì)象),然后借助ImageIcon獲取Image對(duì)象,進(jìn)而調(diào)用其getWidth()和getHeight()方法獲取寬高信息。
更底層的字節(jié)流方式(未驗(yàn)證)
對(duì)于處理一些特定格式(如 PNG、JPEG 等)更底層的字節(jié)流方式(相對(duì)復(fù)雜且特定場(chǎng)景)以處理 JPEG 格式圖片為例,通過解析 JPEG 文件頭信息來獲取寬高,示例代碼如下:
/**
*
* @param imagePath 圖片地址
* @return key是寬,value是高
*/
public static ImmutablePair<Integer, Integer> fileInputStreamMeasure(String imagePath){
try {
FileInputStream fis = new FileInputStream(imagePath);
byte[] header = new byte[16];
fis.read(header);
if (header[0] == (byte) 0xFF && header[1] == (byte) 0xD8 && header[2] == (byte) 0xFF &&
(header[3] & 0xF0) == 0xE0) {
// 解析寬度
int width = ((header[7] << 8) & 0xFF00) | (header[8] & 0xFF);
// 解析高度
int height = ((header[9] << 8) & 0xFF00) | (header[10] & 0xFF);
return new ImmutablePair<>(width, height);
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
return new ImmutablePair<>(0, 0);
}這種方式是直接讀取圖片文件的字節(jié)流,通過解析特定格式(這里以 JPEG 為例)的文件頭中記錄寬高的字節(jié)位置來獲取相應(yīng)信息,不過這種方式對(duì)不同格式要按照其各自規(guī)范去解析,通用性不如前面利用 Java 標(biāo)準(zhǔn)庫(kù)中圖像相關(guān)類的方法,一般用于對(duì)性能等有特殊要求且明確圖片格式的場(chǎng)景。
完整代碼
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.ImmutablePair;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.io.FileInputStream;
@Slf4j
public class ImageHelper {
/**
* 使用BufferedImage(適用于常見圖片格式)
* 如果是處理本地文件系統(tǒng)中的圖片文件,可以使用 Java 的ImageIO類結(jié)合BufferedImage來獲取寬高。
* @param imagePath 圖片path
* @return key是寬,value是高
*/
public static ImmutablePair<Integer, Integer> bufferedImageMeasure(String imagePath){
try {
// 讀取圖片文件,將其轉(zhuǎn)換為BufferedImage對(duì)象
File imageFile = new File(imagePath);
BufferedImage image = ImageIO.read(imageFile);
// 獲取圖片的寬度和高度
int width = image.getWidth();
int height = image.getHeight();
return new ImmutablePair<>(width, height);
} catch (IOException e) {
log.error("error", e);
}
return new ImmutablePair<>(0, 0);
}
/**
* 用于處理不僅僅是本地文件的圖片資源,比如網(wǎng)絡(luò)圖片等(不過需要先獲取圖片的輸入流等額外操作)
* @param imageUrl 圖片url
* @return key是寬,value是高
*/
public static ImmutablePair<Integer, Integer> imageMeasure(String imageUrl){
try {
URL url = new URL(imageUrl);
ImageIcon icon = new ImageIcon(url);
Image image = icon.getImage();
// 獲取寬度和高度
int width = image.getWidth(null);
int height = image.getHeight(null);
return new ImmutablePair<>(width, height);
} catch (IOException e) {
e.printStackTrace();
}
return new ImmutablePair<>(0, 0);
}
/**
*
* @param imagePath 圖片地址
* @return key是寬,value是高
*/
public static ImmutablePair<Integer, Integer> fileInputStreamMeasure(String imagePath){
try {
FileInputStream fis = new FileInputStream(imagePath);
byte[] header = new byte[16];
fis.read(header);
if (header[0] == (byte) 0xFF && header[1] == (byte) 0xD8 && header[2] == (byte) 0xFF &&
(header[3] & 0xF0) == 0xE0) {
// 解析寬度
int width = ((header[7] << 8) & 0xFF00) | (header[8] & 0xFF);
// 解析高度
int height = ((header[9] << 8) & 0xFF00) | (header[10] & 0xFF);
return new ImmutablePair<>(width, height);
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
return new ImmutablePair<>(0, 0);
}
}源碼地址
https://github.com/toutouge/javademosecond
到此這篇關(guān)于SpringBoot如何獲取圖片的寬高的文章就介紹到這了,更多相關(guān)SpringBoot獲取圖片寬高內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于PreparedStatement的setObject作用及說明
這篇文章主要介紹了關(guān)于PreparedStatement的setObject作用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
java中的BitSet使用實(shí)戰(zhàn)實(shí)例
Java中的BitSet類是一個(gè)高效處理位操作的工具類,用于表示可動(dòng)態(tài)擴(kuò)展的位向量(bit vector),本文給大家介紹java中的BitSet使用實(shí)戰(zhàn)實(shí)例,感興趣的朋友一起看看吧2025-09-09
Java進(jìn)階之在Word文檔中動(dòng)態(tài)增刪頁(yè)面的完整指南
本文將介紹如何使用一款第三方 Java 庫(kù),以編程方式實(shí)現(xiàn)對(duì) Word 文檔頁(yè)面及段落內(nèi)容的精細(xì)化管理,文章將從環(huán)境配置開始,逐步演示如何新增頁(yè)面,在指定位置插入頁(yè)面以及刪除特定頁(yè)面,希望對(duì)大家有一定的幫助2026-05-05
Java基礎(chǔ)學(xué)習(xí)之字符緩沖流的應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Java基礎(chǔ)中的字符緩沖流的相關(guān)應(yīng)用,例如復(fù)制Java文件等,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一2022-09-09
SpringBoot自定義starter啟動(dòng)器的實(shí)現(xiàn)思路
這篇文章主要介紹了SpringBoot如何自定義starter啟動(dòng)器,通過starter的自定義過程,能夠加深大家對(duì)SpringBoot自動(dòng)配置原理的理解,需要的朋友可以參考下2022-10-10
java實(shí)現(xiàn)最短路徑算法之Dijkstra算法
這篇文章主要介紹了java實(shí)現(xiàn)最短路徑算法之Dijkstra算法, Dijkstra算法是最短路徑算法中為人熟知的一種,是單起點(diǎn)全路徑算法,有興趣的可以了解一下2017-10-10

