Java Bufferedimage setRgb getRgb,2个不同的结果

时间:2013-09-26 14:07:54

标签: java bufferedimage java-2d

我正在尝试将图像转换为矩阵并将其转换回来,但是这两张图片是不同的: 将其转换为矩阵:

 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;
}

并将其转换回bufferedImage:

   public BufferedImage matrixToBufferedImage(int[][] matrix) {
    int width=matrix[0].length;
    int height=matrix.length;
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);

    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix[0].length; j++) {

            int pixel=matrix[i][j] <<24|matrix[i][j] <<16|matrix[i][j]<<8|matrix[i][j] ;
            bufferedImage.setRGB(i, j, pixel);
        }
    }
    return bufferedImage;

}

有了这个结果:

http://img59.imageshack.us/img59/5464/mt8a.png

谢谢!

1 个答案:

答案 0 :(得分:2)

为什么要这样做

int pixel=matrix[i][j] <<24|matrix[i][j] <<16|matrix[i][j]<<8|matrix[i][j];

而不仅仅是

int pixel=matrix[i][j];