开发者

Whats the best way to lower the bitdepth of a PNG

开发者 https://www.devze.com 2023-02-27 05:41 出处:网络
I have a PNG image in an BufferedImage and I would like to lower the bitdepth in order to make it smaller. Below is a function that saves a small portion of an image to \"disk.\"The writeImage functio

I have a PNG image in an BufferedImage and I would like to lower the bitdepth in order to make it smaller. Below is a function that saves a small portion of an image to "disk." The writeImage function is the function that writes it to disk. Any ideas?

private BufferedImage createSubImage(Avatar avatar, int[] srcRect, Dimension size, BufferedImage image, String name) throws IOException {
    Graphics2D graphics;
    BufferedImage thumb = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
    graphics = (Graphics2D) thumb.getGraphics();

    //lower PNG bitdepth here
    graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    graphics.drawImage(image, 0, 0, size.width, size.height, srcRect[0], srcRect[1],
            srcRect[2], srcRect[3], null);

    writeImage(DataAccess.APP_DATA_BUCKET, thumb, 开发者_C百科"avatars/" + avatar.getId() + "/" + name);
    thumb.flush();
    return thumb;
}


png supports greyscale, indexed or truecolor with 8 or 16 bits per channel. if you reduce the bit depth yourself by rounding each value in all channels to a multiple of 4 for example the resulting png will be smaller because compression will be better. it will use only 6 instead of 8 bits so it should be around 6/8 the size of the original but can be much better (depending on the image) because most of the noisy, hard to compress information is in the 2 removed bits.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号