Random thoughts shooting out of volatile mind
Generating thumbnail from a captured image in Blackberry
Blackberry OS v4.6 and later will allow you to directly interact with camera and capture images. Till OS v4.5 for capturing image you needed to invoke the default camera application present in Blackberry using Invoke api. After capturing you may want to display the image as a thumbnail to user so now you will have question how can you do this. Even I had this same question and looked into many forums but didn't get a satisfactory answer later when I saw the Blackberry api reference i found a function by name scaleImage32 under EncodedImage class. But I couldn't make it work properly since i had to convert my Bitmap image into EncodedImage and then use the function and get the Bitmap from this scaled image using getBitmap which is an expensive call requiring lot of processing along with that whatever i get as output of this function was only 4bytes which means there was some problem in my logic. After struggling a lot I looked at the class Bitmap in the JDE refernce i found the createBitmapFromBytes allowed scaling and this function worked perfectly fine. Below is the piece of code which does this work.

FileConnection fc = null;
InputStream is = null;
int length;
byte[] data = null;
Bitmap thumbnail = null;

try {
fc = (FileConnection)Connector.open(filename);
is = fc.openInputStream();
if(fc.exists()) {
length = (int)fc.fileSize();
data = new byte[length];
fc.read(data,0,length);
thumnail = Bitmap.createBitmapFromBytes(data,0,length,20);
}
}
catch(Exception e)
{

}

Prototype of createBitmapFromBytes is as follows
static Bitmap createBitmapFromBytes(byte[] bytes, int offset, int length, int scale)
Here I've just mentioned the logic modify it to suite your needs. Your thumbnail will be returned as Bitmap object which you can use easily in your application.
One more thing I wanted to add here is scale factor to get thumbnail sized image your scale factor should be more than 20. C ya till next time
Posted by: copyninja on Wednesday, 14 October 2009

blog comments powered by Disqus
Fork me on GitHub