2016-07-11 157 views
0

我有一个rgb值的一维整数数组,我需要为BufferedImage提供数据,然后使用它来生成一个jpeg。我有128个* 128像素,这我踏过使用setrgb(我试图通过缓冲图像阵列中的一个去,遇到了麻烦和好,这是一个单独的问题)BufferedImage setRGB:图像被压缩到四分之一大小

 int numRows = 128; 
     int numCols = 128; 
     int inputIndex = 0; 
     BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
     for(int row = 0; row < numRows; row++){ 
      for(int col = 0; col < numCols; col ++){ 
       int rgb = rgbvals[inputIndex++]; 
       rgb = (rgb << 8) + rgbvals[inputIndex++]; 
       rgb = (rgb << 8) + rgbvals[inputIndex++]; 
       image.setRGB(col,row, rgb); 
      }    
     } 
     File outputFile = new File("output.jpg"); 
     ImageIO.write(image, "JPEG", outputFile); 

这看起来相当简单,但我的图像看起来像这样: 128*128 picture of an eye, except it takes up 1/4 the space it needs and the rest of the image is black

我觉得我可能忽略了一个小细节。我会感谢任何见解,谢谢!

回答

0

这实际上是一个愚蠢的错误,我只需要正确调用与numRows和numCols的BufferedImage构造函数,而不是我使用的其他变量(宽度,高度指的是别的东西)。

这固定了它。