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
Developer
Posts: 43
Registered: ‎09-28-2009
My Device: Not Specified

Thumbnails work around.

Hi All,

 

I will use this as hopefully my introduction to this wonderfull forum from where I have learned so much and can hopefully start giving something back to all of you.

 

So a problem I was always faced with is generating thumbnails. Something a few "blackberry" applications do seamlessly, yet we as pleb developers have never been giving the oppurtunity (or api) to be able to efficiently create thumbnails. The work around that is provided is that we Scale the image ourselves. Now for anyone that cares about memory and cpu cycles (and lets face it, we are mobile developers, so ALL of us) scaling is a costly and time consuming procedure, this is because we need to primarily access the original file (assign memory) then rescale it creating the new image (cpu cycle and more memory) and only now can we clean up the original memory however the cpu cycles have been waisted.  The other option, resize the images included in your application before releasing the project, however this does not allow for any new image rescaling or dynamic rescaling. Also what happens with different screen sizes, now ofcourse we need to do scaling for each of every kind, and this headache only continues...

 

So, the solution, after much playing around with the above solutions I stumbled across an interesting hidden file in ANY folder that contains images, namely "BBThumbs.dat". After some more investigation and a HexEditor I found that BBThumbs.dat contains PNG bitmap information, interestingly enough it contains a PNG Bitmap for each image in the folder and each of these Bitmaps is conveniently scaled to Thumbnail size.

 

So the next step was figuring out how to read this PNG hex format, easily enough we have an identifier .PNG or as you see in the extract below Hex 89 50 4E 47 followed by a image name, the Header in IHDR the body and lastly the END always followed by HEX AE 42 50 82.

 

Hex ANSI

 

89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52
00 00 00 01 00 00 00 01 08 02 00 00 00 90 77 53
DE 00 00 00 0E 49 44 41 54 78 DA 62 F8 CF C0 00
10 60 00 03 01 01 00 66 FD 9F 24 00 00 00 00 49
45 4E 44 AE 42 60 82

 

Text ANSI

 

.PNG........IHDR
..............wS
Þ....IDATxÚbøÏÀ.
.`.....fý.$....I
END®B`.

 

 

So now I knew where to find the file, I knew how to read the file, and I knew how to see the bitmap, so the next step was the code, the following I provide as a Proof of Concept and feel free to use or ammend as you wish.

 

 

//@byte[] search refers to BBThumbs.dat file already opened previously and indexed for faster

//lookups

//@String szSearch refers to the image we want the thumbnail of, so if picture01.jpg is in the //folder with the current BBThumbs.dat then that will be my search String

//Also now with BB 5.0 we have random file access which will make this method of lookup even

//faster for single file thumbnail creation

public static byte[] readThumbs(byte[] search, String szSearch) {

//convert the search string to bytes for easier comparison

byte[] searchtmp = szSearch.getBytes();
for (int x = 0; x < search.length; x++) {
boolean found = false;
int lastbyte = 0;

  //For the length of searchtmp trying to find a match in the byte file

//we could also have converted search to String [new String(search)]

//and have done an index of however I prefer direct byte access as

//lookups tend to be faster

for (int y = 0; y < searchtmp.length; y++) {
if (search[x+y] == searchtmp[y]) {
lastbyte = x+y+1;
found = true;
} else {
found = false;
break;
}
}

  if (found) {

  //we found our search string so next we want to see how long in

//bytes the files is so we only read untill end of this PNG

//without needing to search for the AE 42 50 82 Hex String,

//also there might be a chance that AE 42 50 82 repeats itself

//as such it is highly recommended to get the size

byte[] tmpB = new byte[4];
tmpB[0] = search[lastbyte+20];
tmpB[1] = search[lastbyte+19];
tmpB[2] = search[lastbyte+18];
tmpB[3] = search[lastbyte+17];
long readsize = 0;
int t = 0;

  //convert and retrieve the size

for (int shiftBy=0; shiftBy<32; shiftBy+=8) {
readsize |= (long)(tmpB[t++] & 0xff) <<shiftBy;
}
tmpB = null;
tmpB = new byte[(int)readsize];
t = 0;

  //now we read from the start of the image untill the end of

//the image

for (int y = lastbyte+21; y < (int)readsize+lastbyte+21; y++) {
tmpB[t++] = search[y];
}

  //and like that we have our bitmap

//now all that is left to do is to convert it using

//Bitmap.createBitmapFromBytes(tmpB, x, y, z);

return tmpB;

}
}
return null;
}

 

And now everyone can have a quick and easy thumbnail method with the blackberry doing all the work for us.

 

Hope you guys found this helpfull, I have a few other "tricks" up my sleeve that I will release as I have more time.

 

Best Wishes to All

 

Andre

 

 

Please use plain text.
Developer
packiaraj
Posts: 231
Registered: ‎07-09-2009
My Device: Not Specified

Re: Thumbnails work around.

Great work Andre...

 

I have few question:

 

                1. How can i call this method and how can i use this..

                2. Have you tried to generate thumbnail for video. If yes, pls give some sample code its very  helpful for me

 

 

Thanks,

Packiaraj,

Please use plain text.
New Contributor
glennrp
Posts: 2
Registered: ‎02-20-2010
My Device: none
My Carrier: none

Re: Thumbnails work around.

Good.  For additional confidence you could check the first 16 bytes

 

89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52

 (the 8-byte PNG signature, length 13, "IHDR")

 

and the final 12 bytes

00 00 00 00 49 45 4E 44 AE 42 60 82

(length 0, "IEND", IEND crc)

 

since they are always the same in all valid PNG datastreams.

 

../glennrp

Please use plain text.
Developer
hades_6
Posts: 178
Registered: ‎08-26-2009
My Device: Not Specified

Re: Thumbnails work around.

Nice find!

Please use plain text.
New Developer
anilgoyalp
Posts: 7
Registered: ‎09-22-2008
My Device: Not Specified

Re: Thumbnails work around.

Hi All,

 

I have also same question, that how to create Thumnails for Video files.

 

Thanks,

 

Please use plain text.
New Developer
david_schwartz_bb
Posts: 137
Registered: ‎02-04-2010
My Device: Tour 9630
My Carrier: verizon

Re: Thumbnails work around.

How can I generate a thumbnail from a byte[] - not from an image file?

I store images in data store & want to create a thumbnail directly from the byte[] data.

thanks

Please use plain text.
Contributor
ratheesh
Posts: 26
Registered: ‎07-04-2011
My Device: curve 9300
My Carrier: airtel

Re: Thumbnails work around.

Hi Any one give me a code for creating thumbnail in blackberry ...........

 

 i founded some code that ios not working fine ,,,,,plse give me a correct code   or give some idea to how to do ..

 

 

 

Thanks mn advance ... ratheesh

Please use plain text.
New Developer
Farid123
Posts: 160
Registered: ‎03-02-2011
My Device: Not Specified

Re: Thumbnails work around.

I am trying your way to get images because I am encoutering many performance problems in that topic. But where to find the BBThumbs.dat file? I searched everywhere for this file and could not find it even in the folders containing images. This is a hidden file so it can be accessed using FileConnection? Please reply

Please use plain text.