我有一个2D整数数组,我可以从BufferedImage方法获取“getRGB()”。 当我尝试将2D整数数组转换回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
为什么每个Pixel都是“-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;
}
答案 0 :(得分:3)
所有像素似乎都是黑色的,具有不同的alpha值。您必须使用TYPE_INT_ARGB才能丢失Alpha通道。
答案 1 :(得分:0)
如果您使用TYPE_INT_RGB
,则可以这样做:
BufferedImage.getRaster().setPixels(xCord, YCord, Width, Height, IntArray);