PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 18, 2022

[FIXED] How to convert a BufferedImage to gif in java

 July 18, 2022     encoding, gif, java     No comments   

Issue

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

Solution

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


Answered By - Erwin Bolwidt
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing