在 Java 中,你可以使用不同的方式来压缩图片。下面我会介绍三种常用的方式,并提供相应的代码示例和所需的第三方库依赖。
步骤流程:
import java.io.File;
和 import javax.imageio.ImageIO;
ImageIO.read()
方法读取原始图片。BufferedImage
来保存压缩后的图片。ImageIO.write()
方法的参数之一 ImageWriteParam
对象,设置压缩参数,如压缩质量。ImageIO.write()
方法将压缩后的图片保存到文件。示例代码:
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;
public class ImageCompressionExample {
public static void main(String[] args) throws IOException {
File inputFile = new File("input.jpg");
File outputFile = new File("output_compressed.jpg");
BufferedImage originalImage = ImageIO.read(inputFile);
BufferedImage compressedImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_RGB);
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
ImageWriteParam params = writer.getDefaultWriteParam();
params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
params.setCompressionQuality(0.5f); // Adjust the quality (0.0 to 1.0)
FileImageOutputStream output = new FileImageOutputStream(outputFile);
writer.setOutput(output);
writer.write(null, new javax.imageio.IIOImage(compressedImage, null, null), params);
output.close();
}
}
依赖坐标(Maven):
<dependency>
<groupId>javax.imageio</groupId>
<artifactId>javax.imageio-api</artifactId>
<version>1.0.1</version>
</dependency>
步骤流程:
import net.coobird.thumbnailator.Thumbnails;
Thumbnails.of()
方法创建一个 Thumbnails.Builder
对象。toFile()
方法将压缩后的图片保存到文件。示例代码:
import java.io.File;
import java.io.IOException;
import net.coobird.thumbnailator.Thumbnails;
public class ImageCompressionExample {
public static void main(String[] args) throws IOException {
File inputFile = new File("input.jpg");
File outputFile = new File("output_compressed.jpg");
Thumbnails.of(inputFile)
.size(800, 600) // Set the dimensions
.outputQuality(0.5) // Set the quality (0.0 to 1.0)
.toFile(outputFile);
}
}
依赖坐标(Maven):
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.14</version>
</dependency>
依赖坐标(Gradle):
implementation 'net.coobird:thumbnailator:0.4.14'
步骤流程:
import org.imgscalr.Scalr;
Scalr.resize()
方法来调整图片尺寸并压缩。ImageIO.write()
方法将压缩后的图片保存到文件。示例代码:
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.imgscalr.Scalr;
public class ImageCompressionExample {
public static void main(String[] args) throws IOException {
File inputFile = new File("input.jpg");
File outputFile = new File("output_compressed.jpg");
BufferedImage originalImage = ImageIO.read(inputFile);
BufferedImage resizedImage = Scalr.resize(originalImage, Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, 800, 600);
ImageIO.write(resizedImage, "jpg", outputFile);
}
}
依赖坐标(Maven):
<dependency>
<groupId>org.imgscalr</groupId>
<artifactId>imgscalr-lib</artifactId>
<version>4.2</version>
</dependency>
依赖坐标(Gradle):
implementation 'org.imgscalr:imgscalr-lib:4.2'
以上三种方式都是常见的压缩图片的方法,你可以根据你的需求和偏好选择其中一种。在集成这些库时,请根据你的构建工具选择相应的依赖坐标。