09-28-2012 02:18 AM
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.
09-28-2012 05:45 AM
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.