在 Android 中基于叠加裁剪位图 - Camera API 2

BRD*_*oid 5 android bitmap android-camera android-bitmap android-camera2

我正在使用相机 api 来捕获身份证照片,我有一个叠加层,如下图所示。我想裁剪框中的图像。你能建议一下具体应该如何做吗?我已经写下了我所尝试过的以及它给我的结果。

这是我想要捕获的 id 的屏幕截图..

在此输入图像描述

输出。

在此输入图像描述

白色矩形框是一个相框,位于相对布局的中心

<View
    android:id="@+id/photo_frame"
    android:layout_width="match_parent"
    android:layout_height="212dp"
    android:background="@drawable/bg_photo_frame"
    android:layout_centerInParent="true"
    android:layout_margin="@dimen/double_padding"
    android:visibility="visible"/>
Run Code Online (Sandbox Code Playgroud)

如何计算这个帧来剪切图像

这就是我必须剪切需要修改的图像,但不确定前进的方向是什么

       public Bitmap cutImage(final Bitmap srcBmp, final int pixepWidth, final int pixelsHeight, float widthRatio) {
//        Bitmap croppedBitmap = Bitmap.createBitmap(bitmap, 20, 20, pixepWidth, pixelsHeight);
//        return croppedBitmap;
        Bitmap dstBmp;
        if (srcBmp.getWidth() >= srcBmp.getHeight()){

            dstBmp = Bitmap.createBitmap(
                    srcBmp,
                    srcBmp.getWidth()/2 - srcBmp.getHeight()/2,
                    0,
                    srcBmp.getHeight(),
                    srcBmp.getHeight()
            );

        }else{

            dstBmp = Bitmap.createBitmap(
                    srcBmp,
                    0,
                    srcBmp.getHeight()/2 - srcBmp.getWidth()/2,
                    srcBmp.getWidth(),
                    srcBmp.getWidth()
            );
        }

        return dstBmp;
    }
Run Code Online (Sandbox Code Playgroud)

Mic*_*l N 0

使用 onTouchListener 调整视图大小

\n

您可以将某种 touchListener 应用于叠加视图,以调整叠加层的大小和移动叠加层。ImageButton 似乎可以工作,因此我将 ImageButton 与frame.xml 一起使用来绘制边框。

\n

然后,将叠加层的全局坐标(x、y、宽度、高度)应用于位图的坐标(x、y、宽度、高度)。

\n

覆盖x和覆盖y不能小于0,并且覆盖x+覆盖宽度不能大于位图宽度。

\n

要执行裁剪,请使用 Bitmap.createBitmap():

\n
Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height)\n
Run Code Online (Sandbox Code Playgroud)\n

如何裁剪位图?

\n