Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Administrator
MSohm
Posts: 14,446
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Bitmap to ByteArray

Quote from the previous forum:


Guest: bsr
Bitmap to ByteArray
Posted: Aug 4, 2006 7:16 AM
Reply
Anyone got any hints or code snippet to help me convert a Bitmap to byte[]? (not the other way round).

 


Guest: bsr
Re: Bitmap to ByteArray
Posted: Aug 7, 2006 5:53 AM
Reply
anyone?

 

Guest
Re: Bitmap to ByteArray
Posted: Aug 9, 2006 2:23 PM
Reply
I do not know exactly what you mean.

Does it mean you want getByte from Bitmap file which is stored at handheld?

 

Guest: bsr
Re: Bitmap to ByteArray
Posted: Aug 9, 2006 6:36 PM
Reply
i have an image, a .png packaged with the application, but i want to convert it to a byte array, i can do this no problem with a remote .png downloaded over http, this is just saved via stream to byte array. but i need to convert a packaged .png to byte array, any ideas?

this is driving me mad, if i could do this it'd make my app so much simpler, local or remote images saved the same way, otherwise i'll have to have a much more complex datastore.

 

Guest: jason glass
Re: Bitmap to ByteArray
Posted: Aug 9, 2006 7:48 PM
Reply
wouldnt there be something in java canvas class?

I was doing this on Nokia, i.e. take an image, load it, copy it, then ATTEMPT to convert the image to a larger size, by increasing the pixels, I used a byte array to store the data before and after

good luck

Jason Glass
Http://IChiBanComputers.Com

 

peterstrange

Posts: 1,481
From: UK
Registered: Apr, 2005

Re: Bitmap to ByteArray
Posted: Aug 9, 2006 9:24 PM
Reply
Probably missing the point here, but do you just want to read a png file that is packaged with the application? If so, then I think the following knowledge Base article tells you how to do it. Sample reads in text, but I'm sure you can easily change it.

How To - Add plain text or binary files to an application
Last Updated: 27 April 2006
Article Number: DB-00148
http://www.blackberry.com/knowledgecenterpublic/l ivelink.exe/fetch/2000/348583/796557/800332/800620 /How_To_-_Add_plain_text_or_binary_files_to_an_app lication.html?nodeid=800687&vernum=0

 

Guest: bsr
Re: Bitmap to ByteArray
Posted: Aug 10, 2006 3:05 AM
Reply
perfect! - yet another gap in my knowledge filled, cheers peter.

 

peterstrange

Posts: 1,481
From: UK
Registered: Apr, 2005

Re: Bitmap to ByteArray
Posted: Aug 10, 2006 5:25 AM
Reply
I would have answered it earlier, but I thought, like some other posters I suspect, that you had a Bitmap and you wanted to convert it back to a png. I was watching with interest, as doing that, I think, would be a challenge!

 

VipulPathak

Posts: 53
From: Columbia, MD
Registered: Apr, 2008

Re: Bitmap to ByteArray
Posted: Jun 17, 2008 9:38 AM
 
I would rather ask a couple of things additionally-

- How to get the contents of a Bitmap in order to send it as an attachment with an email.

Email attachment can be done by setting the email contents as a Multipart. The multipart can contain test and additional files as its parts.

However, Parts in the Multipart can be "SupportedAttachmentPart" or "TextBodyPart", each of which allows a setContent() method.

The real question in front of me is that, the SupportedAttachmentPart's setContent() accepts an object (raw contents of Bitmap) while we have the actual bitmap object, so how can we convert it ?

Any light on this?

Thanks,

*(Vipul)() ;

 

msohm

Posts: 2,299
Registered: Jul, 2005

Re: Bitmap to ByteArray
Posted: Jun 17, 2008 9:58 AM
 
The Bitmap.getARGB method can be used to get the raw contents of a Bitmap. The link below contains a sample on how to create an email attachment.

How To - Create an attachment
DB-00009
http://tinyurl.com/34hmrt

Mark Sohm
BlackBerry Developer Journal
www.blackberrydeveloperjournal.com

 

VipulPathak

Posts: 53
From: Columbia, MD
Registered: Apr, 2008

Re: Bitmap to ByteArray
Posted: Jun 24, 2008 11:03 PM
 
Thanks Mark,

But the getARGB() method would return it in form of an int[]. The method SupportedAttachmentPart.setContent() expects an object (which is I hope a byte[]).

So how do we transform the int[] into a byte[]?

Thanks a bunch in Advance,

*(Vipul)() ;

 

VipulPathak

Posts: 53
From: Columbia, MD
Registered: Apr, 2008

Re: Bitmap to ByteArray
Posted: Jun 30, 2008 8:32 AM
 
For bitmap to get as a byte[], I am still in question. But for now, I am using JPEGEncodedImage.getData().

Anyone, if know how to deal with the getARGB() method who returns the int[], or who knows that how to obtain a byte[] from a bitmap object, please uncover the suspense.

Thanks,

 

Ted Hopp

Posts: 33
From: Gaithersburg, Maryland, USA
Registered: Jul, 2007

Re: Bitmap to ByteArray
Posted: Jun 30, 2008 6:45 PM
 
When you call Bitmap.getARGB, you are retrieving the pixel values for the bitmap. What you need in order to send an email attachment is some encoding (identified by mime type) of those values as a byte stream. Pixel values and encoding are two fundamentally different things. (Like the difference between a character and it's encoding in UTF-8, ISO 8859-1, etc.)

So to convert an int[] into a byte stream, you need to pick the encoding you want and do the conversion. Unfortunately, although the RIM APIs support decoding an image in BMP, JPEG, PNG, and WBMP formats, I don't think there's any support for encoding. You'll have to roll your own if you really need to encode images from pixel values. (This is a big job, but you can find lots of sample code on the internet to help.)

 

Guest
Re: Bitmap to ByteArray
Posted: Jun 30, 2008 7:29 PM
 
I think that you are complicating the problem more than it should be. Starting from a .png image, which is already in bytes. Out of this you create a Bitmap to display, or you do not need to convert it to Bitmap unless your are seeking performance. You can handle everything from reading to displaying using EncodedImage class. In Graphics, you have drawBitmap() and also DrawImage(). In BitmapField, you have setBitmap(), and also setImage.
Given a .png file on the sdcard or on the server or in the resource, you need to load it using an inputStream that will end up reading a byte array:

something like:

InputStream input = Class.forName"xxx").getResourceAsStream(strFullNa me);
int len = input.available();
byte[] data = new byte[len];
input.read(data);
EncodedImage image = EncodedImage.createEncodedImage(data,0,data.lengt h);

So you have a byte array that you can send or use to create an Image etc...

I hope that this can help.

Rab

 

VipulPathak

Posts: 53
From: Columbia, MD
Registered: Apr, 2008

Re: Bitmap to ByteArray
Posted: Jul 21, 2008 10:47 PM
 
This thread clarifies that some thing is not possible using the API. There are alternatives that are being proposed by other developers. This would be a good future reference for people.

Marking to move this thread to the new forums ...

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
richard_puckett
Posts: 191
Registered: ‎04-03-2008
My Device: Bold 9700

Re: Bitmap to ByteArray

[ Edited ]

Ok, folks. I ported a PNGEncoder to BlackBerry and am making it available here. What this will do is convert a Bitmap to a PNG and then return a byte array of the resulting PNG, which should be generally useful since that's pretty much the lingua franca of midp. Source is located at my Mobiyana site.

 

Here's how to use it:

 

 

PNGEncoder encoder = new PNGEncoder(bitmap, true);
byte[] imageBytes = encoder.encode(true);

 

if you then want an EncodedImage do this:

 

 

EncodedImage fullImage = EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);

 

Enjoy!

 

 

Message Edited by richard_puckett on 07-22-2008 02:14 PM
Please use plain text.
Administrator
MSohm
Posts: 14,446
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: Bitmap to ByteArray

This is great Richard, thanks for posting it!
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
Shubhangi
Posts: 97
Registered: ‎07-17-2008
My Device: Not Specified

Re: Bitmap to ByteArray

Hi,

 

I am resizing the bitmap using getARGB method. I want the byte data from resized bitmap?

 

How can I do that? I have tried the way explained in forums but no success.

 

I am not able to generate the bitmap from the data which is converted from int[] to byte[].

 

Can anybody tell how to do it?

 

 

Reagrds,

Shubhangi

Please use plain text.
New Developer
dcorb
Posts: 9
Registered: ‎07-28-2008
My Device: Not Specified

Re: Bitmap to ByteArray

I have used the following but it is is very slow. I am currently working to speed this up.

 

DataBuffer db = new DataBuffer();
db.setBigEndian(false);
                       
width = Display.getWidth();
height = Display.getHeight();

Bitmap testBitmap = new Bitmap(width, height);
Display.screenshot(testBitmap, 0, 0, width, height);

int[] rgbdata = new int[width*height];
testBitmap.getARGB(rgbdata, 0, width, 0, 0, width, height);  // <<<<<<<<< int array

 

for (int i = 0; i < (width * height); i++)                   // <<<<<<<<< move to byte array
{
        db.writeInt(rgbdata[i]);
}

db.rewind();

Please use plain text.
New Developer
dcorb
Posts: 9
Registered: ‎07-28-2008
My Device: Not Specified

Re: Bitmap to ByteArray

Does anyone have any ideas to quickly transfer a integer array to a byte array?

 

I need to transfer the bitmap information across the USB connection. The USBPort class requires a byte array. I also attempted to compress the image but the ZLibOutputStream class also requires a byte array.

Please use plain text.
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Bitmap to ByteArray

I used to work at EyeWonder and wrote a java applet for streaming audio and video to browsers ( mostly for ad delivery

until Flash audio support was better). Anyway, I remember getting annoyed that in some cases the screen updates/manipulations

with the int<->byte conversions  were  on the top of the profiler list which is quite a trick when you have a codec and a lot of other stuff going on.  AFAIK, the brute force shift and mask approach is still quite reasonable. Anyone who tries to serialize an int[] needs an endian

spec etc. I did find a RIM BitSet class and was hoping they had something like a ByteStream that loads from int[] but nothing obvious.

Offhand I wouldn't bet that a per-int call to DataBuffer would be any faster than your own hard coded shifting logic.

 

Please use plain text.
Developer
sandeepkumar03
Posts: 117
Registered: ‎02-12-2009
My Device: Not Specified

Re: Bitmap to ByteArray

Hi,

 

I used the PNGEncoder in JDE 4.2.1. I wanted to send the Bitmap as email. So i converted Bitmap to Byte Array using PNGEncoder.After composing the Message i am passing the Message object as argument to Invoke Email Client. Now when user clicks on send, i get null pointer exception. One more thing i observed was the size of attachment shown in email client after encoding increases (Actual size was 80kb, After attachment 700+ kb). Also i wrote a small program to call Email Client by hardcoding the attachment by not using PNGEncoder, and i don't get any exception here.

 

Observation on JDE 4.7 - PNGEncoder was working fine. But if i send email, come back again compose Message and invoke email client. If i repeat this. I again get Null Pointer Exception. (The behaviour is inconsistant). Then i used JPEGEncoder, its working fine. But it is available 4.6 onwards.

 

Anyone has faced similar issue? Please provide your opinion.

 

Thanks,

Sandeep

Please use plain text.
Developer
sandeepkumar03
Posts: 117
Registered: ‎02-12-2009
My Device: Not Specified

Re: Bitmap to ByteArray

 Hi All,

 

Gentle reminder. Please help in resolving the issue.

 

Thanks,

Sandeep

 

Please use plain text.
Developer
simon_hain
Posts: 16,044
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Bitmap to ByteArray

you are aware of the PNGEncoder been developed by Richard? If you have a problem with the software you can contact him. Maybe he helps you - on the other hand, it is a free tool and there is no entitlement for support.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.