0

I'm trying to convert a gif to a jpeg using imageIO but the resulting image is pink... Anyone can help?

public byte[] convert(byte[] bytes)
throws Exception {
    ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
    BufferedImage bufferedImage = ImageIO.read(inputStream); 
    ByteArrayOutputStream osByteArray = new ByteArrayOutputStream();
    ImageOutputStream outputStream = ImageIO.createImageOutputStream(osByteArray);
    ImageIO.write(bufferedImage, "jpg", outputStream);
    outputStream.flush();
    outputStream.close();
    return osByteArray.toByteArray();
}
FranckJS
  • 53
  • 1
  • 2
  • 6
  • Also related: http://stackoverflow.com/questions/4386446/problem-using-imageio-write-jpg-file – finnw Jan 19 '11 at 01:29

1 Answers1

2

Perhaps, pink is defined as the transparency color for the gif image. If so, the following example might work. Basically, a new image is created and the "backgound color" is explicitly set to whatever is passed in.

public static byte[] convert(byte[] bytes, Color backgroundColor) throws Exception
{
    ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
    BufferedImage bufferedImage = ImageIO.read(inputStream);
    BufferedImage newBi = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = (Graphics2D) newBi.getGraphics();
    g2d.drawImage(bufferedImage, 0, 0, bufferedImage.getWidth(), bufferedImage.getHeight(), backgroundColor, null);
    bufferedImage.getHeight(), null);
    ByteArrayOutputStream osByteArray = new ByteArrayOutputStream();
    ImageOutputStream outputStream = ImageIO.createImageOutputStream(osByteArray);
    ImageIO.write(newBi, "jpg", outputStream);
    outputStream.flush();
    outputStream.close();
    return osByteArray.toByteArray();
}

Looks like this might be related.

Community
  • 1
  • 1
jt.
  • 7,625
  • 4
  • 27
  • 24