我有一個2D整數數組,這是我從BufferedImage的方法「的getRGB()」獲得。 當我嘗試二維整數數組轉換回BufferdImage,我只得到一個黑色的畫面。java中如何轉換INT [] []將一個的BufferedImage
這種方法
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
int pixel=matrix[i][j];
System.out.println("The pixel in Matrix: "+pixel);
bufferedImage.setRGB(i, j, pixel);
System.out.println("The pixel in BufferedImage: "+bufferedImage.getRGB(i, j));
}
}
給這個輸出:
The pixel in Matrix: 0
The pixel in BufferedImage: -16777216
The pixel in Matrix: 721420288
The pixel in BufferedImage: -16777216
The pixel in Matrix: 738197504
The pixel in BufferedImage: -16777216
The pixel in Matrix: 520093696
The pixel in BufferedImage: -16777216
The pixel in Matrix: 503316480
The pixel in BufferedImage: -16777216
爲什麼每個像素 「-16777216」?
謝謝!
UPDATE
它返回整數矩陣
public int[][] getMatrixOfImage(BufferedImage bufferedImage) {
int width = bufferedImage.getWidth(null);
int height = bufferedImage.getHeight(null);
int[][] pixels = new int[width][height];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
pixels[i][j] = bufferedImage.getRGB(i, j);
}
}
return pixels;
}
這是否幫助? http://stackoverflow.com/questions/5436931/convert-a-2d-array-of-int-ranging-from-0-256-into-a-grayscale-png –
不,結果相同 –
您的格式矩陣值是錯誤的。你從哪裏弄到的?你能顯示代碼嗎? – Dukeling