【问题标题】:cropping a Bitmap based on overlay in Android - Camera API 2在 Android 中基于叠加层裁剪位图 - Camera API 2
【发布时间】:2019-10-12 19:35:39
【问题描述】:

我正在使用 Camera 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"/>

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

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

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

【问题讨论】:

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


    【解决方案1】:

    Resizing view using onTouchListener

    您将某种 touchListener 应用到覆盖视图以调整和移动覆盖层。 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?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 2011-10-18
      • 2015-02-02
      相关资源
      最近更新 更多