Skip to content Skip to sidebar Skip to footer

Create Rectangle Bitmap From Trapezoid Crop

I would like to convert the trapezoid section of the image denoted by points A,B,C,D to a rectangle bitmap. I am able to make the user mark the 4 points in the image, but I am not

Solution 1:

The implementation has been done. Thanks for @IcedLance too for the reference. This is for future reference and for the community. The entire implementation has been done.

private Bitmap trapezoidToRectangleTransform() {

        float[] src = newfloat[8];

        Point point = mAnchorPoints.get(0);
        src[0] = point.x;
        src[1] = point.y;

        point = mAnchorPoints.get(1);
        src[2] = point.x;
        src[3] = point.y;

        point = mAnchorPoints.get(3);
        src[4] = point.x;
        src[5] = point.y;

        point = mAnchorPoints.get(2);
        src[6] = point.x;
        src[7] = point.y;

        // set up a dest polygon which is just a rectanglefloat[] dst = newfloat[8];
        dst[0] = 0;
        dst[1] = 0;
        dst[2] = mOriginalBitmap.getWidth();
        dst[3] = 0;
        dst[4] = mOriginalBitmap.getWidth();
        dst[5] = mOriginalBitmap.getHeight();
        dst[6] = 0;
        dst[7] = mOriginalBitmap.getHeight();

        // create a matrix for transformation.
        Matrix matrix = new Matrix();

        // set the matrix to map the source values to the dest values.
        boolean mapped = matrix.setPolyToPoly (src, 0, dst, 0, 4);

        float[] mappedTL = newfloat[] { 0, 0 };
        matrix.mapPoints(mappedTL);
        int maptlx = Math.round(mappedTL[0]);
        int maptly = Math.round(mappedTL[1]);

        float[] mappedTR = newfloat[] { mOriginalBitmap.getWidth(), 0 };
        matrix.mapPoints(mappedTR);
        int maptry = Math.round(mappedTR[1]);

        float[] mappedLL = newfloat[] { 0, mOriginalBitmap.getHeight() };
        matrix.mapPoints(mappedLL);
        int mapllx = Math.round(mappedLL[0]);

        int shiftX = Math.max(-maptlx, -mapllx);
        int shiftY = Math.max(-maptry, -maptly);

        Bitmap croppedAndCorrected = null;
        if (mapped) {
            Bitmap imageOut = Bitmap.createBitmap(mOriginalBitmap, 0, 0, mOriginalBitmap.getWidth(), mOriginalBitmap.getHeight(), matrix, true);
            croppedAndCorrected = Bitmap.createBitmap(imageOut, shiftX, shiftY, mOriginalBitmap.getWidth(), mOriginalBitmap.getHeight(), null, true);
            imageOut.recycle();
            mOriginalBitmap.recycle();
            System.gc();
        }

        return croppedAndCorrected;
    }

Here is the link: https://github.com/wwdablu/ExtImageView/blob/master/extimageview/src/main/java/com/wwdablu/soumya/extimageview/trapez/OriginalBitmapCropper.java

Post a Comment for "Create Rectangle Bitmap From Trapezoid Crop"