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,309
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Getting bytes from objects

Quote from the previous forum:


jwhanes

Posts: 46
Registered: Dec, 2007

Getting bytes from objects
Posted: Mar 27, 2008 12:36 PM
 
I think this may have been posted awhile back but there was never a solution. Is there any way to extract a byte array from an object? More specifically an Image object, like a Bitmap or PNG.

I realize there is the getARGB() method that will populate an array of integers with the raw ARGB data of the bitmap. But the problem is if you wanted to POST this data it will not be recognized as a bitmap file.

This is because a Bitmap contains a certain format: it has a header, information, color pallet, and data block. All this information is not contained in the integer array.

You can extract a byte array from an image by doing something like this
EncodedImage image = EncodedImage.getEncodedImageResource("temp.png");
byte[] array = image.getData();

But what if you wanted to get it from a Bitmap object rather then an already existing resource?

Does the format ofthe Bitmap need to be constructed byte by byte?

Thank you in advance for any responses. This problem is driving me insane.

 


jwhanes

Posts: 46
Registered: Dec, 2007

Re: Getting bytes from objects
Posted: Mar 27, 2008 3:56 PM
 
I guess I could use

JPEGEncodedImage jpg = JPEGEncodedImage encode(Bitmap bitmap, int quality)

and then do a jpg.getBytes().
But this is only available on JDE 4.3

Is this the only way of getting a byte array from a Bitmap object?

 

tylerjames

Posts: 16
Registered: Sep, 2007

Re: Getting bytes from objects
Posted: Mar 27, 2008 5:39 PM
 
Hey,

How're you getting the Bitmap object in the first place? Are you reading a file stored on the device?

 

jwhanes

Posts: 46
Registered: Dec, 2007

Re: Getting bytes from objects
Posted: Mar 28, 2008 12:03 PM
 
It's just created like any other object really.

Bitmap bmp = new Bitmap(Graphics.getScreenWidth(),Graphics.getScre enHeight());

I need to get the bmp into a byte array.

 

Guest
Re: Getting bytes from objects
Posted: Jul 23, 2008 3:22 AM
 
This might become a good future reference for people.

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

The Bitmap.getARGB method can be used to obtain a byte array of a Bitmap.
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: Getting bytes from objects

Hi,

 

Can anybody explain how to get byte array from int array data (We got int array data using 'getARGB()' method.)?

 

I have used the code posted in one of the post. But the bitmap is not formed using the data which is retrieved by the functionalit. Please give hint if any one knows. 

 

In JDE 4.3, I have used the oJPEGEncodedImage.getData(). It is working fine. Now I wants to do the same thing in JDE 4.2.

 

Regards,

Shubhangi 

 

 

Please use plain text.
Administrator
MSohm
Posts: 14,309
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: Getting bytes from objects

Can you elaborate on this:

 


I have used the code posted in one of the post. But the bitmap is not formed using the data which is retrieved by the functionalit. Please give hint if any one knows. 

 


 

 

 

What problem are you running in to?

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
knight9
Posts: 84
Registered: ‎09-21-2008
My Device: Not Specified

Re: Getting bytes from objects

Was there a resolution to this post?

 

The issue is that we need to get byte[] data out of a Bitmap object using API prior to 4.3. As mentioned post 4.3 we can useJPEGEncodedImage to generate an encoded image and get the byte data that way. 

 

I think someone is suggesting somehow building the byte array using the getARGB function of the Bitmap object, butthe int[] array returned by that function does not correlate to a byte array of an image and can not be used to create an encoded image as far as I can tell..

 

 

Any help with this issue would be greatly appreciated. 

 

 

Please use plain text.
Administrator
MSohm
Posts: 14,309
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: Getting bytes from objects

There are no methods for this in the BlackBerry API set prior to version 4.3.0.
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: Getting bytes from objects

Check this solution and see if it'll get you going.
Please use plain text.
Developer
shreyasb
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: Getting bytes from objects

[ Edited ]

Hi all,

 

I m saving the image as

 

 

private void saveBitmap(int picIndex, Bitmap bitmap)
{

String PHOTO_DIR = System.getProperty("fileconn.dir.photos");
String EXTENSION = ".bmp";

 

try
{
FileConnection fconn = (FileConnection)Connector.open(PHOTO_DIR + picIndex + EXTENSION, Connector.READ_WRITE);

if(!fconn.exists())
fconn.create();

OutputStream outputStream = fconn.openOutputStream();

PNGEncoder encoder = new PNGEncoder(bitmap, true); //PNGEncoder class created by Richard

byte[] imageBytes = encoder.encode(true);

outputStream.write(imageBytes);
outputStream.close();
fconn.close();
}
catch(Exception e){
System.out.println(" Exception while saving Bitmap:: "+e.toString());
}
}

 

 , the bimap is saved in Media->Pictures->Pictures Folder as "1.bmp".

 

but when clicked on it to view.

 

Unable to create EncodedImage for: /store/home/user/pictures/1.bmp

 error comes on console.

 

 I tried changing EXTENSION = ".png" but no use.

 

 I tried adding...


 

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

 

after outputStream.write(..); but no use

 

Its getting saved but same error "Unable toc reate Encoded image for: ....." error. while viewing it.

 

I m working in Blackberry JDE 4.7.0.41 & testing in Blackberry 9500 & 9530 Simulators. I tested it on Storm 9500 device but not working.

The same above problem is coming.

 

Not getting what may be the problem.  Plz suggest....

 

 

Message Edited by shreyasb on 09-01-2009 10:05 AM
Please use plain text.
Developer
shreyasb
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: Getting bytes from objects

I m saving the image like....

 

 

private void saveBitmap(int picIndex, Bitmap bmp)
{

 

String PHOTO_DIR = System.getProperty ("fileconn.dir.photos");
String EXTENSION = ".bmp";
String filePath = PHOTO_DIR + picIndex + EXTENSION;

 

try
{
FileConnection fconn = (FileConnection)Connector.open(filePath, Connector.READ_WRITE);

if(fconn.exists())

fcomm.delete();

 

fconn.create();

OutputStream outputStream = fconn.openOutputStream();

 

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


outputStream.write(imageBytes);
outputStream.close();
fconn.close();
}
catch(Exception e){
System.out.println(" Exception while saving Bitmap:: "+e.toString());
}
}

 


It is saved in Media->Pictures->Device Memory->1.bmp but when clicked on it error comes as "Unable to create Encoded Image for ..." as I told in my previous post.
I tested this in 9500 Simulator & Storm 9500.

 

Now I accessed that saved image through another project by adding that image in BitmapField & adding it on Screen like...

 

 

private void readBitmap(int picIndex)
{
String PHOTO_DIR = System.getProperty ("fileconn.dir.photos");
String EXTENSION = ".bmp";
String filePath = PHOTO_DIR + picIndex + EXTENSION;

try
{
FileConnection fconn = (FileConnection)Connector.open(filePath, Connector.READ);
if (fconn.exists())
{
InputStream input = fconn.openInputStream();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
int j = 0;
while((j=input.read()) != -1) {
baos.write(j);
}
byte[] byteArray = baos.toByteArray();

Bitmap bitmapImage = Bitmap.createBitmapFromBytes(byteArray,0,byteArray.length,1);

BitmapField bitmapField = new BitmapField(bitmapImage);
hfm.add(bitmapField);
}
else
{
System.out.println("No Image Found");
}
}
catch(Exception e)
{
System.out.println(" Exception while reading Bitmap:: "+e.toString());
}

}

 

 

 

Its coming properly. I tested this in 9500 Simulator & Storm 9500.

Then dont know why its not getting displayed in Media->Pictures->Device Memory->1.bmp.

Do anyone know the answer for this?

Please use plain text.