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: 95
Registered: ‎07-24-2013
My Device: 9380
My Carrier: Airtel
Accepted Solution

Need help in resizing image

Hi all,

   I have downloaded an image from a URL and i want to display it on my BB screen.  I want to get the image resized before displaying on the screen. I have written code but it is cropped horizontally and vertically.  How can i get the image resized preserving its quality? I want the new Width of my image as like (screen width -20) / 3    and height as something like original image height / 3 .  I am posting my code here

 

public static Bitmap resizeImage(Bitmap originalImage, int newWidth, int newHeight) {
	    Bitmap newImage = new Bitmap(newWidth, newHeight);
	    originalImage.scaleInto(newImage, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FILL);
	    return newImage;
	}

 

 

 

Thanks and Warm Regards.

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

Re: Need help in resizing image

If you enlarging the image, you can't expect to retain the quality.

 

If you are reducing the image, then you need to think carefully about the aspect ratio - do you need to keep the same proportion of height and width to retain the look of the Bitmap?

 

When scaling you also need to look carefully at the various Bitmap.SCALE... options to make sure you are using the correct one.

 

If you need further input, please supply the width and height of the image you are receiving, and the width and height that you are scaling to, plus a full description of the problem that you are seeing.

Developer
Posts: 95
Registered: ‎07-24-2013
My Device: 9380
My Carrier: Airtel

Re: Need help in resizing image

Thanks Peter for reply.

 

   I want to shorten the image. Size of the image i am gettiing is 198*295.  I want the width to be equal to (Screen Width-20)/3  and height to be equal to (image height / 3).  I am attaching two images named original and result.  As names explain.. original is the image i am getting adn result is the image i am getting after conversion.

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

Re: Need help in resizing image

So you are prepared for the distortion that scaling will cause, especially on a landscape format screen? 

 

On a 320 by 240 screen, your image will become 100 x 97.  I am assuming by image height you are referring to the original height?

Developer
Posts: 95
Registered: ‎07-24-2013
My Device: 9380
My Carrier: Airtel

Re: Need help in resizing image

Replacing Bitmap.SCALE_TO_FILL with Bitmap.SCALE_STRETCH solved the problem.

 

Thanks.

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

Re: Need help in resizing image

Excellent!  If the problem is solved, then can you mark it as such - see Help link near top of screen if you are not familiar with this,

Developer
Posts: 151
Registered: ‎09-28-2012
My Device: Blackberry Developer
My Carrier: Aircel

Re: Need help in resizing image

you can try this...

 

public static EncodedImage resizeImage(EncodedImage image, int newWidth, int newHeight) {
   int scaleFactorX = Fixed32.div(Fixed32.toFP(image.getWidth()), Fixed32.toFP(newWidth));
   int scaleFactorY = Fixed32.div(Fixed32.toFP(image.getHeight()), Fixed32.toFP(newHeight));
   return image.scaleImage32(scaleFactorX, scaleFactorY);
}


----------------------------------------------------------
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.
Highlighted
Developer
Posts: 95
Registered: ‎07-24-2013
My Device: 9380
My Carrier: Airtel

Re: Need help in resizing image

Thanks for reply Ankur !!!