Blackberry - how to resize image?

2019-01-07 14:02发布

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

8条回答
甜甜的少女心
2楼-- · 2019-01-07 14:30

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.

查看更多
\"骚年 ilove
3楼-- · 2019-01-07 14:35
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;
}
查看更多
The star\"
4楼-- · 2019-01-07 14:38

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

查看更多
女痞
5楼-- · 2019-01-07 14:41

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.

查看更多
够拽才男人
6楼-- · 2019-01-07 14:48

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

查看更多
\"骚年 ilove
7楼-- · 2019-01-07 14:54

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

查看更多
登录 后发表回答