我正在使用Robot
该类制作一个打印屏幕,并将 BufferedImage 转换为一个 int 数组。然后我想将 int 数组转换回 bufferedimage 但这会出错。这是我的代码:
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
BufferedImage printscreen = robot.createScreenCapture(new Rectangle(screen));
int[] pixels = ((DataBufferInt) printscreen.getRaster().getDataBuffer()).getData();
BufferedImage image = new BufferedImage(screen.width, screen.height, BufferedImage.TYPE_INT_RGB);
WritableRaster raster = (WritableRaster) image.getRaster();
raster.setPixels(0, 0, screen.width, screen.height, pixels);
但我得到了错误:ArrayIndexOutOfBoundsException: 2073600
但为什么?
我在这条线上遇到了异常:
raster.setPixels(0, 0, screen.width, screen.height, pixels);
编辑:如果我将第二个缓冲图像类型更改为 TYPE_BYTE_GRAY,它将起作用。