5

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.

FIRST TRY

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();

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?

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?

1

2 Answers 2

9

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;
}

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

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

EDIT: 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;
}
7
  • it gives me an image but i think byte order of this image is not correct. have u done any byte operation with your image?
    – Jony
    Jun 22, 2012 at 5:06
  • 1
    To be honnest, not yet. I coded this function because I had the same with byte[] and will eventually need the same with short[] one day. However, Java is big-endianed and if you're working with Windows, the stream you're receiving is probably little-endianed and you would have to make yourself the conversion. Check the java.nio.ByteOrder.nativeOrder() to know which one is the native Byte Order (if you want to keep your code cross-platformed).
    – Matthieu
    Nov 24, 2012 at 12:32
  • could you please provide the method with byte array input? Jan 20, 2016 at 11:50
  • 2
    @H.Aqjn just change the DataBufferShort to DataBufferByte and BufferedImage.TYPE_USHORT_GRAY to BufferedImage.TYPE_BYTE_GRAY(and all short[] to byte[]) and you'll be good to go.
    – Matthieu
    Jan 20, 2016 at 14:19
  • 1
    This was exactly what i was looking for. Just changed Short to Byte at respective places as i need it to work with byte[]. Thanks a lot @Matthieu
    – Kishan
    Apr 24, 2018 at 7:35
4

Whether or not the byte array contains literally just pixel data or a structured image file such as TIFF etc really depends on where you got it from. It's impossible to answer that from the information provided.

However, if it does contain a structured image file, then you can generally:

  • wrap a ByteArrayInputStream around it
  • pass that stream to ImageIO.read()

If you just have literally raw pixel data, then you have a couple of main options:

  • 'manually' get that pixel data so that it is in an int array with one int per pixel in ARGB format (the ByteBuffer and IntBuffer classes can help you with twiddling about with bytes)
  • create a blank BufferedImage, then call its setRGB() method to set the actual pixel contents from your previously prepared int array

I think the above is easiest if you know what you're doing with the bits and bytes. However, in principle, you should be able to do the following:

  • find a suitable WritableRaster.create... method method that will create a WritableRaster object wrapped around your data
  • pass that WritableRaster into the relevant BufferedImage constructor to create your image.
1
  • well i am geting my raw byte array from natve method and its data of my x-ray sensor device. n it is containing raw data for 16 bit gray scale image. as i told that storing this array as .raw file and opening this .raw file in imageJ gives me correct x-ray image
    – Jony
    May 23, 2012 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.