降低java中的图像分辨率

发布于 2024-11-16 08:58:36 字数 114 浏览 2 评论 0原文

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

我需要将大小从 1MB 减少到大约 50kb - 100 kb。 当然,分辨率会降低,但这并不重要。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

古镇旧梦 2024-11-23 08:58:36

根据这篇博客文章: http://i-proving.com/ 2006/07/06/java-advanced-imaging/ 您可以使用Java 高级成像库 来完成您想要的操作。以下示例代码应该为您提供一个良好的起点。这将调整图像的高度和宽度以及图像质量。一旦图像达到所需的文件大小,您可以在显示图像时将其缩放回所需的像素高度和宽度。

// read in the original image from an input stream
SeekableStream s = SeekableStream.wrapInputStream(
  inputStream, true);
RenderedOp image = JAI.create("stream", s);
((OpImage)image.getRendering()).setTileCache(null);

// now resize the image

float scale = newWidth / image.getWidth();

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


// lastly, write the newly-resized image to an
// output stream, in a specific encoding

JAI.create("encode", resizedImage, outputStream, "PNG", null);

According to this blog post: http://i-proving.com/2006/07/06/java-advanced-imaging/ you can use the Java Advanced Imaging Library to do what you want. The following sample code should provide you a good starting point. This will resize the image, both in height and width, as well as image quality. Once your image is of the desired file size, you can scale it back up to your desired pixel height and width when you display the image.

// read in the original image from an input stream
SeekableStream s = SeekableStream.wrapInputStream(
  inputStream, true);
RenderedOp image = JAI.create("stream", s);
((OpImage)image.getRendering()).setTileCache(null);

// now resize the image

float scale = newWidth / image.getWidth();

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


// lastly, write the newly-resized image to an
// output stream, in a specific encoding

JAI.create("encode", resizedImage, outputStream, "PNG", null);
梦年海沫深 2024-11-23 08:58:36

这是工作代码

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 is the working code

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();
    }
}

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);

你是我的挚爱i 2024-11-23 08:58:36

您可以使用 JAI 来提高写入 JPEG 的压缩率,而无需进行任何缩放。请参阅 http://java.sun。 com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.html

You can use JAI to increase the compression of a written JPEG without doing any scaling. See http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.html

白云悠悠 2024-11-23 08:58:36

如果 的实现支持您的图像类型ImageWriteParam,您可以调整质量,如示例。其他 ImageWriteParam方法,例如 getBitRate(),可以让您优化结果。

If your image type is supported by an implementation of ImageWriteParam, you can adjust the quality as shown in this example. Other ImageWriteParam methods, such as getBitRate(), may allow you to optimize the result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文