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
dchapiesky
Posts: 20
Registered: ‎07-30-2008
My Device: Not Specified

Image Resize

When resizing a bitmap, I wanted to supply the width and height rather than a Scale ratio...

 

Here is my code based on open source from the Funambol project.  I have included the legals so that everyone get's their due recognition.

 

Basically I converted their code from using the Image object to the Bitmap object.

 

Hope this helps anyone else trying to accurately resize bitmaps. 

 

Note: Aspect Ratio is preserved...

 

As a demonstration application I encapsulated the resizing code in the SplashScreen demo from RIM:

 

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800256/...

 

Daniel

 

/* Demonstrate a splash screen that automatically resizes the splash bitmap to the size of the given device display..... Based on and extends code from: http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800256/How_To_-_Create_a_splash_screen.html?nodeid=800334&vernum=0 Additional code from: Funambol is a mobile platform developed by Funambol, Inc. Copyright (C) 2003 - 2007 Funambol, Inc. sync4j-cvs-2008-07-21\funambol\clients\j2me\mail\UI\basic\src\com\funambol\mailclient\ ui \ utils\UiUtils.java they were using javax.microedition.lcdui.Image changed to use net.rim.device.api.system.Bitmap Included here as part of the license at the head of UiUtils.java ----------------------------------------------------------------------------------- * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU Affero General Public License version 3 as published by * the Free Software Foundation with the addition of the following permission * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. ----------------------------------------------------------------------------------- Under the above term here is my modifications for net.rim platform mkSplashScreen() Daniel Chapiesky dchapiesky@cmtechnologyservices.net Example use: DemoApp() { DemoMainScreen myMainScreen = new DemoMainScreen(); DemoSplashScreen mySplashScreen = new mkSplashScreen("img/mySplashScreen.png", this, myMainScreen); // // the constructor of mkSplashScreen automatically pushes its self/ pops its self / and then pushes // the myMainScreen... // } */ import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; import net.rim.device.api.system.*; import net.rim.device.api.system.Bitmap; import net.rim.device.api.system.Display; import java.util.*; public class mkSplashScreen extends MainScreen { private MainScreen next; private UiApplication application; private Timer timer = new Timer(); private static Bitmap original_bitmap; private static Bitmap _bitmap; public mkSplashScreen(String splashImageName, UiApplication ui, MainScreen next) { super(Field.USE_ALL_HEIGHT | Field.FIELD_LEFT); original_bitmap = Bitmap.getBitmapResource(splashImageName); Display myDisplay = new Display(); _bitmap = bestFit(original_bitmap, myDisplay.getWidth(), myDisplay.getHeight()); this.application = ui; this.next = next; this.add(new BitmapField(_bitmap)); mkSplashScreenListener listener = new mkSplashScreenListener(this); this.addKeyListener(listener); timer.schedule(new CountDown(), 5000); application.pushScreen(this); } public void dismiss() { timer.cancel(); application.popScreen(this); application.pushScreen(next); } private class CountDown extends TimerTask { public void run() { DismissThread dThread = new DismissThread(); application.invokeLater(dThread); } } private class DismissThread implements Runnable { public void run() { dismiss(); } } protected boolean navigationClick(int status, int time) { dismiss(); return true; } protected boolean navigationUnclick(int status, int time) { return false; } protected boolean navigationMovement(int dx, int dy, int status, int time) { return false; } public static class mkSplashScreenListener implements KeyListener { private mkSplashScreen screen; public boolean keyChar(char key, int status, int time) { //intercept the ESC and MENU key - exit the splash screen boolean retval = false; switch (key) { case Characters.CONTROL_MENU: case Characters.ESCAPE: screen.dismiss(); retval = true; break; } return retval; } public boolean keyDown(int keycode, int time) { return false; } public boolean keyRepeat(int keycode, int time) { return false; } public boolean keyStatus(int keycode, int time) { return false; } public boolean keyUp(int keycode, int time) { return false; } public mkSplashScreenListener(mkSplashScreen splash) { screen = splash; } } // based on code from // // * Funambol is a mobile platform developed by Funambol, Inc. // * Copyright (C) 2003 - 2007 Funambol, Inc. // sync4j-cvs-2008-07-21\funambol\clients\j2me\mail\UI\basic\src\com\funambol\mailclient\ ui \ utils\UiUtils.java // // they were using javax.microedition.lcdui.Image // // changed to use net.rim.device.api.system.Bitmap // // Included here as part of the license at the head of UiUtils.java // // ----------------------------------------------------------------------------------- // * This program is free software; you can redistribute it and/or modify it under // * the terms of the GNU Affero General Public License version 3 as published by // * the Free Software Foundation with the addition of the following permission // * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED // * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE // * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. // ----------------------------------------------------------------------------------- // // Under the above term here is my modifications for net.rim platform // // Daniel Chapiesky // dchapiesky@cmtechnologyservices.net // 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) { // Note from DCC: // an int being 4 bytes is large enough for Alpha/Red/Green/Blue in an 8-bit plane... // my brain was fried for a little while here because I am used to larger plane sizes for each // of the color channels.... // //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 static 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; //resizing return resizeBitmap(image, desiredWidth, desiredHeight); } }

 

 

 

 

 

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Image Resize

i usually use EncodedImage.scaleImage32
----------------------------------------------------------
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
Developer
kzettel
Posts: 120
Registered: ‎07-16-2008
My Device: 9000

Re: Image Resize

EncodedImage.scaleImage was introduced in 4.2.0.  Anything older OSes cannot use that method.
Kurt Zettel | Chief Architect | Metova
www.metova.com
Developer
dchapiesky
Posts: 20
Registered: ‎07-30-2008
My Device: Not Specified

Re: Image Resize

Thats' right... this method is moderately portable across OS versions. 
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Image Resize

I use that approach a line at a time for 2^n scaling w/o interpolation. For large images,you can just

make the pieces you need ( there is no need to do more than fits on a screen at a time, x scrolling

piece buffering etc).

Developer
smiley
Posts: 1,497
Registered: ‎07-14-2008
My Device: Z10

Re: Image Resize

your sample code works great in the simulator but when i try loading a large bitmap 1920x960 it crashes with out of memory error in the real device.

 

is there a way to replace the scaling function with scaleImage32() ??

Developer
rajeshkparmar
Posts: 104
Registered: ‎08-03-2010
My Device: Blackberry Z10

Re: Image Resize

 

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

 You can Use for resizing your Bitmap Images...

 


Please mark posts as solved if you found a solution.
Feel free to press the kudos button to thank the user that helped you
Contributor
sneha_J
Posts: 21
Registered: ‎02-10-2012
My Device: 8520, 9790

Re: Image Resize

Thanks Rajesh..

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

Re: Image Resize

If you are processing on more recent OS's that support, I recommend looking at Bitmap's scaleInto() method

Contributor
sneha_J
Posts: 21
Registered: ‎02-10-2012
My Device: 8520, 9790

Re: Image Resize

Hello peter,

Thank for your reply,

I have tried with the scaleinto method bt its not working for all the images.