BlackBerry Forums Support Community

Closed Thread
LinkBack Thread Tools
Old 02-22-2007, 12:38 AM   #1 (permalink)
New Member
Join Date: Jan 2007
Model: 7100T
Carrier: j2me
Posts: 3
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default problem in resizing of images according to width and height which we give as input

Please Login to Remove!

This is kannan.We are developing an photoviewer applicaion using blackberry api. In that we want to resize the images according to the height and width which we give as input. For that we used image class of javax.microedition.lcdui to create the image first and then resize it
by using shown below .

int[] argb = null;
int imageWidth = 100;
int imageHeight = 100;

Image temp = Image.createImage(data, 0, data.length);
temp = resizeImage(temp, imageWidth, imageHeight );
Bitmap bitMap = new Bitmap([imageWidth, imageHeight);
argb = new int[imageWidth * imageHeight];
BitmapField bitfield = new BitmapField();
temp.getRGB(argb, 0, imageWidth, 0, 0, imageWidth, imageHeight); bitMap.setARGB(argb, 0, imageWidth, 0, 0, imageWidth, imageHeight);

By using above code Bitmap is added to BitmapField and displayed in mainscreen..And below code is used to resize the image according to width and height which we give as input.

public Image resizeImage(Image src, int dstW, int dstH) {
if (src != null && src.getWidth() == dstW && src.getHeight() == dstH) {
return src;

final int srcW = src.getWidth();
final int srcH = src.getHeight();

final Image tmp = Image.createImage(dstW, srcH);
Graphics g = tmp.getGraphics();

int delta = (srcW << 16) / dstW;
int pos = delta/2;

for (int x = 0; x < dstW; x++) {
g.setClip(x, 0, 1, srcH);
g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
pos += delta;

final Image dst = Image.createImage(dstW, dstH);
g = dst.getGraphics();

delta = (srcH << 16) / dstH;
pos = delta/2;

for (int y = 0; y < dstH; y++) {
g.setClip(0, y, dstW, 1);
g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
pos += delta;
return dst;

The problem is itís working fine when the image size is small. But when the image size is large it takes time to create the image using image class and to display through bitmap.
So how to create images fastly in blackberry devices and resize it according to height and width which we give as input. And then display it in mainscreen. Is their any other way to resize the images without using image class and bitmap class. Vey urgent. Help me in this regard.

Thanks and Regards,
Old 02-22-2007, 12:41 AM   #2 (permalink)
BBF Moderator
John Clark's Avatar
Join Date: Jun 2005
Model: Z30
OS: 10.2.1.x
PIN: s & needles
Carrier: AT&T
Posts: 34,715
Post Thanks: 6
Thanked 115 Times in 82 Posts

*** Moved to the Developer Forum ***
Old 02-23-2007, 05:54 AM   #3 (permalink)
Talking BlackBerry Encyclopedia
Skipper_Joe's Avatar
Join Date: Jan 2007
Location: Kharkov, Ukraine
Model: 8300
Carrier: N/A
Posts: 237
Post Thanks: 0
Thanked 0 Times in 0 Posts

We trapped into the same problem. We use EncodedImage instead of Bitmap and Image.
EncodedImage can be resized using its method setScale() and can be drawn on Graphics using one of Graphics.drawImage() methods.

I just wrote about it yesterday: How to display a .GIF image?
Old 05-06-2007, 11:09 PM   #4 (permalink)
Thumbs Must Hurt
Join Date: Feb 2007
Model: 8100
Carrier: T-Mobile
Posts: 54
Post Thanks: 0
Thanked 0 Times in 0 Posts

Here's what I do to scale an image. There may be an easier way to do it by percentage of original image size, but I haven't found it.
Hope this helps.

EncodedImage bitmap = null;
String path = "/classpath/to/image.png";
InputStream in = ThisClass.class.getResourceAsStream(path);
byte[] data = new byte[in.available()];;
bitmap = EncodedImage.createEncodedImage(data, 0, data.length);

int divisor = Fixed32.toFP(100);

* Change this number, < 100 makes the image bigger by
* percentage. i.e. 90 makes the image 110% normal size.
* By increasing, you will reduce the image size,
* e.g. 110 makes the image 90% normal size.
int multiplier = Fixed32.toFP(110);

int fixedX = Fixed32.toFP(1);
/** First, divide the image scale by 100. */
fixedX = Fixed32.div(fixedX,divisor);
/** Now, multiply the image scale by the multiplier. */
fixedX = Fixed32.mul(fixedX,multiplier);

int fixedY = Fixed32.toFP(1);
fixedY = Fixed32.div(fixedY,divisor);
fixedY = Fixed32.mul(fixedY,multiplier);

bitmap = bitmap.scaleImage32(fixedX,fixedY);

BitmapField bmp = new BitmapField();

Copyright © 2004-2016
The names RIM © and BlackBerry © are registered Trademarks of BlackBerry Inc.