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: 80
Registered: ‎05-22-2011
My Device: Blackberry Torch
My Carrier: Airtel

Reducing size of the image

[ Edited ]

Hello everyone,

 

In my current project, I have a requirement to reduce the size of the images which I am getting from web service. If I explain my exact requirement, I have images which are coming from web service having dimensions 480x640 and having size 32KB. I need to alter the dimension to 50x50 and reduce the size to say less than 10KB.

 

The method which I am using currently is pasted below.

public static Bitmap resizeBitmap(EncodedImage image, int height, int width){
	int multH;
	int multW;
	int currHeight = image.getHeight();
	int currWidth = image.getWidth();
	multH = Fixed32.div(Fixed32.toFP(currHeight), Fixed32.toFP(height));
	multW = Fixed32.div(Fixed32.toFP(currWidth), Fixed32.toFP(width));
	image = image.scaleImage32(multW, multH);
	return image.getBitmap();
}

 

 

When I analyzed, I got to know that the above method is altering the dimension to 50x50 as I need but the size is not getting reduced. Can anyone suggest some idea on how to reduce size of an image in BlackBerry?

Developer
Posts: 17,012
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Reducing size of the image

i would suggest to use
http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/system/EncodedImage.html#scale...
----------------------------------------------------------
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
Developer
Posts: 80
Registered: ‎05-22-2011
My Device: Blackberry Torch
My Carrier: Airtel

Re: Reducing size of the image

Hi Simon,

 

Thanks for the response.

 

This is what I am using right now (kindly check the method in my post). The problem  I am facing is, though the image's dimension is getting reduced, its size isn't. Have you come across such scenario? 

Developer
Posts: 17,012
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Reducing size of the image

what size are you actually checking?
note that a bitmap is uncompressed, if you save the bitmap the file might get bigger instead.
----------------------------------------------------------
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
Developer
Posts: 80
Registered: ‎05-22-2011
My Device: Blackberry Torch
My Carrier: Airtel

Re: Reducing size of the image

The image which I am getting from web service is of dimension, 480x640 and of size 34KB. I need to convert the dimension to 50x50 and size to say 5 KB.

The method you suggested is not reducing the image size to what I need. I saved the converted image to SDCard and upon analysing it I could understand that the size has not reduced. Its the same as before.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Reducing size of the image

[ Edited ]

Three things

 

1) For clarity, can you please change the code supplied, replace the last two lines which are currently:

 

image = image.scaleImage32(multW, multH);
return image.getBitmap();

 

to

 

EncodedImage newImage = image.scaleImage32(multW, multH);

return newImage.getBitmap();

 

2) Regarding your question, this method does not return the bytes that are written to the SD Card,  You need to convert the Bitmap in some way before writing it out.  There are a variety of ways this can be done, some will write large files, some smaller.  So how are you converting it?

 

3) Can you tell us the file format of the file you have downloaded?  jpg?  png?  

Developer
Posts: 80
Registered: ‎05-22-2011
My Device: Blackberry Torch
My Carrier: Airtel

Re: Reducing size of the image

[ Edited ]

Hi Peter,

 

Will your changed code make much difference? I was reusing the same variable but you used a new variable for holding the scaled image.

While saving to SDCard, I take the byte array from the scaled Encoded image using EncodedImage.getData() method and then write it to the SDCard location. Any other suggestion?

 

File format is PNG. 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Reducing size of the image

1) "Will your changed code make much difference?"

No.  It just I have seen that code before and it looks like it is updating the input parameter.  I'd rather it did not look like that.  As I mentioned - this is just for clarify. 

 

2)  "I take the byte array from the scaled Encoded image"

The method does not return an EncodedImage, it returns a Bitmap. 

 

3) I think you might have missed my 3rd question - what format is the original image?

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Reducing size of the image

Due to edit timings, the previous post does not directly follow, and I wanted to make sure that the OP spotted this update, so have added a new Post.

 

I suggest you use PGNEncodedImage, rather than just EncodedImage.  Then when you do a getData, you will get a PNG image, which is compressed. 

Highlighted
Developer
Posts: 80
Registered: ‎05-22-2011
My Device: Blackberry Torch
My Carrier: Airtel

Re: Reducing size of the image

1) Okay.
2) image.scaleImage32(multW, multH) method will return an encoded image and I can get the byte array from getData() method. I am saving this byte array to SDCard.
3) When I checked the API doc for PNGEncodedImage, I didn't see getData() implementation in it. Its the same method inherited from its parent class. Are you saying that this method will return a compressed PNG image byte array?