0

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(); } 
2

1 Answer 1

2

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(); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thx that was easier than I thought :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.