简体   繁体   中英

Compress png with Java

i want compress png with graphicsmagick or ImageMagick using im4java , anyone can help me about it ? i found

 package example;
import com.googlecode.pngtastic.core.PngImage;
import com.googlecode.pngtastic.core.PngOptimizer;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * Example pngtastic image optimization usage from java
 */
public class Test {

    public static void main(String[] args) throws Exception {
        // load png image from a file
        final InputStream in = new BufferedInputStream(new FileInputStream("/input.png"));
        final PngImage image = new PngImage(in);

        // optimize
        final PngOptimizer optimizer = new PngOptimizer();
        final PngImage optimizedImage = optimizer.optimize(image);

        // export the optimized image to a new file
        final ByteArrayOutputStream optimizedBytes = new ByteArrayOutputStream();
        optimizedImage.writeDataOutputStream(optimizedBytes);
        optimizedImage.export("/output.png", optimizedBytes.toByteArray());
    }
}

using pngtastic but it very small image before compress 198.3kB and after compress 179.6kB

i found it pngnq on linux , you can install it and using Runtime.getRuntime().exec("pngnq <file png>"); in java

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