简体   繁体   中英

Compress image size using Java

I want to compress(reduce) image size using Java. We can upload images in jpg/jpeg/png formats. The general format of images is PNG. So, after image uploaded to the server, we need to compress(reduce file size) and convert it to PNG.

I have the next code for the compress image:

    BufferedImage bufferedImage = ImageIO.read(inputStream);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    switch (imageType) {
        case PNG:
        case JPG:
            // need Java 9+ for PNG writer support
            ImageWriter writer = ImageIO.getImageWritersByFormatName(imageType.getExtension()).next();
            ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream);
            writer.setOutput(ios);

            ImageWriteParam param = writer.getDefaultWriteParam();
            if (param.canWriteCompressed()) {
                param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                param.setCompressionQuality(0.3f);
            }

            writer.write(null, new IIOImage(bufferedImage, null, null), param);
            writer.dispose();
            return new ByteArrayInputStream(outputStream.toByteArray());
        default:
            log.warn("Image type unknown");
            return null;
    }

The problem is - after processing the image, I got the result - file size increased instead of reducing. The original image has a lower size than compressed. Any suggestions on how to solve this issue?

As a solution for this problem I can recommend the API of TinyPNG.
You can use it for compressing as well as resizing the image.

It works for both .jpeg and .png.

Documentation: tinypng.com/developers/reference/java

Unfortunately the lossy JPEG compression compresses far better than the lossless PNG compression. You could restrict width and height of the image and scale proportionally.

So I would switch to JPEG and restrict the size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM