写点什么

图像处理 -Java- 指定大小压缩

作者:alexgaoyh
  • 2024-01-25
    河南
  • 本文字数:1248 字

    阅读完需:约 4 分钟

背景

  《图像处理-Java-指定大小压缩》,之前在编写过关于图像边缘检测-去黑边图像边缘检测-自动纠偏图像处理-锐化图像处理-去噪/高斯模糊/套红图像处理-背景色平滑/反色等一系列文章,接下来主要介绍图像压缩,将图像压缩至指定大小。

概述

  使用 JAVA 语言实现,将给定的 JPEG 图像文件调整到指定的目标文件大小,使用循环,不断地调整图像质量,然后保存图像并检查文件大小是否达到目标大小。

代码实现

package com.pap.base.util.image;
import javax.imageio.ImageIO;import javax.imageio.ImageWriteParam;import javax.imageio.ImageWriter;import javax.imageio.stream.FileImageOutputStream;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.Iterator;
public class JPGCompressTest {
// 输入图像绝对路径、输出图像绝对路径、期望的图像大小 public static void convertJpg(String inputImagePath, String outputImagePath, double targetSizeBytes) throws IOException { File inputFile = new File(inputImagePath); File outputFile = new File(outputImagePath);
long currentSize = inputFile.length(); float quality = 0.5f;
while (currentSize > targetSizeBytes && quality > 0.1f) { BufferedImage bufferedImage = ImageIO.read(inputFile); writeJpgWithQuality(bufferedImage, outputFile, quality);
currentSize = outputFile.length(); quality -= 0.005f; }
while (currentSize < targetSizeBytes) { BufferedImage bufferedImage = ImageIO.read(inputFile); writeJpgWithQuality(bufferedImage, outputFile, quality);
currentSize = outputFile.length(); quality += 0.005f; }
}
private static void writeJpgWithQuality(BufferedImage image, File outputFile, float quality) throws IOException { Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg"); if (writers.hasNext()) { ImageWriter writer = writers.next(); ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(quality);
try (FileImageOutputStream output = new FileImageOutputStream(outputFile)) { writer.setOutput(output); writer.write(null, new javax.imageio.IIOImage(image, null, null), param); } finally { writer.dispose(); } } }}
复制代码

参考

  1. http://pap-docs.pap.net.cn/

  2. https://gitee.com/alexgaoyh


发布于: 刚刚阅读数: 3
用户头像

alexgaoyh

关注

DevOps 2013-12-08 加入

https://gitee.com/alexgaoyh

评论

发布
暂无评论
图像处理-Java-指定大小压缩_Java_alexgaoyh_InfoQ写作社区