Blackberry - how to resize image?
Asked Answered
C

8

8

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

Convent answered 20/11, 2009 at 11:2 Comment(0)
B
7

Just an alternative:
BlackBerry - draw image on the screen
BlackBerry - image 3D transform

Bend answered 20/11, 2009 at 15:59 Comment(0)
B
10

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.)

Bulbar answered 15/2, 2010 at 18:21 Comment(2)
@TreeUK: I have that exact function in working code right now. Are you converting the ints to Fixed32 and using Fixed32.div() to figure out the scale factors? Normal integer division won't cut it.Bulbar
Thanks, was using it incorrectly. Not integer related, but still wrong.Tempa
B
7

Just an alternative:
BlackBerry - draw image on the screen
BlackBerry - image 3D transform

Bend answered 20/11, 2009 at 15:59 Comment(0)
D
3

I'm not a Blackberry programmer, but I believe some of these links will help you out:

Image Resizing Article
Resizing a Bitmap on the Blackberry
Blackberry Image Scaling Question

Delija answered 20/11, 2009 at 11:9 Comment(0)
A
3

Keep in mind that the default image scaling done by BlackBerry is quite primitive and generally doesn't look very good. If you are building for 5.0 there is a new API to do much better image scaling using filters such as bilinear or Lanczos.

Atalee answered 20/11, 2009 at 17:4 Comment(0)
E
2

For BlackBerry JDE 5.0 or later, you can use the scaleInto API.

Excel answered 3/12, 2010 at 21:30 Comment(0)
T
1
in this there is two bitmap.temp is holding the old bitmap.In this method you just pass
bitmap ,width,height.it return new bitmap of your choice.

  Bitmap ImgResizer(Bitmap bitmap , int width , int height){
    Bitmap temp=new Bitmap(width,height);
    Bitmap resized_Bitmap = bitmap;
    temp.createAlpha(Bitmap.HOURGLASS);
    resized_Bitmap.scaleInto(temp , Bitmap.FILTER_LANCZOS);
    return temp;
}
Tymes answered 20/12, 2011 at 11:8 Comment(3)
just call this method and hold this in to a bitmap..enjoyTymes
scaleInto() for Bitmap is available since OS 5.0.Absentee
@Absentee Yes it has been since BlackBerry API 5.0.0Tymes
I
0

Here is the function or you can say method to resize image, use it as you want :

int olddWidth;
int olddHeight;
int dispplayWidth;
int dispplayHeight;

EncodedImage ei2 = EncodedImage.getEncodedImageResource("add2.png");
olddWidth = ei2.getWidth();
olddHeight = ei2.getHeight();
dispplayWidth = 40;\\here pass the width u want in pixels
dispplayHeight = 80;\\here pass the height u want in pixels again

int numeerator = net.rim.device.api.math.Fixed32.toFP(olddWidth);
int denoominator = net.rim.device.api.math.Fixed32.toFP(dispplayWidth);
int widtthScale = net.rim.device.api.math.Fixed32.div(numeerator, denoominator);
numeerator = net.rim.device.api.math.Fixed32.toFP(olddHeight);
denoominator = net.rim.device.api.math.Fixed32.toFP(dispplayHeight);
int heighhtScale = net.rim.device.api.math.Fixed32.div(numeerator, denoominator);
EncodedImage newEi2 = ei2.scaleImage32(widtthScale, heighhtScale); 
Bitmap _add =newEi2.getBitmap();
Intervocalic answered 7/12, 2011 at 8:7 Comment(0)
M
0

I am posting this answers for newbie in Blackberry Application development. Below code is for processing Bitmap images from URL and Resizing them without loass of Aspect Ratio :

   public static Bitmap imageFromServer(String url)
{
Bitmap bitmp = null;
try{
HttpConnection fcon = (HttpConnection)Connector.open(url);
int rc = fcon.getResponseCode();
if(rc!=HttpConnection.HTTP_OK)
{
    throw new IOException("Http Response Code : " + rc);            
}
InputStream httpInput = fcon.openDataInputStream();
InputStream inp = httpInput;
byte[] b = IOUtilities.streamToBytes(inp);
EncodedImage img = EncodedImage.createEncodedImage(b, 0, b.length);
bitmp = resizeImage(img.getBitmap(), 100, 100);
}
catch(Exception e)
{
Dialog.alert("Exception : " + e.getMessage());          
}
return bitmp;
}

public static Bitmap resizeImage(Bitmap originalImg, int newWidth, int newHeight)
{
    Bitmap scaledImage = new Bitmap(newWidth, newHeight);
    originalImg.scaleInto(scaledImage, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FIT);
    return scaledImage;
}

The Method resizeImage is called inside the method imageFromServer(String url). 1) the image from server is processed using EncodedImage img. 2) Bitmap bitmp = resizeImage(img.getBitmap(), 100, 100); parameters are passed to resizeImage() and the return value from resizeImage() is set to Bitmap bitmp.

Monostich answered 6/1, 2013 at 14:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.