0

I have a png file and tried to convert it to jpeg. But the resulting image has wrong colors with very big areas of pink. This is my code:

        BufferedImage image = null
        BufferedImage imageRGB = null

        image = ImageIO.read(new ByteArrayInputStream(imageBytesPng))

        imageRGB = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB)

        imageRGB.setData(image.getData())

        ByteArrayOutputStream baos=new ByteArrayOutputStream()

        ImageIO.write(imageRGB, "jpeg", baos)
        baos.flush()
        def outImage = baos.toByteArray()
        baos.close()
        return outImage

What can I change to make the image colors apear as in the png file?

Michael
  • 32,527
  • 49
  • 210
  • 370
  • Upload the image to an image share site and link. Does it use transparency? Why transform it from PNG to JPG? – Andrew Thompson Dec 22 '12 at 00:06
  • I found a very easy solution.See the following post: http://stackoverflow.com/questions/9555917/java-png-to-jpg-bug – Michael Dec 28 '12 at 17:45

2 Answers2

1
InputStream pngInputStream = ...
OutputStream jpgOutputStream = ...

BufferedImage image = ImageIO.read(pngInputStream));
ImageIO.write(image, "jpeg", jpgOutputStream);
dnault
  • 8,340
  • 1
  • 34
  • 53
0

Try this all the best..

import javax.media.jai.*;
public class jai_png_jpg 
{
   public static void main(String[] args)throws Exception  
   {
  String filename="input_png.png";
  //Read input PNG as a PlanarImage file
  PlanarImage inputfile = JAI.create("fileload", filename); 
  //write output in JPG Format 
  JAI.create("filestore",inputfile,"jai_jpg_output.jpg","JPEG"); } }