如何使用布局内的摄像头扫描 QR 码(如 Whatsapp Web 扫描仪)

How can I use scan QR Code with a camera inside the layout (like Whatsapp Web scanner)

我是开发人员,但 Android 是新手,我需要知道如何在 Activity 布局中使用相机。

我知道我需要使用 Surface View 在 activity 中插入摄像头,目前我的应用程序正在使用默认摄像头使用 Google 视觉读取 QR 码(一个按钮打开摄像头,用户需要照片和我的应用程序感知 activity 结果)。

但我确实需要在带有实时扫描仪的应用程序中实现该功能。

有人可以指导我吗?

使用库,不要将相机放在 Activity 上,只需调用相机即可。 你可以检查这个 link 并尝试实现它:https://github.com/zxing/zxing

下面是我如何实现与您需要的类似的东西。

-首先,我使用了Zxing库来实现这个。因此,您需要将以下依赖项添加到 gradle:

compile 'com.journeyapps:zxing-android-embedded:3.5.0'

-下面直接link到journeyapps扫描仪项目'Github link:

https://github.com/journeyapps/zxing-android-embedded

-下面是我制作布局文件的方法:

<!-- language: lang-xml -->
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <com.journeyapps.barcodescanner.DecoratedBarcodeView
        android:id="@+id/view_scanner"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/view_footer"
        android:soundEffectsEnabled="true" />

    <LinearLayout
        android:id="@+id/view_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="horizontal">

        <LinearLayout
            android:id="@+id/view_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center|top"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/_10sdp"
                android:layout_marginTop="@dimen/_10sdp"
                android:src="@drawable/ic_back_light" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/_50sdp"
            android:gravity="right|top">

            <ImageView
                android:id="@+id/iv_flash"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/_10sdp"
                android:layout_marginTop="@dimen/_10sdp"
                android:src="@drawable/ic_flash_inactive" />
        </LinearLayout>
    </LinearLayout>


    <LinearLayout
        android:id="@+id/view_footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:gravity="center"
        android:orientation="vertical">

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="@dimen/_10sdp"
            android:layout_marginRight="@dimen/_10sdp"
            android:background="@android:color/darker_gray" />

        <TextView
            android:id="@+id/code_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@string/font_sans_serif_light"
            android:padding="@dimen/_15sdp"
            android:text="@string/app_name"
            android:textColor="@android:color/white"
            android:textSize="@dimen/_15sdp" />


    </LinearLayout>

</FrameLayout>

所以现在 DecoratedBarcodeView 用作布局中的主要扫描区域。

-初始化条形码视图如下:

private DecoratedBarcodeView barcodeView;

barcodeView = (DecoratedBarcodeView) findViewById(R.id.view_scanner);
barcodeView.setStatusText("");
barcodeView.decodeContinuous(callback);

-在您的 activity 中,您可以通过以下方式在 BarcodeCallback 中获取扫描结果:

private BarcodeCallback callback = new BarcodeCallback() {
    @Override
    public void barcodeResult(BarcodeResult result) {
        //Process your scan result here
        String resultString = result.getText();
    }

    @Override
    public void possibleResultPoints(List<ResultPoint> resultPoints) {
    }
};

希望对您有所帮助。