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
babakar
Posts: 770
Registered: ‎05-29-2012
My Device: see later
Accepted Solution

Image resize

I have this

 

private static final Bitmap _bitmap = Bitmap.getBitmapResource("SplashScreen.png");

 

 

how can i resize image based on screen size?

 

 

 public static EncodedImage sizeImage(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;  }

Developer
babakar
Posts: 770
Registered: ‎05-29-2012
My Device: see later

Re: Image resize

solution for anyone intrested

static EncodedImage eih1 = EncodedImage.getEncodedImageResource("SplashScreen.png");

private static final EncodedImage _bitmap2 = MAMClient.sizeImage(eih1,Display.getHeight(), Display.getWidth());
private static final Bitmap _bitmap = _bitmap2.getBitmap();
Developer
babakar
Posts: 770
Registered: ‎05-29-2012
My Device: see later

Re: Image resize

Full solution

 

 

 static EncodedImage eih1 = EncodedImage.getEncodedImageResource("SplashScreen.png");
   private static final EncodedImage _bitmap2 = sizeImage(eih1,Display.getWidth(), Display.getHeight());
   private static final Bitmap _bitmap = cropBitmap(_bitmap2.getBitmap(),Display.getWidth(),Display.getHeight());
   
   

 

 

public static EncodedImage sizeImage(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;  }

 

 

 public static Bitmap cropBitmap(Bitmap original, int width, int height) {
	        Bitmap bmp = new Bitmap(width, height);

	         int x = (original.getWidth() / 2) - (width / 2); // Center the new size by width
	        int y = (original.getHeight() / 2) - (height / 2); // Center the new size by height
	        int[] argb = new int[width * height]; 
	        original.getARGB(argb, 0, width, x, y, width, height);
	        bmp.setARGB(argb, 0, width, 0, 0, width, height);
	        return bmp;
	    }

 

Developer
Jam321
Posts: 59
Registered: ‎06-15-2012
My Device: 9800

Re: Image resize

 

Hey Babakar  Thanks for sharing the code. but insted of passing the file name can i pass the image path. i have tried that i am  getting error . how to handel that situation. In my case i am taking image from camera and saving to the internal storage. and i want to rezie that image stored in internal storage . like file:///stor/Test/test.png

 

Thanks in advances