简体   繁体   中英

How to convert a BufferedImage to gif in java

I want to convert a BufferedImage to gif. I already converted BufferedImage to jpeg with a function and the output was Byte[] so I could send it over the inet with a socket connection.

I need a gif encode function similiar to this jpeg encode function:

public static byte[] getImageAsJPEG(BufferedImage image) throws ImageFormatException, IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
    param.setQuality(0.9f, false);
    encoder.setJPEGEncodeParam(param);
    encoder.encode(image);

    return output.toByteArray();
}

Use:

ImageIO.write(image, "GIF", output);

As in:

public static byte[] getImageAsGIF(BufferedImage image) throws ImageFormatException, IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    ImageIO.write(image, "GIF", output);

    return output.toByteArray();
}

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