int数组到BufferedImage [英] int array to BufferedImage

查看:148
本文介绍了int数组到BufferedImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Robot类制作打印屏幕,并将BufferedImage转换为一个int数组.然后,我想将int数组转换回bufferedimage,但这给出了一个错误.这是我的代码:

I'm making with the Robot class a printscreen and I convert the BufferedImage into an int array. Then I want to convert the int array back to a bufferedimage but that gives an error. This is my code:

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但是为什么?

But I get the error: ArrayIndexOutOfBoundsException: 2073600 but why?

我在此行遇到异常:

raster.setPixels(0, 0, screen.width, screen.height, pixels);

如果我将第二个缓冲图像类型更改为TYPE_BYTE_GRAY,则可以正常工作.

It is working if I change the second bufferedimage type to TYPE_BYTE_GRAY.

推荐答案

更改为:

getRaster().getPixels(0, 0, screen.width, screen.height, pixels)

它有效!仍然感谢您的帮助

and it works! Thanks for help anyway

这篇关于int数组到BufferedImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆