2

I have tried both lines of code below to no avail. The code works fine with jpg, or gif but turns the image pink if a png.

ImageIO.write(input, "jpg", profileFile);

RenderedOp op = JAI.create("filestore", input, pFileName, "jpeg");

Anyone else run into this problem? I haven't been able to find a solution.

Asaph
  • 159,146
  • 25
  • 197
  • 199
Adam Stokar
  • 61
  • 1
  • 2
  • 3
  • 2
    Is the PNG transparent? Does it have an alpha channel? – Sam Barnum Dec 02 '09 at 03:23
  • It is not transparent. I am not sure about the alpha channel. This code is used when a user uploads photos on a website. I convert all photos to JPG and would like to let them use PNG if they happen to have that. – Adam Stokar Dec 02 '09 at 12:41

3 Answers3

1

You have duplicated your question. And there is answer about reported bug in Sun's library and workaround and link.

Problem converting PNG to JPG using Java (ImageIO.write())

Community
  • 1
  • 1
0

I'm not sure if this is the right answer or not. But there is another post that suggest that the implementation of JPEG writting with an alpha channel is a bit screwy.

Community
  • 1
  • 1
monksy
  • 14,156
  • 17
  • 75
  • 124
0

I draw PNGs with the following code and do not run into a problem. It combines multiple PNG images into a single image. The images have transparency, and use bilinear transform for blending.

BufferedImage image = new BufferedImage(BOARD_SIZE, BOARD_SIZE, BufferedImage.TYPE_INT_ARGB); 
Graphics2D g2d = image.createGraphics();
AffineTransformOp transformOp = new AffineTransformOp(new AffineTransform(), AffineTransformOp.TYPE_BILINEAR);
g2d.drawImage(someOtherImage, transformOp, 0, 0);

When I finish the image, I write it to a response using the following code:

OutputStream responseStream = response.getOutputStream();
ImageIO.write(image, "PNG", responseStream);
Kaleb Brasee
  • 51,193
  • 8
  • 108
  • 113