从梯形裁剪创建矩形位图 [英] Create rectangle bitmap from trapezoid crop

查看:70
本文介绍了从梯形裁剪创建矩形位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将点A,B,C,D表示的图像的梯形部分转换为矩形位图.

I would like to convert the trapezoid section of the image denoted by points A,B,C,D to a rectangle bitmap.

我能够使用户标记图像中的4个点,但是我不确定如何将梯形转换为矩形.

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.

感谢社区的任何投入.

推荐答案

实现已完成.也感谢@IcedLance供参考.这是供将来参考和供社区使用.整个实现已完成.

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;
    }

这里是链接:https://github.com/wwdablu/ExtImageView/blob/master/extimageview/src/main/java/com/wwdablu/soumya/extimageview/trapez/OriginalBitmapCropper.java

这篇关于从梯形裁剪创建矩形位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆