gpt4 book ai didi

java - int 数组到 BufferedImage

转载 作者:搜寻专家 更新时间:2023-10-31 08:17:27 24 4
gpt4 key购买 nike

我正在使用 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,它会起作用。

最佳答案

int[] bitMasks = new int[]{0xFF0000, 0xFF00, 0xFF, 0xFF000000};
SinglePixelPackedSampleModel sm = new SinglePixelPackedSampleModel(
DataBuffer.TYPE_INT, width, height, bitMasks);
DataBufferInt db = new DataBufferInt(pixels, pixels.length);
WritableRaster wr = Raster.createWritableRaster(sm, db, new Point());
BufferedImage image = new BufferedImage(ColorModel.getRGBdefault(), wr, false, null);

关于java - int 数组到 BufferedImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14416107/

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