2016-02-26 117 views
2

我正在读取.jpg文件作为整数数组(源)并试图从相同数据生成新图像,但代码正在生成黑色图像。但它应该产生重复的图像作为源。无法从源输入生成相同的输出图像

 String srcName = "input.jpg"; 
     File srcFile = new File(srcName); 
     BufferedImage image = ImageIO.read(srcFile); 
     System.out.println("Source image: " + srcName); 


     int w = image.getWidth(); 
     int h = image.getHeight(); 
     int[] src = image.getRGB(0, 0, w, h, null, 0, w); 

     System.out.println("Array size is " + src.length); 


     BufferedImage dstImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 
     // generating destination image with same source array 
     dstImage.setRGB(0, 0, w, h, src, 0, w); 

     String dstName = "output.jpg"; 
     File dstFile = new File(dstName); 
     ImageIO.write(dstImage, "jpg", dstFile); 
     System.out.println("Output image: " + dstName); 

回答

1

您需要对两个图像使用相同的颜色编码类型。 您的输入图像可能不会编码为BufferedImage.TYPE_INT_ARGB

这个固定为我的测试图像,其中有类型BufferedImage.TYPE_3BYTE_BGR

BufferedImage dstImage = new BufferedImage(w, h, image.getType()); 

不过,我不希望新写的图像是完全一样的输入。我宁愿期望ImageIO在将图像数据编码为jpg时引入一些工件。

+0

似乎是正确的修复。 – PyThon

+0

不客气。快乐编码:) – lupz