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

标签 android bitmap android-camera android-bitmap android-camera2

我正在使用 Camera api 捕捉身份证图片,如下图所示,我有一个覆盖。我想裁剪框中的图像。你能建议它应该如何完成吗?我已经写下了我尝试过的东西以及它给我的结果。

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

enter image description here

输出。

enter image description here

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

<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"/>

如何计算此帧以剪切图像

这就是我必须剪切需要修改的图像但不确定前进的方向
       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;
    }

最佳答案

Resizing view using onTouchListener
您将某种 touchListener 应用到覆盖 View 以调整和移动覆盖层。 ImageButton 似乎可以工作,所以我使用带有 frame.xml 的 ImageButton 来绘制边框。
然后将覆盖的全局坐标 (x, y, width, height) 应用于位图的坐标 (x, y, width, height)。
叠加 x 和叠加 y 不能小于 0,叠加 x + 叠加宽度不能大于位图宽度。
要执行裁剪,请使用 Bitmap.createBitmap():

Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height)
How to crop Bitmap?

关于android - 在 Android 中基于叠加层裁剪位图 - Camera API 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56322457/

相关文章:

Android程序到: Display random image for 90 seconds then switch

android - 如何在 ViewPager 中构建嵌套 fragment ?

Android 在离开 Activity 时从内存中删除位图

android - 如何在 Android 中提高 Camera 2 的图像质量?

android - 运行用于人脸检测的 OpenCV 示例时,将网络摄像头与 Android 模拟器结合使用

android - 清除最近的应用程序删除应用程序内存并且我的接收器停止工作

android - 无法理解 actionbarsherlock 选项卡/fragment/Activity

android - 如何控制位图的透明度?

Android Renderscript,从位图中删除白色/白色背景

android - 在 Zxing 条码应用程序中出现相机错误