如何在Grails / java中将PNG转换为jpeg

时间:2012-12-21 21:23:06

标签: java image grails javax.imageio

我有一个png文件并尝试将其转换为jpeg。但是生成的图像颜色错误,粉红色很大。这是我的代码:

        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

我可以更改什么来使图像颜色像png文件一样?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

尽力而为......

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