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
Trusted Contributor
Posts: 159
Registered: ‎02-17-2011
My Device: Not Specified
Accepted Solution

problem with resize image

hello

 

i want to resize image at runtime according Display width.

i try some below code ,but it show me blank screen.

 

class image extends MainScreen
{
     EncodedImage bit;
      BitmapField bmf;
    image()
    {
            try{
                
            bmf=new BitmapField(Bitmap.getBitmapResource("next.png"));
            bmf.setImage(bit.scaleImage32(50,50));
            add(bmf);
            }
            catch(Exception e)
            {}
            
           
    }
}

Developer
Posts: 19,621
Registered: ‎07-14-2008
My Device: Not Specified

Re: problem with resize image

You haven't set bit to anything have you?  So you create a BitmapField with a real image, then update to an image that doesn't exist?  I suspect that if you put a break point in the catch, you will find that there is a null pointer exception generated by this code. 

 

As a general rule, I would recommend putting debug statements in all catches - otherwise you will never know if they have been used.

Trusted Contributor
Posts: 151
Registered: ‎06-17-2010
My Device: Curve 8900
My Carrier: Vodafone

Re: problem with resize image

Hi Hardikhamar,

 

Dont worry for your problem dear....

 

Here is the solution for you.. Try This code for Resize image and Enjoy... :smileyvery-happy:

 

 

public Bitmap setsizeImage(EncodedImage image, int width, 
    		  int height) {
    		  EncodedImage result = null;

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

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

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

    		  result = image.scaleImage32(scaleXFixed32, scaleYFixed32);
    		  return result.getBitmap();
    		 }

 


 

 

Don't Say thanks, press the kudos button  on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.

Trusted Contributor
Posts: 159
Registered: ‎02-17-2011
My Device: Not Specified

Re: problem with resize image

thanks for reply.

 

 

i know this code.

 

I think this code is useful when we process the url of  image  using http call.

 

But here i have  image  -> loading.png

 

So how i can resize it. 

 

Developer
Posts: 19,621
Registered: ‎07-14-2008
My Device: Not Specified

Re: problem with resize image

If you are using OS 5.0 or later, than there are various Bitmap scale functions available to you.  If not, then I have a more complicated option.....

Developer
Posts: 207
Registered: ‎09-10-2010
My Device: Not Specified

Re: problem with resize image

[ Edited ]

Peter is right it will throw null pointer exception. So first you need to initialize your 'bit' object with image.

 

EncodedImage bit = EncodedImage.getEncodedImageResource("next.png");

 

Then scale this image at run time according to display width:

 

Bitmap imageScaled = new Bitmap(Display.getWidth() , Display.getHeight() );

bit.getBitmap().scaleInto(imageScaled, Bitmap.FILTER_LANCZOS);

 

then add this image to your bitmap filed.

 

BitmapField bmf = new BitmapField(imageScaled);

 

Hope this helps. 

Trusted Contributor
Posts: 159
Registered: ‎02-17-2011
My Device: Not Specified

Re: problem with resize image

thanks for reply.

 

it's good code for higher version.

 

I make app in bb jde 4.3 and i have to support it in bb jde 4.3 and later version.

in bb jde 4.3 scaleInto() method not avaiable.

 

  

Trusted Contributor
Posts: 151
Registered: ‎06-17-2010
My Device: Curve 8900
My Carrier: Vodafone

Re: problem with resize image

[ Edited ]

Hey hardikkhamar,

 

Hey man, how you think that this code is useful when we process the url of  image  using http call?

 

Please Reffer the code once again... its for local image that is located in your res folder..

 

you have loading.png image, Right?

 

then get Encoded image of that bitmap Using

 

 

PNGEncodedImage encode = PNGEncodedImage.encode(Bitmap.getBitmapResource("loading.png"););

 and then pass encode to this method with requested height and width.. its so simple dude...

 

Bitmap Resizedbitmap = setsizeImage(encode, 200,150);

 


Don't Say thanks, press the kudos button  on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.

 

 

Developer
Posts: 19,621
Registered: ‎07-14-2008
My Device: Not Specified

Re: problem with resize image

Save yourself some processing and use the code supplied by indusBULL to created an EncodedImage:

 

EncodedImage bit = EncodedImage.getEncodedImageResource("next.png");

 

Then use the scale function you have already found (and that Albert_Anks pasted) to scale it. 

 

This approach is good for OS's 4.2 and later. 

Trusted Contributor
Posts: 159
Registered: ‎02-17-2011
My Device: Not Specified

Re: problem with resize image

thanks

 

Albert_Anks code works.