在现代应用程序开发中,图片格式转换是一个常见需求。无论是将用户上传的图片转换为标准格式,还是为不同设备优化图片,我们都需要可靠的图片处理工具。Java提供了ImageIO类,它是处理图片I/O操作的标准API,能够满足大多数基本的图片转换需求。
本文将详细介绍如何使用ImageIO进行图片格式转换,包括基础用法、高级技巧以及实际应用示例。
一、ImageIO简介
javax.imageio.ImageIO是Java标准库的一部分,自Java 1.4版本起就包含在JDK中。它提供了一系列静态方法,用于读取和写入各种常见图片格式。
支持的格式
ImageIO支持多种常见的图片格式,包括但不限于:
-
读取支持:JPEG、PNG、GIF、BMP、WBMP
-
写入支持:JPEG、PNG、GIF、BMP、WBMP
要查看当前JVM支持的所有读写格式,可以使用以下代码:
// 获取支持的读取格式
String[] readFormats = ImageIO.getReaderFormatNames();
System.out.println("支持的读取格式: " + Arrays.toString(readFormats));
// 获取支持的写入格式
String[] writeFormats = ImageIO.getWriterFormatNames();
System.out.println("支持的写入格式: " + Arrays.toString(writeFormats));
二、基础图片转换
最基本的图片转换是从一种格式读取图片,然后以另一种格式保存。下面是一个简单的示例,将JPEG图片转换为PNG格式:
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class BasicImageConverter {
public static void convertImageFormat(String inputPath, String outputPath, String outputFormat) {
try {
// 读取输入图片
File inputFile = new File(inputPath);
BufferedImage image = ImageIO.read(inputFile);
if (image == null) {
System.err.println("无法读取图片,可能格式不支持或文件损坏: " + inputPath);
return;
}
// 写入输出图片
File outputFile = new File(outputPath);
boolean result = ImageIO.write(image, outputFormat, outputFile);
if (result) {
System.out.println("图片转换成功: " + outputPath);
} else {
System.err.println("图片转换失败,可能不支持该输出格式: " + outputFormat);
}
} catch (IOException e) {
System.err.println("转换过程中发生IO异常: " + e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args) {
// 示例:将JPEG转换为PNG
convertImageFormat("input.jpg", "output.png", "png");
}
}
三、特殊格式转换技巧
1、特殊格式转换
如:特殊格式:HEIC、DNG、webp、bmp、tiff、avif,上传时出现特殊格式的时候系统需要自动将这些格式转化为JPG、png的图片格式。
package com.hxl.kms.util;
import com.hxl.kms.config.KmsException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Files;
public class ImageToJpgConverter {
public static void main(String[] args) {
File input = new File("avif.avif"); // 也可以是 .tiff, .webp
BufferedImage image;
try {
image = ImageIO.read(Files.newInputStream(input.toPath()));
if (image == null) {
System.err.println("❌ ImageIO.read() 返回了 null,可能是因为文件格式不受支持,或文件已损坏");
throw new KmsException("无法读取图片,可能格式不支持");
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image, "png", outputStream);
FileOutputStream fos = new FileOutputStream("avif.png");
outputStream.writeTo(fos);
} catch (Exception e) {
throw new KmsException("图片格式不正确,文件转换失败");
}
}
}
2. 处理透明通道
PNG支持透明通道,而JPEG不支持。在转换包含透明区域的PNG到JPEG时,透明区域会被填充为白色或其他颜色。
如果需要保留透明效果,应选择PNG或GIF格式。如果必须转换为JPEG,可以考虑将透明区域替换为特定背景色。
四、特殊格式maven依赖
ImageIO是 Java 标准库的一部分,自 Java 1.4 起就包含在 JDK 中,因此 通常情况下不需要额外的 Maven 依赖 即可使用。
也就是说,你可以在项目中直接使用 javax.imageio.ImageIO类及其相关功能,而无需在 pom.xml中添加特定的依赖项。特殊格式需要单独引入依赖包。示例:
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-core</artifactId>
<version>3.10.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-tiff</artifactId>
<version>3.10.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-webp</artifactId>
<version>3.10.1</version>
</dependency>
1417

被折叠的 条评论
为什么被折叠?



