2016-07-11 163 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構造函數,而不是我使用的其他變量(寬度,高度指的是別的東西)。

這固定了它。