首页 > 解决方案 > 如何在 AndroidStudio 中使用 EasyImage 将矩形叠加层添加到相机屏幕

问题描述

我有这个代码在用户按下按钮时运行:

    fun takeImage(fragment: Fragment?) {
        Dexter.withActivity(fragment?.activity)
                .withPermissions(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .withListener(object : BaseMultiplePermissionsListener() {
                    override fun onPermissionsChecked(report: MultiplePermissionsReport?) {
                        if (report?.areAllPermissionsGranted() == true)
                            EasyImage.openChooserWithGallery(fragment, fragment?.getString(R.string.select_image), 0)
                        else
                            // Show Error
                    }
                })
                .check()
    }

我希望相机以白色矩形作为占位符打开,以便用户可以将需要拍照的卡片放入其中,如下所示: https ://i.stack.imgur.com/QurcT.png

我正在使用 EasyImage 打开相机视图,我应该怎么做那个矩形?

标签: javaandroidkotlineasyimage

解决方案


您可以使用相机套件https://github.com/CameraKit/camerakit-android 它使用表面视图,在其中您可以根据需要自定义相机。

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@color/white"
    android:fitsSystemWindows="true">

    <com.camerakit.CameraKitView
        android:id="@+id/cameraKitView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignTop="@+id/maskView"
        android:layout_alignBottom="@+id/maskView"
        android:adjustViewBounds="true"
        android:keepScreenOn="true"
        app:camera_flash="off"
        app:camera_focus="auto"
        app:camera_permissions="camera" />

    <br.com.getninjas.pro.view.SquareImageView
        android:id="@+id/maskView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mask_camera_toolbar"
        tools:background="@drawable/ic_document_selfie_mask" />

示例图像


推荐阅读