Blackberry - how to resize image?

后端 未结 8 1976
栀梦
栀梦 2020-11-30 05:11

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条回答
  • 2020-11-30 05:57

    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.

    0 讨论(0)
  • 2020-11-30 06:01

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

    0 讨论(0)
提交回复
热议问题