从原始数据的字节数组中获取缓冲图像 [英] Get buffered image from byte array of raw data

查看:161
本文介绍了从原始数据的字节数组中获取缓冲图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JNA。我从我的c ++方法获取原始数据的字节数组。
现在我被困在如何使用这个原始数据字节数组在java中获取缓冲图像。
我曾尝试过很少的东西来把它作为tiff图像,但我得到了成功。
这是我到目前为止尝试的代码。
这里我的字节数组包含16位灰度图像的数据。我从x传感器设备获得这些数据。现在我需要从这个字节数组中获取图像。

I am using JNA. and i am getting byte array of raw data from my c++ method. Now i am stuck how to get buffered image in java using this raw data byte array. I had tried few things to get it as tiff image but i dint get success. this are the code i tried so far. here my byte array contains data for 16 bit gray scale image. i get this data from x-sensor device. and now i need to get image from this byte array.

首先尝试

byte[] byteArray = myVar1.getByteArray(0, 3318000);//array of raw data

          ImageInputStream stream1=ImageIO.createImageInputStream(newByteArrayInputStream(byteArray));
            ByteArraySeekableStream stream=new ByteArraySeekableStream(byteArray,0,3318000);
                 BufferedImage bi = ImageIO.read(stream);

SECOND TRY

        SeekableStream stream = new ByteArraySeekableStream(byteArray);
         String[] names = ImageCodec.getDecoderNames(stream);


          ImageDecoder dec = ImageCodec.createImageDecoder(names[0], stream, null);
//at this line get the error ArrayIndexOutOfBoundsException: 0 
            RenderedImage im = dec.decodeAsRenderedImage();

我想我在这里失踪了。
由于我的数组包含原始数据,因此它不包含tiff图像的标题。
我对吗?
如果是,则如何在字节数组中提供此标头。并最终如何从这个字节数组中获取图像?

I think i am missing here. As my array is containing raw data ,it does not containthen header for tiff image. m i right? if yes then how to provide this header in byte array. and eventually how to get image from this byte array?

测试我从我的本机方法得到正确的字节数组我将这个字节数组存储为.raw文件并在之后在ImageJ软件中打开这个原始文件,它播放正确的图像,所以我的原始数据是正确的。
我唯一需要的是如何在图像字节数组中转换原始字节数组?

to test that i am getting proper byte array from my native method i stored this byte array as .raw file and after opening this raw file in ImageJ software it sows me correct image so my raw data is correct. The only thing i need is that how to convert my raw byte array in image byte array?

推荐答案

以下是我用来将原始像素数据转换为 BufferedImage 。我的像素是16位签名:

Here is what I am using to convert raw pixel data to a BufferedImage. My pixels are signed 16-bit:

public static BufferedImage short2Buffered(short[] pixels, int width, int height) throws IllegalArgumentException {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_GRAY);
    short[] imgData = ((DataBufferShort)image.getRaster().getDataBuffer()).getData();
    System.arraycopy(pixels, 0, imgData, 0, pixels.length);     
    return image;
}

然后我使用JAI对生成的图像进行编码。告诉我你是否还需要这些代码。

I'm then using JAI to encode the resulting image. Tell me if you need the code as well.

编辑:由于@Brent Nash,我已经大大提高了速度回答类似的问题。

I have greatly improved the speed thanks to @Brent Nash answer on a similar question.

编辑:为了完整起见,这里是无符号8位的代码:

For the sake of completeness, here is the code for unsigned 8 bits:

public static BufferedImage byte2Buffered(byte[] pixels, int width, int height) throws IllegalArgumentException {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    byte[] imgData = ((DataBufferByte)image.getRaster().getDataBuffer()).getData();
    System.arraycopy(pixels, 0, imgData, 0, pixels.length);     
    return image;
}

这篇关于从原始数据的字节数组中获取缓冲图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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