简体   繁体   中英

How to convert PNG to jpeg in grails/java

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?

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

BufferedImage image = ImageIO.read(pngInputStream));
ImageIO.write(image, "jpeg", jpgOutputStream);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM