有一个非常简单的 .jpg 图像,我想将其转换为矩阵。但是使用 getRGB(i,j) 指向像素会给出 ArrayIndexOutOfBounds 的运行时异常。以下代码对图像大小有限制吗?
它只是显示整个图像中获得的第一个颜色代码。
BufferedImage img=ImageIO.read(new File("stars.jpg"));
int pix[][]= new int[img.getHeight()][img.getWidth()];
然后我应用了一个嵌套的 for 循环并使用
pix[i][j]= img.getRGB(i,j);
这个功能完全符合你提到的功能,对我来说效果很好。我相信您可能在嵌套 for 循环中做错了什么。
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)