磁盘上的映像文件大小(KB)随着java中动态映像大小调整(缩小大小)而增加

时间:2023-02-09 15:52:07

I'm new to Java. I resized the large sized png files to standard file size by maintaining the aspect ratio. Below is the code snippet that i used. But the downside i see in this code snippet is that, after re-size the file size is much larger than the original. For e.g original is 5kb and the re-sized new one is 16kb.

我是Java的新手。我通过保持纵横比将大尺寸的png文件调整为标准文件大小。以下是我使用的代码段。但是我在这段代码中看到的缺点是,在重新调整大小之后,文件大小远远大于原始大小。例如,原始的是5kb,重新调整大小的是16kb。

Kindly help me in avoid this file size getting larger upon resize

请帮助我避免在调整大小时此文件大小变大

 public void SaveImage(String imagePath, BufferedImage image) {
        try {
            BufferedImage bi = image;
            File newImageFile = new File(imagePath);
            ImageIO.write(bi, "png", newImageFile);
        } catch (IOException e) {

        }
    }

    public BufferedImage resizeImage(final Image image, Dimension newDimension) {
        final BufferedImage bufferedImage = new BufferedImage(newDimension.width, newDimension.height, BufferedImage.TYPE_INT_RGB);
        final Graphics2D graphics2D = bufferedImage.createGraphics();
        //graphics2D.setComposite(AlphaComposite.Src);

        graphics2D.drawImage(image, 0, 0, newDimension.width, newDimension.height, null);
        graphics2D.dispose();

        return bufferedImage;
    }

1 个解决方案

#1


0  

PNG compression works something like this: it uses a predictor to predict a pixel value from previous values and 'zip' compresses the resulting delta changes. The more low frequency content is present in an image, the better PNG can compress it.

PNG压缩的工作原理如下:它使用预测器来预测先前值的像素值,'zip'压缩得到的delta变化。图像中存在的低频内容越多,PNG就越能压缩它。

Resizing will change the content and might prevent the PNG predictors from detecting long runs which can be compressed better. Shrinking the image will enhance high frequencies which results in higher entropy which results in lesser dompression ratio.

调整大小将改变内容并可能阻止PNG预测器检测可以更好地压缩的长跑。缩小图像将增强高频率,这导致更高的熵,这导致更低的压缩比。

Or it's just caused by the ImageIO writer, which might not set the best compression method for PNG. Have a look at ImageWriter and its ImageWriterParam. ImageWriter can be obtained by ImageIO.getImageWritersByFormatName(String formatName).

或者它只是由ImageIO编写器引起的,它可能无法为PNG设置最佳压缩方法。看看ImageWriter及其ImageWriterParam。 ImageWriter可以通过ImageIO.getImageWritersByFormatName(String formatName)获得。

#1


0  

PNG compression works something like this: it uses a predictor to predict a pixel value from previous values and 'zip' compresses the resulting delta changes. The more low frequency content is present in an image, the better PNG can compress it.

PNG压缩的工作原理如下:它使用预测器来预测先前值的像素值,'zip'压缩得到的delta变化。图像中存在的低频内容越多,PNG就越能压缩它。

Resizing will change the content and might prevent the PNG predictors from detecting long runs which can be compressed better. Shrinking the image will enhance high frequencies which results in higher entropy which results in lesser dompression ratio.

调整大小将改变内容并可能阻止PNG预测器检测可以更好地压缩的长跑。缩小图像将增强高频率,这导致更高的熵,这导致更低的压缩比。

Or it's just caused by the ImageIO writer, which might not set the best compression method for PNG. Have a look at ImageWriter and its ImageWriterParam. ImageWriter can be obtained by ImageIO.getImageWritersByFormatName(String formatName).

或者它只是由ImageIO编写器引起的,它可能无法为PNG设置最佳压缩方法。看看ImageWriter及其ImageWriterParam。 ImageWriter可以通过ImageIO.getImageWritersByFormatName(String formatName)获得。