When a user wants to set a picture as profile pic he will need the functionaly for cropping the picture.
import net.rim.device.api.system.Bitmap; import net.rim.device.api.system.Display; import net.rim.device.api.ui.Graphics; import net.rim.device.api.ui.MenuItem; import net.rim.device.api.ui.TouchEvent; import net.rim.device.api.ui.UiApplication; import net.rim.device.api.ui.component.BitmapField; import net.rim.device.api.ui.component.ButtonField; import net.rim.device.api.ui.container.MainScreen; public final class MyCropScreen extends MainScreen { Graphics graphics; int initialLeftPoint = 80; int touchX ; int touchY ; int crop_rectangle_x[] = new int[4]; int crop_rectangle_y[] = new int[4]; int crop_rect_width; int pointerToPoint; Bitmap picture; ButtonField save; BitmapField field; boolean downInsideCropArea = false; boolean onCropLine = false; int lastMovePointSize = 0; /** * * @param bitmap bitmap to be cropped * @param browseScreenInstance screen instance to which cropped bitmap is to be set */ public MyCropScreen(final Bitmap bitmap, final MyBrowseScreen browseScreenInstance) { picture = bitmap; Bitmap blank_surface = new Bitmap(Bitmap.getDefaultType(), picture.getWidth(), picture.getHeight()); graphics = Graphics.create(blank_surface); graphics.drawBitmap(0, 0, picture.getWidth(), picture.getHeight(), picture, 0, 0); crop_rectangle_x[0] = crop_rectangle_x[3] = initialLeftPoint; crop_rectangle_x[1] = crop_rectangle_x[2] = Display.getWidth() - initialLeftPoint; crop_rectangle_y[0] = crop_rectangle_y[1] = (Display.getHeight() - (crop_rectangle_x[2] - crop_rectangle_x[0])) / 2; crop_rectangle_y[2] = crop_rectangle_y[3] = crop_rectangle_y[0] + (crop_rectangle_x[2] - crop_rectangle_x[0]); graphics.drawOutlinedPath(crop_rectangle_x, crop_rectangle_y, null, null, true); crop_rect_width = crop_rectangle_x[1] - crop_rectangle_x[0]; field = new BitmapField(blank_surface) { protected boolean touchEvent(TouchEvent message) { int eventCode = message.getEvent(); if(eventCode == TouchEvent.DOWN ){ // Get the screen coordinates of the touch event touchX = message.getX(1); touchY = message.getY(1); } if (eventCode == TouchEvent.DOWN || downInsideCropArea || onCropLine) { if ((onCropLine || onTheCropLine(touchX, touchY)) && !downInsideCropArea) { onCropLine = true; if (eventCode == TouchEvent.MOVE) { System.out.println(" inside move"); int size = message.getMovePointsSize(); int moveX[] = new int[size]; int moveY[] = new int[size]; message.getMovePoints(1, moveX, moveY, new int[size]); if (size == 1) { cropPoints(moveX[0], moveY[0], touchX, touchY); graphics.clear(); graphics.drawBitmap(0, 0, picture.getWidth(), picture.getHeight(), picture, 0, 0); graphics.drawOutlinedPath(crop_rectangle_x, crop_rectangle_y, null, null, true); invalidate(); } for (int i = lastMovePointSize; i < size && size > 1; i++) { cropPoints(moveX[i], moveY[i], moveX[i - 1], moveY[i - 1]); graphics.clear(); graphics.drawBitmap(0, 0, picture.getWidth(), picture.getHeight(), picture, 0, 0); graphics.drawOutlinedPath(crop_rectangle_x, crop_rectangle_y, null, null, true); invalidate(); } lastMovePointSize = size; System.out.println(crop_rectangle_x[0] + "," + crop_rectangle_x[1] + "," + crop_rectangle_x[2] + "," + crop_rectangle_x[3]); System.out.println(crop_rectangle_y[0] + "," + crop_rectangle_y[1] + "," + crop_rectangle_y[2] + "," + crop_rectangle_y[3]); System.out.println("ON CROP MOVE"); }else{ System.out.println(" outside move on crop event code is "+eventCode); } } else { if (downInsideCropArea || insideCropRect(touchX, touchY)) { downInsideCropArea = true; if (eventCode == TouchEvent.MOVE) { int size = message.getMovePointsSize(); int moveX[] = new int[size]; int moveY[] = new int[size]; message.getMovePoints(1, moveX, moveY, new int[size]); if (size == 1) { int movementX = moveX[0] - touchX; int movementY = moveY[0] - touchY; int x_new[] = { crop_rectangle_x[0] + movementX, crop_rectangle_x[0] + movementX + crop_rect_width, crop_rectangle_x[0] + movementX + crop_rect_width, crop_rectangle_x[0] + movementX }; int y_new[] = { crop_rectangle_y[0] + movementY, crop_rectangle_y[0] + movementY , crop_rectangle_y[0] + movementY + crop_rect_width, crop_rectangle_y[0] + movementY + crop_rect_width}; if (isPointInsidePicture(x_new, y_new)) { crop_rectangle_x = x_new; crop_rectangle_y = y_new; graphics.clear(); graphics.drawBitmap(0, 0, picture.getWidth(), picture.getHeight(), picture, 0, 0); graphics.drawOutlinedPath(crop_rectangle_x, crop_rectangle_y, null, null, true); invalidate(); } } for (int i = lastMovePointSize; i < size && size > 1; i++) { int movementX = moveX[i] - moveX[i - 1]; int movementY = moveY[i] - moveY[i - 1]; int x_new[] = { crop_rectangle_x[0] + movementX, crop_rectangle_x[0] + movementX + crop_rect_width, crop_rectangle_x[0] + movementX + crop_rect_width, crop_rectangle_x[0] + movementX }; int y_new[] = { crop_rectangle_y[0] + movementY, crop_rectangle_y[0] + movementY , crop_rectangle_y[0] + movementY + crop_rect_width, crop_rectangle_y[0] + movementY + crop_rect_width}; if (isPointInsidePicture(x_new, y_new)) { crop_rectangle_x = x_new; crop_rectangle_y = y_new; System.out.println(crop_rectangle_x[0] + "," + crop_rectangle_x[1] + "," + crop_rectangle_x[2] + "," + crop_rectangle_x[3]); System.out.println(crop_rectangle_y[0] + "," + crop_rectangle_y[1] + "," + crop_rectangle_y[2] + "," + crop_rectangle_y[3]); System.out.println(movementX + "," + movementY); graphics.clear(); graphics.drawBitmap(0, 0, picture.getWidth(), picture.getHeight(), picture, 0, 0); graphics.drawOutlinedPath(crop_rectangle_x, crop_rectangle_y, null, null, true); invalidate(); } } lastMovePointSize = size; } } else { lastMovePointSize = 0; downInsideCropArea = false; } } } if (eventCode == TouchEvent.UP) { lastMovePointSize = 0; downInsideCropArea = false; onCropLine = false; } return true; } }; add(field); MenuItem crop = new MenuItem("crop", 0, 0) { public void run() { Bitmap croppedBitmap = cropImage(bitmap, crop_rectangle_x[0], crop_rectangle_y[0], (crop_rectangle_x[2] - crop_rectangle_x[0]), (crop_rectangle_y[3] - crop_rectangle_y[0])); browseScreenInstance.setBitmap(croppedBitmap); UiApplication.getUiApplication().popScreen(MyCropScreen.this); }; }; addMenuItem(crop); } // This method determines what the next crop points should be public void cropPoints(int movementX, int movementY, int touchX, int touchY) { int diff = 0; if ((touchX < crop_rectangle_x[0] + 3 && touchX > crop_rectangle_x[0] - 5)) { if (movementX >= 1) { diff = crop_rectangle_x[0] - movementX; crop_rectangle_x[0] = crop_rectangle_x[3] = movementX; crop_rectangle_y[0] = crop_rectangle_y[1] = crop_rectangle_y[0] - diff; crop_rectangle_x[1] = crop_rectangle_x[2] = crop_rectangle_x[2] + diff; crop_rectangle_y[2] = crop_rectangle_y[3] = crop_rectangle_y[2] + diff; } System.out.println("diff " + diff + "move x " + movementX); System.out.println(crop_rectangle_x[0] + "," + crop_rectangle_x[1] + "," + crop_rectangle_x[2] + "," + crop_rectangle_x[3]); System.out.println(crop_rectangle_y[0] + "," + crop_rectangle_y[1] + "," + crop_rectangle_y[2] + "," + crop_rectangle_y[3]); } else if ((touchX < crop_rectangle_x[1] + 3 && touchX > crop_rectangle_x[1] - 5)) { if (movementX <= (Display.getWidth() - 1)) { diff = crop_rectangle_x[1] - movementX; crop_rectangle_x[0] = crop_rectangle_x[3] = crop_rectangle_x[0] + diff; crop_rectangle_y[0] = crop_rectangle_y[1] = crop_rectangle_y[0] + diff; crop_rectangle_x[1] = crop_rectangle_x[2] = movementX; crop_rectangle_y[2] = crop_rectangle_y[3] = crop_rectangle_y[2] - diff; } System.out.println("diff " + diff + "move x " + movementX); System.out.println(crop_rectangle_x[0] + "," + crop_rectangle_x[1] + "," + crop_rectangle_x[2] + "," + crop_rectangle_x[3]); System.out.println(crop_rectangle_y[0] + "," + crop_rectangle_y[1] + "," + crop_rectangle_y[2] + "," + crop_rectangle_y[3]); } else if ((touchY < crop_rectangle_y[0] + 3 && touchY > crop_rectangle_y[0] - 5)) { if(movementY >= 1){ diff = crop_rectangle_y[0] - movementY; crop_rectangle_x[0] = crop_rectangle_x[3] = crop_rectangle_x[0] - diff; crop_rectangle_y[0] = crop_rectangle_y[1] = movementY; crop_rectangle_x[1] = crop_rectangle_x[2] = crop_rectangle_x[2] + diff; crop_rectangle_y[2] = crop_rectangle_y[3] = crop_rectangle_y[2] + diff; } System.out.println("diff " + diff + "move x " + movementY); System.out.println(crop_rectangle_x[0] + "," + crop_rectangle_x[1] + "," + crop_rectangle_x[2] + "," + crop_rectangle_x[3]); System.out.println(crop_rectangle_y[0] + "," + crop_rectangle_y[1] + "," + crop_rectangle_y[2] + "," + crop_rectangle_y[3]); } else if ((touchY < crop_rectangle_y[2] + 3 && touchY > crop_rectangle_y[2] - 5)) { if(movementY <= (Display.getHeight() - 1)){ diff = crop_rectangle_y[2] - movementY; crop_rectangle_x[0] = crop_rectangle_x[3] = crop_rectangle_x[0] + diff; crop_rectangle_y[0] = crop_rectangle_y[1] = crop_rectangle_y[0] + diff; crop_rectangle_x[1] = crop_rectangle_x[2] = crop_rectangle_x[2] - diff; crop_rectangle_y[2] = crop_rectangle_y[3] = movementY; } System.out.println("diff " + diff + "move x " + movementY); System.out.println(crop_rectangle_x[0] + "," + crop_rectangle_x[1] + "," + crop_rectangle_x[2] + "," + crop_rectangle_x[3]); System.out.println(crop_rectangle_y[0] + "," + crop_rectangle_y[1] + "," + crop_rectangle_y[2] + "," + crop_rectangle_y[3]); } crop_rect_width = crop_rectangle_x[1] - crop_rectangle_x[0]; } // This method determines if the touched point is inside the given picture or not public boolean isPointInsidePicture(int x[], int y[]) { if (x[0] >= 1 && x[1] <= (Display.getWidth() - 1) && y[0] >= 1 && y[2] <= (Display.getHeight() - 1)) { return true; } return false; } // This method determines if the touched point is inside the crop rectangle or not public boolean insideCropRect(int touchX, int touchY) { if (crop_rectangle_x[0] < touchX && crop_rectangle_x[1] > touchX && crop_rectangle_y[0] < touchY && crop_rectangle_y[2] > touchY) { System.out.println("inside rect"); return true; } System.out.println("outside rect"); System.out.println(crop_rectangle_x[0] + "," + crop_rectangle_x[1] + "," + crop_rectangle_x[2] + "," + crop_rectangle_x[3]); System.out.println(crop_rectangle_y[0] + "," + crop_rectangle_y[1] + "," + crop_rectangle_y[2] + "," + crop_rectangle_y[3]); return false; } // This method determines if the touched point is on the crop rectangle or not public boolean onTheCropLine(int touchX, int touchY) { if ((touchX < crop_rectangle_x[0] + 3 && touchX > crop_rectangle_x[0] - 5) || (touchX < crop_rectangle_x[1] + 3 && touchX > crop_rectangle_x[1] - 5) || (touchY < crop_rectangle_y[0] + 3 && touchY > crop_rectangle_y[0] - 5) || (touchY < crop_rectangle_y[2] + 3 && touchY > crop_rectangle_y[2] - 5)) { System.out.println("on crop region"); return true; } System.out.println("outside crop"+touchX+" ,"+touchY); System.out.println(crop_rectangle_x[0] + "," + crop_rectangle_x[1] + "," + crop_rectangle_x[2] + "," + crop_rectangle_x[3]); System.out.println(crop_rectangle_y[0] + "," + crop_rectangle_y[1] + "," + crop_rectangle_y[2] + "," + crop_rectangle_y[3]); return false; } // This method returns the image after cropping public 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); return result; } }