我正在将一个HTML5的Canvas示例移植到Java中,目前进展顺利,直到遇到这个函数调用:
Canvas.getContext('2d').getImageData(0, 0, 100, 100).data
我谷歌搜索了一会儿,找到了这个canvas规范页面:
阅读后,我创建了下面的函数:
public int[] getImageDataPort(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
int[] ret = new int[width * height * 4];
int idx = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int color = image.getRGB(x, y);
ret[idx++] = getRed(color);
ret[idx++] = getGreen(color);
ret[idx++] = getBlue(color);
ret[idx++] = getAlpha(color);
}
}
return ret;
}
public int getRed(int color) {
return (color >> 16) & 0xFF;
}
public int getGreen(int color) {
return (color >> 8) & 0xFF;
}
public int getBlue(int color) {
return (color >> 0) & 0xFF;
}
public int getAlpha(int color) {
return (color >> 24) & 0xff;
}
Java Graphics API 中是否有内置此功能的任何类,还是我应该使用我已创建的类?