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
footose
Posts: 300
Registered: ‎03-12-2009
My Device: Not Specified
Accepted Solution

Resizing a Bitmap using .scaleImage32 instead of .setScale

I have a simple piece of code that I created that takes a string (image name), gets the image, and reszies it and outputs a Bitmap file.

 

 

public static Bitmap scaleBitmap(String imagename) { EncodedImage ei = EncodedImage.getEncodedImageResource(imagename); ei.setScale(2); Bitmap bmp = ei.getBitmap(); return bmp; }

 

The problem is that .setScale is deprecated, and although still works I'd like to make sure I'm using the latest functions so my app works across the board. I'd also like to scale not by half. I can't seem to get .scaleImage32 to work tho.

 

 

 

public static Bitmap scaleBitmap(String imagename) { int numerator = Fixed32.toFP(1); int denominator = Fixed32.toFP(2); int scale = Fixed32.div(numerator, denominator); EncodedImage ei = EncodedImage.getEncodedImageResource(imagename); ei.scaleImage32(scale, scale); Bitmap bmp = ei.getBitmap(); return bmp; }

 

 

Am I using this incorrectly? 

 

I'm testing on the 8830 and the 9000. the results are the same on both, setScale resizes no problem, the scaleImage does not.

 

 

 

Please use plain text.
Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: Resizing a Bitmap using .scaleImage32 instead of .setScale

Try this:

 

private Bitmap getScaledBitmapImage(String imagename) { EncodedImage image = EncodedImage.getEncodedImageResource(imagename); int currentWidthFixed32 = Fixed32.toFP(image.getWidth()); int currentHeightFixed32 = Fixed32.toFP(image.getHeight()); int width = image.getWidth() /2; int height = image.getHeight() /2; int requiredWidthFixed32 = Fixed32.toFP(width); int requiredHeightFixed32 = Fixed32.toFP(height); int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32); int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32); image = image.scaleImage32(scaleXFixed32, scaleYFixed32); return image.getBitmap(); }

 

Regards

Bikas

 

Please use plain text.
Developer
footose
Posts: 300
Registered: ‎03-12-2009
My Device: Not Specified

Re: Resizing a Bitmap using .scaleImage32 instead of .setScale

[ Edited ]

 That worked perfectly.

 

Now what exactly was I doing wrong?

 

So you convert the width to a fixed3d, then divide it by 2 to get half the width, then convert that to a fixed32 and use the scale for that?

 

Also: How would I use your code to scale by say... 3/4's... not 1/2?

Message Edited by footose on 06-10-2009 03:20 PM
Please use plain text.
Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: Resizing a Bitmap using .scaleImage32 instead of .setScale

[ Edited ]

private Bitmap getScaledBitmapImage(String imagename, int ratioX, int ratioY)
{

EncodedImage image = EncodedImage.getEncodedImageResource(imagename);

int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
int currentHeightFixed32 = Fixed32.toFP(image.getHeight());

double ratio = (double)ratioX / (double) ratioY;
double w = (double) image.getWidth() * ratio;
double h = (double)image.getHeight() * ratio;
int width = (int) w;
int height = (int) h;

int requiredWidthFixed32 = Fixed32.toFP(width);
int requiredHeightFixed32 = Fixed32.toFP(height);

int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32);
int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32);

image = image.scaleImage32(scaleXFixed32, scaleYFixed32);

return image.getBitmap();
}


For scaling the image 3/4 use:

    getScaledBitmapImage("imagename", 3, 4)

 

Regards

Bikas

Message Edited by bikas on 06-11-2009 01:35 AM
Message Edited by bikas on 06-11-2009 01:52 AM
Please use plain text.
Developer
footose
Posts: 300
Registered: ‎03-12-2009
My Device: Not Specified

Re: Resizing a Bitmap using .scaleImage32 instead of .setScale

thank you so much.

 

I wonder why they made it so complex to do, when setscale was so 'easy'

Please use plain text.
Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: Resizing a Bitmap using .scaleImage32 instead of .setScale

You are welcome. :smileyhappy:
Please use plain text.
Developer
RLord321
Posts: 343
Registered: ‎02-23-2009
My Device: 8700 | 8310 | BOLD | STORM

Re: Resizing a Bitmap using .scaleImage32 instead of .setScale

These are good posts and code for scaling an image.  My question since you guys have already implemented the code is:

 

Is this code alright for scaling images in a real time loop.. For instance if I wanted to scale an graphic of a Sun from a little dot all the way up to the size of the screen, would using the EncodedImage code to scale be useful or should this only be used as a one time thing to scale images and not in a major loop?

 

Thanks

Please use plain text.
Developer
cristisor
Posts: 48
Registered: ‎07-15-2009
My Device: Not Specified

Re: Resizing a Bitmap using .scaleImage32 instead of .setScale

Let's suppose that I need to scale the image to 90% of it's original size and the final image width should also divide 7, so I need to find a right scalation factor. I tried like this, but it's not working:

 

int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32); int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32); EncodedImage tempImage; tempImage = image.scaleImage32(scaleXFixed32, scaleYFixed32); while (tempImage.getWidth() % 7 != 0) { scaleXFixed32++; scaleYFixed32++; tempImage = image.scaleImage32(scaleXFixed32, scaleYFixed32); } return tempImage.getBitmap();

 Does anyone have an idea?

 

 

Please use plain text.
New Developer
goodnoodle
Posts: 21
Registered: ‎10-06-2009
My Device: Not Specified

Re: Resizing a Bitmap using .scaleImage32 instead of .setScale

Hi Bikas,

 

Is there any way that you know of to scale images for JDE 4.3?  This code does not work for Blackberry JDE Component Package 4.30.  The image remains unscaled.

 

Thanks.

Please use plain text.
Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: Resizing a Bitmap using .scaleImage32 instead of .setScale

Hi goodnoodle,

 

Try with this:

 

//resize the image Bitmap to given width and height 
public static Bitmap resizeBitmap(Bitmap image, int width, int height)
{
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();

// Need an array (for RGB, with the size of original image)
int rgb[] = new int[imageWidth * imageHeight];

// Get the RGB array of image into "rgb"
image.getARGB(rgb, 0, imageWidth, 0, 0, imageWidth, imageHeight);

// Call to rescaleArray() function and obtain rgb2
int rgb2[] = rescaleArray(rgb, imageWidth, imageHeight, width, height);

// Create an image with that RGB array
Bitmap temp2 = new Bitmap(width, height);

temp2.setARGB(rgb2, 0, width, 0, 0, width, height);

return temp2;
}

private static int[] rescaleArray(int[] ini, int x, int y, int x2, int y2)
{
int out[] = new int[x2*y2];
for (int yy = 0; yy < y2; yy++)
{
int dy = yy * y / y2;
for (int xx = 0; xx < x2; xx++)
{
int dx = xx * x / x2;
out[(x2 * yy) + xx] = ini[(x * dy) + dx];
}
}
return out;
}

 

Regards

Bikas

 

Please use plain text.