BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 02-21-2007, 11:38 PM   #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!

Hi,
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();
bitfield.setBitmap(bitMap);
add(bitfield);
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;
}
System.out.println("/////");
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,
kannan
Offline  
Old 02-21-2007, 11:41 PM   #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,667
Post Thanks: 1
Thanked 84 Times in 65 Posts
Default

*** Moved to the Developer Forum ***
Offline  
Old 02-23-2007, 04: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
Default

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?
Offline  
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
Default

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()];
in.read(data);
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);
in.close();

BitmapField bmp = new BitmapField();
bmp.setImage(bitmap);
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright © 2004-2014 BlackBerryForums.com.
The names RIM © and BlackBerry © are registered Trademarks of BlackBerry Inc.