Blackberry - how to resize image?

Bohemian picture Bohemian · Nov 20, 2009 · Viewed 13.5k times · Source

I wanted to know if we can resize an image. Suppose if we want to draw an image of 200x200 actual size with a size of 100 x 100 size on our blackberry screen.

Thanks

Answer

Skrud picture Skrud · Feb 15, 2010

You can do this pretty simply using the EncodedImage.scaleImage32() method. You'll need to provide it with the factors by which you want to scale the width and height (as a Fixed32).

Here's some sample code which determines the scale factor for the width and height by dividing the original image size by the desired size, using RIM's Fixed32 class.

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);
}

If you're lucky enough to be developer for OS 5.0, Marc posted a link to the new APIs that are a lot clearer and more versatile than the one I described above. For example:

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;
}

(Naturally you can substitute the filter/scaling options based on your needs.)