【代码片】图像处理工具OpenCV、JAI、ImageJ、Thumbnailator和Graphics2D

Graphics2D图像压缩

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class ImageResolutionModifier {
    public static void main(String[] args) {
        // 输入图片路径和输出图片路径
        String inputImagePath = "D:\\out1.png";
        String outputImagePath = "D:\\out1-graphics2d.jpg";
        // 目标宽度和高度
        int targetWidth = 1024;
        int targetHeight = 576;
        try {
            // 读取输入图片
            File inputFile = new File(inputImagePath);
            BufferedImage inputImage = ImageIO.read(inputFile);

            // 创建缩放后的图像
            BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, inputImage.getType());
            Graphics2D g2d = outputImage.createGraphics();
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            g2d.drawImage(inputImage, 0, 0, targetWidth, targetHeight, null);
            g2d.dispose();

            // 写入输出图片文件
            ImageIO.write(outputImage, "JPEG", new File(outputImagePath));
            System.out.println("图片分辨率修改成功,并保存到 " + outputImagePath);
        } catch (Exception e) {
            System.out.println("图片分辨率修改失败: " + e.getMessage());
        }
    }
}

Thumbnailator图像压缩

import net.coobird.thumbnailator.Thumbnails;
import java.io.File;

public class ImageResolutionModifier {
    public static void main(String[] args) {
        // 输入图片路径和输出图片路径
        String inputImagePath = "D:\\out1.png";
        String outputImagePath = "D:\\out1-thumbnailator.jpg";
        // 目标宽度和高度
        int targetWidth = 1024;
        int targetHeight = 576;
        try {
            Thumbnails.of(new File(inputImagePath))
                    .size(targetWidth, targetHeight)
                    .toFile(new File(outputImagePath));

            System.out.println("图片分辨率修改成功,并保存到 " + outputImagePath);
        } catch (Exception e) {
            System.out.println("图片分辨率修改失败: " + e.getMessage());
        }
    }
}

ImageJ图像压缩

import ij.ImagePlus;
import ij.io.FileSaver;
import ij.io.Opener;
import ij.process.ImageProcessor;

public class ImageResolutionModifier {
    public static void main(String[] args) {
        // 输入图片路径和输出图片路径
        String inputImagePath = "D:\\out1.png";
        String outputImagePath = "D:\\out1-imagej.jpg";
        // 目标宽度和高度
        int targetWidth = 1024;
        int targetHeight = 576;
        try {
            // 打开输入图片
            Opener opener = new Opener();
            ImagePlus imp = opener.openImage(inputImagePath);
            ImageProcessor ip = imp.getProcessor();

            // 缩放图片
            ip = ip.resize(targetWidth, targetHeight);

            // 创建输出图片
            ImagePlus outputImage = new ImagePlus("Output Image", ip);

            // 保存调整分辨率后的图片到输出文件
            FileSaver fileSaver = new FileSaver(outputImage);
            fileSaver.saveAsJpeg(outputImagePath);

            System.out.println("图片分辨率修改成功,并保存到 " + outputImagePath);
        } catch (Exception e) {
            System.out.println("图片分辨率修改失败: " + e.getMessage());
        }
    }
}

JAI图像压缩

import javax.media.jai.Interpolation;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import javax.media.jai.operator.ScaleDescriptor;

public class ImageResolutionModifier {
    public static void main(String[] args) {
        // 输入图片路径和输出图片路径
        String inputImagePath = "D:\\out1.png";
        String outputImagePath = "D:\\out1-jai.jpg";
        // 目标宽度和高度
        int targetWidth = 1024;
        int targetHeight = 576;
        try {
            // 读取输入图片
            RenderedOp inputImage = JAI.create("fileload", inputImagePath);
            // 缩放图片
            RenderedOp scaledImage = ScaleDescriptor.create(inputImage, targetWidth / (float) inputImage.getWidth(),
                    targetHeight / (float) inputImage.getHeight(), 0.0F, 0.0F, 
                    Interpolation.getInstance(Interpolation.INTERP_BICUBIC), null);

            // 保存调整分辨率后的图片到输出文件
            JAI.create("filestore", scaledImage, outputImagePath, "JPEG");
            System.out.println("图片分辨率修改成功,并保存到 " + outputImagePath);
        } catch (Exception e) {
            System.out.println("图片分辨率修改失败: " + e.getMessage());
        }
    }
}

OpenCV图像压缩

import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class ImageResolutionModifier {
    public static void main(String[] args) {
        // 加载 OpenCV 库
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        // 输入图片路径和输出图片路径
        String inputImagePath = "D:\\out1.png";
        String outputImagePath = "D:\\out1-opencv.jpg";
        // 目标宽度和高度
        int targetWidth = 1024;
        int targetHeight = 576;
        try {
            // 读取输入图片
            Mat inputImage = Imgcodecs.imread(inputImagePath);

            // 创建目标尺寸的图像
            Mat resizedImage = new Mat();
            Size size = new Size(targetWidth, targetHeight);
            Imgproc.resize(inputImage, resizedImage, size, 0, 0, Imgproc.INTER_AREA);

            // 保存调整尺寸后的图像
            Imgcodecs.imwrite(outputImagePath, resizedImage);
            System.out.println("图片分辨率修改成功,并保存到 " + outputImagePath);
        } catch (Exception e) {
            System.out.println("图片分辨率修改失败: " + e.getMessage());
        }
    }
}

参考文章:选择最佳图像处理工具OpenCV、JAI、ImageJ、Thumbnailator和Graphics2D
Powered By niaonao

  • 11
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

niaonao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值