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
Contributor
Posts: 48
Registered: ‎04-07-2011
My Device: 9550
My Carrier: Desktop

Problem In Crop Image Its not Cropping Correctly

Hi ,

 

   I have selected an area i want to Crop but it Crops another Area i did not Know Where the Problem.

 

   I Used the Following Code :

  

private void croppingImage() {

if (firstXY != null && lastXY != null) {
croppedImage = cropImage(image, xyRect.x, xyRect.y, xyRect.width,
xyRect.height);
if (croppedImage != null) {
 image = croppedImage;
firstXY = null;
lastXY = null;

} else {
UiApplication.getUiApplication().invokeLater(new Runnable() {

public void run() {
Dialog.alert(" Please select the image area and click on Crop button ");
}
});
}
}

}

 

private Bitmap cropImage(Bitmap image, int x, int y, int width, int height) {
Bitmap result = new Bitmap(width, height);
Graphics g = Graphics.create(result);
g.drawBitmap(0, 0, width, height, image, x, y);
isCropped = true;
return result;
}

 

Please Give me the Solution For this Problem...........

 

 

Thanks & Regards,

Nagarjuna Metla.

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

Re: Problem In Crop Image Its not Cropping Correctly

You don't define what cropImage actually does.  For example, if you crop an image and the image you want is shorter, but the same width as the original, then you need to loose some of the original, but you could loose it from the top, the bttom, or both. 

 

Assuming you want it to crop the image so that it displays in the 'centre', you code would look something like:

 

private Bitmap cropImage(Bitmap image, int x, int y, int width, int height) {
Bitmap result = new Bitmap(width, height);
Graphics g = Graphics.create(result);

int currentWidth = image.getWidth();

int currentHeight = image.getHeight();
g.drawBitmap((width - currentWidth)/2, (height - currentHeight)/2, Math.max(width, currentWidth), Math.max(height, currentHeight), image, 0, 0);
isCropped = true;
return result;
}

 

I'm sure you can figure out the calculations involved if you need to position the image anywhere else.