gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:42:18 29 4
gpt4 key购买 nike

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

 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

谢谢!

最佳答案

你为什么这么做

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

而不仅仅是

int pixel=matrix[i][j];

关于Java Bufferedimage setRgb getRgb,2个不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19030564/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com