编程方式降低JPEG文件大小文件大小、方式、JPEG

2023-09-07 12:52:39 作者:人言可畏

道歉任何偏见,但我从来没有与JPEG图像(更不用说任何类型的图像)在Java中了。

Apologies for any ignorance, but I have never worked with jpeg images (let alone any types of images) in Java before.

假如我想从一个Web服务发送一个JPEG图像到客户端。有没有什么办法,我可以通过操纵以某种方式图像的颜色配置文件缩小JPEG文件的大小?

Supposing I want to send a jpeg image from a web service to a client. Is there any way that I can reduce the jpeg file size by manipulating the colour profile of the image in some way?

我已经能够通过使用一个整洁的工具,对于BufferedImage名为 imgscalr 缩放图像使其缩小图像尺寸。请参见这里。

I have already been able to reduce the image size by scaling it using a neat tool for BufferedImages called imgscalr. See here.

我也想了JPEG,有颜色比高质量JPEG图像更少。例如,我想能够使用8bit的色彩在我的JPEG,而不是说,16位色。

I would also like a jpeg that has less colours than a high quality jpeg image. For example, I would like to be able to use 8bit colour in my jpeg instead of say 16bit colour.

究竟,我需要改变,如果我有Java的2D封装的BufferedImage?

What exactly would I need to change if I have a BufferedImage from Java's 2D package?

推荐答案

另一种方式来减少图像尺寸是改变COM pression水平。你可以做到这一点使用的ImageWriter。

Another way to reduce image size is to change compression level. You can do that using ImageWriter.

    ImageWriter writer = null;
    Iterator<ImageWriter> iwi = ImageIO.getImageWritersByFormatName("jpg");
    if (!iwi.hasNext())
        return;
    writer = (ImageWriter) iwi.next();
    ImageWriteParam iwp = writer.getDefaultWriteParam();
    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ;
    iwp.setCompressionQuality(compressionQuality);
    writer.setOutput(...);
    writer.write(null, image, iwp);