This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes MemoryImageSource and byte arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "MemoryImageSource and byte arrays" Watch "MemoryImageSource and byte arrays" New topic
Author

MemoryImageSource and byte arrays

Eric Hoskland
Greenhorn

Joined: Oct 12, 2000
Posts: 29
I can't for the life of me figure out a way to provide MemoryImageSource with a byte array and it gives me back an image.
My byte array stores values like this:
{R0,G0,B0,R1,G1,B1, etc..}
It has a contructor for a bytearray but I can't figure out Colorspace,ColorModel,SampleModel, Databuffer, ColorSpace, PixelInterleavedSomethingorOther combination to create an image
Thanks in advance
Eric
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
There is an example in the MemoryImageSource api.
Eric Hoskland
Greenhorn

Joined: Oct 12, 2000
Posts: 29
from the memoryimagesource API
pixels = new int[size];
.....
....
source = new MemoryImageSource(width, height, pixels, 0, width);

reread what I said and you'll realize why this isn't what I want.
reconstructing a byte array into an int array is very inefficient. That's like 160*120*3 operations per image without an alpha channel.
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
160 * 120 * 3 is not many operations. This will probably take less than a millisecond.
D.
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
takes between 0.5 and 0.6 milliseconds to do the conversion :

Can you wait that long?
D.
Eric Hoskland
Greenhorn

Joined: Oct 12, 2000
Posts: 29
With speed considerations aside the solution of bit shifting and or'ing in all the values upon every single reception of a new image is quite messy. The entire purpose of programming in 3rd generation languages is to give into the OO paradigm and relieve ourselves from such tedious non-harmonious solutions. If we're going to call the solution above acceptable then why not just do the whole thing in assembly. Why even try to use the layer of abstraction that makes java so fun. So with that in mind here's the solution to what I asked:

Now all you have to do is refill the byte array b_pixels and display the new buffered image..Works excellent for displaying images coming from an input stream of some kind..just use b_pixels as the array to stream into and display bImage.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MemoryImageSource and byte arrays
 
Similar Threads
image is not beeing drawn
byte[] to file
How to get byte[] from the Image?
byte or int array into BufferedImage
Cropping Image