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
cagdasgerede
Posts: 241
Registered: ‎11-20-2008
My Device: Not Specified

Scaling a Bitmap (Without EncodedImage)

I am doing image processing on a bitmap and eventually I am displaying the result on the screen. For the second step, I need to scale the bitmap to make it fit on the screen.

 

I searched the forums. The suggested approach for scaling is as follows:

 

EncodedImage image;

BitmapField bitmap_field;

//image and bitmap_field are initialize

//...

 

//now scale

EncodedImage scaled_image = image.scaleImage32(scaleX, scaleY);

bitmap_field.setBitmap(scaled_image.getBitmap());

 

 

This approach sounds good if your starting point is an encoded image. What if you have a bitmap as a start? The one approach that will follow what is recommended would be:

 

//encode the bitmap to an encoded image, and then scale it

EncodedImage image = PNGEncodedImage.encode(bitmap).scaleImage32(scaleX, scaleY);

 

 

However, this approach has 3 disadvantages:

1) I create a copy of the bitmap in encoded image form (ok this is compressed image but still extra memory usage since my bitmap size is pretty big compared to the screen size)

2) I compress and decompress an encoded image which means a lot of time when I am doing this many many times. 

 

 

My question is that is there a way to scale a bitmap in a more effienct way than the approach I described above.

 

Thanks.

 

 

Please use plain text.
Developer
cagdasgerede
Posts: 241
Registered: ‎11-20-2008
My Device: Not Specified

Re: Scaling a Bitmap (Without EncodedImage)

I appreciate if any one help on this.
Please use plain text.
Developer
simon_hain
Posts: 15,863
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Scaling a Bitmap (Without EncodedImage)

as the API provides you no methods you can only implement them yourself. image processing can be of any desired complexity - that's why most people work with stuff others have already provided, like the different encodedimage types.
to scale a bitmap you have to read it in byte format and delete the pixels you want to get rid of - if you spent some time in advanced data structure and alorithm classes: happy coding.

i spent some time there and would still prefer to work with encoded image.
why do you need the bitmap class? pictures included in the project are stored as png internally and the most likely class to display a picture, bitmapfield, can also display encoded image.
even the low-level graphics class has the drawImage method that offers the same parameters as drawBitmap.

in real-life use bitmap is almost non-existential. some people send bitmaps within word-documents as e-mail because they don't know better, but usually you don't have to deal with them anymore.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
cagdasgerede
Posts: 241
Registered: ‎11-20-2008
My Device: Not Specified

Re: Scaling a Bitmap (Without EncodedImage)

In our application, we receive images from the network and then we need to process them before displaying on the screen. Furthermore, the image size is bigger than the phone screen so we need to scale them.

 

Is there a way to scale a bitmapfield during drawing with graphics by setting width and height in some some layout manager? 

 

or do you still recommend me to follow the encodedimage path i described?

Please use plain text.
Developer
simon_hain
Posts: 15,863
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Scaling a Bitmap (Without EncodedImage)

but you don't really receive files in bitmap format, do you?
a 45kb jpg would be a 900kb bitmap, why would anybody do something like this?
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
cagdasgerede
Posts: 241
Registered: ‎11-20-2008
My Device: Not Specified

Re: Scaling a Bitmap (Without EncodedImage)

As you expect, we are receiving the images in compressed format. However, due to processing, we need to decompress them and work on argb[].

Please use plain text.
Developer
cagdasgerede
Posts: 241
Registered: ‎11-20-2008
My Device: Not Specified

Re: Scaling a Bitmap (Without EncodedImage)

[ Edited ]

I was checking Android documentation to see how they handle scaling. In their case, the scaling api allows you to provide a temporary storage. This way you can reuse the memory you allocated again and again to resize different bitmaps without triggering garbage collection.

 

I hope Blackberry extends their apis to have this kind of functionality.

 

 

See these classes:

 

http://developer.android.com/reference/android/graphics/BitmapFactory.html 

 

static Bitmap decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts)

Decode an immutable bitmap from the specified byte array.

 

 

and 

 

http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html

 

 
public byte[] inTempStorage Temp storage to use for decoding.
public int inSampleSize If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. 

 

Message Edited by cagdasgerede on 06-26-2009 11:51 AM
Please use plain text.
New Contributor
jonesc
Posts: 6
Registered: ‎03-08-2010
My Device: BlackBerry Simulator
My Carrier: Engineer

Re: Scaling a Bitmap (Without EncodedImage)

In SDK 5.0, Bitmap class has a new function called scaleInto().

 

Before 5.0, you can try to decode your picture into BMP format (uncompressed) instead of ARGB.

Then you will get an EncodedImage of .bmp.

 

Jones

Please use plain text.
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: Scaling a Bitmap (Without EncodedImage)

I found this code a while back for scaling images below 5.0 but if you are targeting 5.0 then I highly suggest the scaleInto() method. It works very well

 

 

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


    public static Bitmap resizeBitmap(Bitmap image, int width, int height)
    {       
            //Need an array (for RGB, with the size of original image)
             //
             int rgb[] = new int[image.getWidth()*image.getHeight()];

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

           //Call to our function and obtain RGB2
         //
             int rgb2[] = rescaleArray(rgb, image.getWidth(), image.getHeight(), 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;
    }

     public Bitmap bestFit(Bitmap image, int maxWidth, int maxHeight)
        {

             // getting image properties
            int w = image.getWidth();
              int h = image.getHeight();

            //  get the ratio
              int ratiow = 100 * maxWidth / w;
               int ratioh = 100 * maxHeight / h;

                             // this is to find the best ratio to
                           // resize the image without deformations
               int ratio = Math.min(ratiow, ratioh);

         // computing final desired dimensions
          int desiredWidth = w * ratio / 100;
            int desiredHeight = h * ratio / 100;
            imgscale = ratio;

          //resizing
             return resizeBitmap(image, desiredWidth, desiredHeight);
       }

 So you would call bestFit with the area that you needed to fit it into and it would return the resized bitmap. Note that this only preserves aspect ratio, it does not fill.

 

Like all of my posts
Please use plain text.
Developer
tuxGurl
Posts: 20
Registered: ‎07-08-2010
My Device: 9700
My Carrier: Telus

Re: Scaling a Bitmap (Without EncodedImage)

I stumbled across this.. here are the methods behind the 5.0 bitmap scaling and rotating that you can use on lower versions like 4.5:

http://supportforums.blackberry.com/t5/Java-Development/Rotate-and-scale-bitmaps/ta-p/492524

Please use plain text.