Create rectangle bitmap from trapezoid crop

强颜欢笑 提交于 2021-02-11 12:15:34

问题


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 sure as to how can I convert the trapezoid to rectangle.

Any input from the community is appreciate.


回答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 = new float[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 rectangle
        float[] dst = new float[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 = new float[] { 0, 0 };
        matrix.mapPoints(mappedTL);
        int maptlx = Math.round(mappedTL[0]);
        int maptly = Math.round(mappedTL[1]);

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

        float[] mappedLL = new float[] { 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



来源:https://stackoverflow.com/questions/57033572/create-rectangle-bitmap-from-trapezoid-crop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!