LazyWeb: How to create an Image from a byte array using JAI

Dear Web,

I have the following problem: I need to adapt some code that reads an image from a file using JAI to read it from an array of bytes having exactly the same content as the file. The code for reading from a file is quite simple:

RenderedOp img = JAI.create("fileload", "filename.jpg");

My problem is that I don’t have the JPEG file on disk, but it’s in memory inside a byte[] and I guess that I should do something along the lines of:

RenderedOp img = JAI.create("WHOKNOWS", data);

Or:

RenderedOp img = JAI.create("WHOKNOWS", new ByteArrayInputStream(data));

I couldn’t find anything googling around, the JAI documentation is a mess and the FAQ is of no help.

Please help! What operation name should I use instead of “WHOKNOWS”, assuming what I’m trying to do is even possible?

Please note that the byte array does not contain a raster, but a JPEG-encoded image.

7 Responses to “LazyWeb: How to create an Image from a byte array using JAI”


  1. 1 Davanum Srinivas

    Search in google for ByteArraySeekableStream. Here’s an example:
    http://www.mdsplus.org/cgi-bin/MDSplusCVS/javascope/FrameJAI.java?rev=1.13

  2. 2 Aurélien

    Maybe “stream”?

  3. 3 ugo

    Thanks Davanum, that did the trick!

  4. 4 Drizzt

    Could you please add an example on this site. Davanum’s link is broken.

  5. 5 Prinx

    plase, can you post an example of Davanum’s site…
    Davanum’s link is broken.
    Please

  6. 6 Kamal

    I m having same problem ..

  7. 7 ray

    Just thought I’d post another option if you had it as an InputStream instead.
    I think a similar thing would work for ByteArraySeekableStream as well…
    SeekableStream seekableStream = SeekableStream.wrapInputStream(stream,false);
    PlanarImage planarImage = JAI.create(”Stream”, seekableStream);

Leave a Reply