Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to use a byte array to create image

Reply
Thread Tools

how to use a byte array to create image

 
 
jimgardener
Guest
Posts: n/a
 
      06-25-2008
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;
}
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      06-25-2008
jimgardener <> writes:
>bufferedimage etc as below..but i don't know how to use my byte array
>to create the image


You can set a buffer to an int array.

http://download.java.net/jdk7/docs/a...,%20int,%20int[],%20int,%20int)

 
Reply With Quote
 
 
 
 
Tom Anderson
Guest
Posts: n/a
 
      06-25-2008
On Wed, 25 Jun 2008, jimgardener wrote:

> 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

--
Yesterday's research projects are today's utilities and tomorrow's
historical footnotes. -- Roy Smith
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      06-25-2008
jimgardener wrote:
> 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.

--

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
Reply With Quote
 
Qu0ll
Guest
Posts: n/a
 
      06-26-2008
"Knute Johnson" <> wrote in message
news:4862688e$0$4084$.. .
> 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)
_________________________________________________

[Replace the "SixFour" with numbers to email me]

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      06-26-2008
On Jun 26, 7:49*pm, "Qu0ll" <Qu0llSixF...@gmail.com> wrote:
>...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[])

--
Andrew Thompson
http://pscode.org/
 
Reply With Quote
 
Qu0ll
Guest
Posts: n/a
 
      06-26-2008
"Andrew Thompson" <> wrote in message
news:284d91a0-e758-4192-8742-...
> On Jun 26, 7:49 pm, "Qu0ll" <Qu0llSixF...@gmail.com> wrote:
>>...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)
_________________________________________________

[Replace the "SixFour" with numbers to email me]

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
convert form byte[4] to Int32 while retaining the binary value of the byte array jeff@foundrymusic.com C++ 20 09-07-2009 08:54 PM
Re: how to extend a byte[] array with a null byte? Tom McGlynn Java 4 04-18-2008 11:49 PM
Converting a Primative byte array to a Byte array object Kirby Java 3 10-08-2004 03:01 AM
Appending byte[] to another byte[] array Bharat Bhushan Java 15 08-05-2003 07:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57