how to use a byte array to create image

J

jimgardener

i have a byte array that i contains image data.Can someone tell me how
i can use it to create a TIFF image with JAI api? I created a
bufferedimage etc as below..but i don't know how to use my byte array
to create the image

BufferedImage result = mycreateImage(width, height);
WritableRaster raster = result.getRaster();

where
private static BufferedImage mycreateImage(int width, int height) {
int colorSpace = ColorSpace.CS_sRGB;
ColorSpace cs = ColorSpace.getInstance(colorSpace);
boolean hasAlpha = false;
boolean isAlphaPremultiplied = true;
int transparency = java.awt.Transparency.OPAQUE;
int dataType = java.awt.image.DataBuffer.TYPE_USHORT;
java.awt.image.ColorModel cm = new
java.awt.image.ComponentColorModel(
cs,
hasAlpha,
isAlphaPremultiplied,
transparency,
dataType
);

int numBands = 3;
java.awt.image.SampleModel sm = new
java.awt.image.BandedSampleModel(
dataType,
width,
height,
numBands
);

java.awt.Point location = null;
java.awt.image.WritableRaster raster =
java.awt.image.Raster.createWritableRaster(sm, location);

java.util.Hashtable properties = null;
BufferedImage result = new BufferedImage(
cm,
raster,
isAlphaPremultiplied,
properties
);

return result;
}
 
T

Tom Anderson

i have a byte array that i contains image data.Can someone tell me how i
can use it to create a TIFF image with JAI api? I created a
bufferedimage etc as below..but i don't know how to use my byte array to
create the image

You don't actually need to create a BufferedImage. Instead, put your byte
array inside a ByteArrayInputStream. Feed the stream to the appropriate
decoder from javax.imageio or JAI.

tom
 
K

Knute Johnson

jimgardener said:
i have a byte array that i contains image data.Can someone tell me how
i can use it to create a TIFF image with JAI api? I created a
bufferedimage etc as below..but i don't know how to use my byte array
to create the image

BufferedImage result = mycreateImage(width, height);
WritableRaster raster = result.getRaster();

where
private static BufferedImage mycreateImage(int width, int height) {
int colorSpace = ColorSpace.CS_sRGB;
ColorSpace cs = ColorSpace.getInstance(colorSpace);
boolean hasAlpha = false;
boolean isAlphaPremultiplied = true;
int transparency = java.awt.Transparency.OPAQUE;
int dataType = java.awt.image.DataBuffer.TYPE_USHORT;
java.awt.image.ColorModel cm = new
java.awt.image.ComponentColorModel(
cs,
hasAlpha,
isAlphaPremultiplied,
transparency,
dataType
);

int numBands = 3;
java.awt.image.SampleModel sm = new
java.awt.image.BandedSampleModel(
dataType,
width,
height,
numBands
);

java.awt.Point location = null;
java.awt.image.WritableRaster raster =
java.awt.image.Raster.createWritableRaster(sm, location);

java.util.Hashtable properties = null;
BufferedImage result = new BufferedImage(
cm,
raster,
isAlphaPremultiplied,
properties
);

return result;
}

If what you have is truly 'image data' in the Java sense then creating
the image from a byte array is done by converting the bytes to an int[]
and setting the data of the BufferedImage with setRGB(). From there use
ImageIO to write out your TIFF file.

If the 'image data' is an array of bytes read from some file then I
would have to know what sort of file to tell you how to proceed.
 
Q

Qu0ll

Knute Johnson said:
jimgardener wrote:
[...]

If what you have is truly 'image data' in the Java sense then creating the
image from a byte array is done by converting the bytes to an int[] and
setting the data of the BufferedImage with setRGB(). From there use
ImageIO to write out your TIFF file.

If the 'image data' is an array of bytes read from some file then I would
have to know what sort of file to tell you how to proceed.

I too am interested in this. I have some JPEG files from which I need to
create images suitable for transmission across a network and I note that
BufferedImage does not implement Serializable. I can read the contents from
the file as a byte array and transmit that array but how do I turn the array
into some form of image to be displayed on a screen at the other end?

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
A

Andrew Thompson

...I can read the contents from
the file as a byte array and transmit that array but how do I turn the array
into some form of image to be displayed on a screen at the other end?

Toolkit.createImage(byte[])
 
Q

Qu0ll

Andrew Thompson said:
...I can read the contents from
the file as a byte array and transmit that array but how do I turn the
array
into some form of image to be displayed on a screen at the other end?

Toolkit.createImage(byte[])

Excellent, thank you.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top