我有一个二维整数数组,它是从 BufferedImage 方法“getRGB()”中获得的。当我尝试将二维整数数组转换回 BufferdImage 时,我只得到一张黑色图片。
这种方法
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”?
谢谢!
更新
返回整数矩阵的方法
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;
}