0

我有一个 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 文件中的一样?

4

2 回答 2

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

BufferedImage image = ImageIO.read(pngInputStream));
ImageIO.write(image, "jpeg", jpgOutputStream);
于 2012-12-21T22:20:38.770 回答
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"); } }
于 2012-12-21T21:29:18.973 回答