我正在使用一个有方法的 API
public void setImage(byte[][] newImage)
API 参考中给出的唯一信息是
“该图像的格式是简单的原始2D array
字节数。每个字节代表灰度black (0)
to white (255)
。当建立索引时2D array
, Y axis
首先,X axis
其次,例如图像[y][x]。
参数值不会在内部克隆。为了避免不必要的共享,请在调用此方法之前克隆图像数据。”
我应该将 .gif 图像传递给此方法,但无法弄清楚如何将 .gif 转换为byte[][] array
.
任何帮助都会很棒!谢谢。
有多种方法可以将颜色转换为灰度,但最简单的方法是平均红色、绿色和蓝色值,如下所示:
BufferedImage image = ImageIO.read(new File([your gif]));
int[][] imageBytes = new int[image.getHeight()][image.getWidth()];
for (int y = 0; y < image.getHeight(); y++)
{
for (int x = 0; x < image.getWidth(); x++)
{
Color color = new Color(image.getRGB(x, y));
imageBytes[y][x] = (color.getRed() + color.getGreen() + color.getBlue()) / 3;
}
}
请注意,我用过int
数组,因为(正如我之前评论的)声明数组的 API 存在一个根本问题setImage
方法。 Abyte
Java 中的 是有符号的,因此表示最小值为 -128,最大值为 127,因此不能用于值 0 - 255。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)