java如何将int [] []转换为BufferedImage

问题描述 投票:1回答:2

我有一个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”?

谢谢!

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;
}
java bufferedimage
2个回答
3
投票

所有像素看起来都是黑色的,具有不同的alpha值。您必须使用TYPE_INT_ARGB才能丢失Alpha通道。


0
投票

如果你使用TYPE_INT_RGB,你可以这样做:

BufferedImage.getRaster().setPixels(xCord, YCord, Width, Height, IntArray);
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.