1

I want to crop a bitmap using DragReactangle I want to Crop a bitmap with respect to the Dimensions of the Rectangle drawn over it. Heres my Method to CropBitmap

public static Bitmap getCroppedImage(Bitmap mBitmap) {


    //mRectLeft = left of rectangle
    //mRectRight = right of rectangle
    //mRectTop = top of rectangle
    //mRectBottom = Bottom of rectangle


    if ((sKeyRectX <= 0 && sKeyRectY <= 0 && sKeyRectWidth <= 0 && sKeyRectHeight <= 0)) {
        //If items are not initialized then we return the original bitmap
        return mBitmap;
    }


    double ratioHeight = ((double) mBitmap.getHeight() /(double) sKeyRectHeight);
    double ratioWidth = ((double) mBitmap.getWidth() /(double) sKeyRectWidth);


    /**
     * Do sanity checks i.e. y+height should be less than bitmap and x+width less than width of bitmap
     */
    double width = sKeyRectWidth * ratioWidth;
    double height = sKeyRectHeight * ratioHeight;
    double start_x_pixel = sKeyRectX * ratioWidth;
    double start_y_pixel = sKeyRectY * ratioHeight;

    start_x_pixel = start_x_pixel <= mBitmap.getWidth() ? start_x_pixel : mBitmap.getWidth();
    start_y_pixel = start_y_pixel <= mBitmap.getHeight() ? start_y_pixel : mBitmap.getHeight();

    if (width + start_x_pixel > mBitmap.getWidth()) {
        width = mBitmap.getWidth() - start_x_pixel;
        return mBitmap;//return the original bitamp
    }

    if (height + start_y_pixel > mBitmap.getHeight()) {
        height = mBitmap.getHeight() - start_y_pixel;
        return mBitmap;//return the original bitamp
    }

    Bitmap cropedBitmap = Bitmap
            .createBitmap(mBitmap, (int) start_x_pixel, (int) start_y_pixel,
                    (int) width,
                    (int) height
            );

    return cropedBitmap;

}

I want this static crop function that I used in other activity to get cropped images and I am storing Rectangle top, left, right, bottom in shared preferences so that I can call my images and they get cut in the same position I made it the first time.

How can I achieve this

0 Answers0