如何创建从原始数据的BufferedImage [英] How to create a BufferedImage from raw data

查看:262
本文介绍了如何创建从原始数据的BufferedImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从原料样品一个BufferedImage,但我得到试图读取过去,我只是不明白的可用数据范围的例外。我试图做的是:

I'm trying to get a BufferedImage from raw samples, but I get exceptions about trying to read past the available data range which I just don't understand. What I'm trying to do is:

val datasize = image.width * image.height
val imgbytes = image.data.getIntArray(0, datasize)
val datamodel = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, image.width, image.height, Array(image.red_mask.intValue, image.green_mask.intValue, image.blue_mask.intValue))
val buffer = datamodel.createDataBuffer
val raster = Raster.createRaster(datamodel, buffer, new Point(0,0))
datamodel.setPixels(0, 0, image.width, image.height, imgbytes, buffer)
val newimage = new BufferedImage(image.width, image.height, BufferedImage.TYPE_INT_RGB)
newimage.setData(raster)

不幸的是我得到:

Unfortunately I get:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 32784
    at java.awt.image.SinglePixelPackedSampleModel.setPixels(SinglePixelPackedSampleModel.java:689)
    at screenplayer.Main$.ximage_to_swt(Main.scala:40)
    at screenplayer.Main$.main(Main.scala:31)
    at screenplayer.Main.main(Main.scala)

的数据是用1字节填充标准RGB(使1个像素== 4字节)和图像尺寸1366x24像素。

The data is standard RGB with 1 byte padding (so that 1 pixel == 4 bytes) and the image size is 1366x24 px.

我终于得到了code与下面的建议运行。最后code是:

I finally got the code to run with the suggestion below. The final code is:

val datasize = image.width * image.height
val imgbytes = image.data.getIntArray(0, datasize)

val raster = Raster.createPackedRaster(DataBuffer.TYPE_INT, image.width, image.height, 3, 8, null)
raster.setDataElements(0, 0, image.width, image.height, imgbytes)

val newimage = new BufferedImage(image.width, image.height, BufferedImage.TYPE_INT_RGB)
newimage.setData(raster)

如果它可以改进,我愿意,当然建议,但在总体上按预期工作。

If it can be improved, I'm open to suggestions of course, but in general it works as expected.

推荐答案

的setPixels 假设图像数据的的包装。因此它在寻找长度image.width的输入* image.height * 3和运行关闭阵列的末尾。

setPixels assumes that the image data is not packed. So it's looking for an input of length image.width*image.height*3, and running off the end of the array.

下面三个选项如何解决这个问题。

Here are three options for how to fix the problem.

(1)打开 imgbytes 所以它是3倍长,做上述同样的方式。

(1) Unpack imgbytes so it is 3x longer, and do it the same way as above.

(2)手动加载从缓冲区 imgbytes 而不是使用的setPixels

(2) Manually load the buffer from imgbytes instead of using setPixels:

var i=0
while (i < imgbytes.length) {
  buffer.setElem(i, imgbytes(i))
  i += 1
}

(3)不要使用的createDataBuffer ;如果你已经知道你的数据具有正确的格式,您可以创建自己合适的缓冲液(在这种情况下, DataBufferInt

(3) Don't use createDataBuffer; if you already know that your data has the proper formatting you can create the appropriate buffer yourself (in this case, a DataBufferInt):

val buffer = new DataBufferInt(imgbytes, imgbytes.length)

(您可能需要做 imgbytes.clone 如果你的原件可以通过别的东西得到突变)。

(you may need to do imgbytes.clone if your original copy could get mutated by something else).

这篇关于如何创建从原始数据的BufferedImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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