降低java中的图像分辨率 [英] decrease image resolution in java

查看:33
本文介绍了降低java中的图像分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 Java 程序减小图像的大小(不是宽度和高度).他们有什么好的 API 可用于此吗?

I need to reduce the size of image(not the width and height) using Java program. Is their any good API available for this?

我需要将大小从 1MB 减少到大约 50kb - 100 kb.-当然分辨率会降低,但没关系.

I need to reduce the size from 1MB to about 50kb - 100 kb's. Of-course the resolution will decrease but that doesn't matter.

推荐答案

这是工作代码

public class ImageCompressor {
    public void compress() throws IOException {
        File infile = new File("Y:\\img\\star.jpg");
        File outfile = new File("Y:\\img\\star_compressed.jpg");

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                infile));
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(outfile));

        SeekableStream s = SeekableStream.wrapInputStream(bis, true);

        RenderedOp image = JAI.create("stream", s);
        ((OpImage) image.getRendering()).setTileCache(null);

        RenderingHints qualityHints = new RenderingHints(
                RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);

        RenderedOp resizedImage = JAI.create("SubsampleAverage", image, 0.9,
                0.9, qualityHints);

        JAI.create("encode", resizedImage, bos, "JPEG", null);

    }

    public static void main(String[] args) throws IOException {

        new ImageCompressor().compress();
    }
}

这段代码对我来说很好用.如果您需要调整图像大小,那么您可以在这里更改 x 和 y 比例 JAI.create("SubsampleAverage", image, xscale,yscale, qualityHints);

This code is Working Great for me. if you need to resize the image then you can change the x and y scale here JAI.create("SubsampleAverage", image, xscale,yscale, qualityHints);

这篇关于降低java中的图像分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆