ZXing barcode scanner in custom layout in fragment

落爺英雄遲暮 提交于 2019-11-28 20:46:13

It's so easy, owner of ZXing said that only add following code into onResume and onPause overrided method:

@Override
public void onResume() {
    barcodeView.resume();
    super.onResume();
}

@Override
public void onPause() {
    barcodeView.pause();
    super.onPause();
}

Try to use the minimal library of zxing like following link: https://github.com/andreipro/zxing-android-minimal

It's easy. You just have to add these lines into your gradle

repositories {
    mavenCentral()

    maven {
        url "https://raw.github.com/embarkmobile/zxing-android-minimal/mvn-repo/maven-repository/"
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v13:22.2.0'

    // Zxing minimal libraries
    compile 'com.embarkmobile:zxing-android-minimal:2.0.0@aar'
    compile 'com.embarkmobile:zxing-android-integration:2.0.0@aar'
    compile 'com.google.zxing:core:3.0.1'
}

And then just call the barcode using this

new IntentIntegrator(this).initiateScan(); // `this` is the current Activity

In you case, you wanna use a custom layout, so you must create your custom layout following some parameters, such as the layout below:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <SurfaceView android:id="@+id/zxing_preview_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

    <com.google.zxing.client.android.ViewfinderView
        android:id="@+id/zxing_viewfinder_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

    <LinearLayout
        android:id="@+id/zxing_result_view"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/zxing_result_view"
        android:visibility="gone"
        android:baselineAligned="false">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="@dimen/zxing_standard_padding">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:gravity="right|center_vertical">

                <ImageView android:id="@+id/zxing_barcode_image_view"
                    android:layout_width="160dip"
                    android:layout_height="wrap_content"
                    android:maxWidth="160dip"
                    android:maxHeight="160dip"
                    android:layout_marginBottom="@dimen/zxing_half_padding"
                    android:adjustViewBounds="true"
                    android:scaleType="centerInside"
                    tools:ignore="ContentDescription"/>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <TextView android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/zxing_msg_default_format"
                        android:textColor="@color/zxing_result_minor_text"
                        android:textStyle="bold"
                        android:paddingRight="@dimen/zxing_half_padding"/>

                    <TextView android:id="@+id/zxing_format_text_view"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/zxing_result_minor_text"/>

                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <TextView android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/zxing_msg_default_type"
                        android:textColor="@color/zxing_result_minor_text"
                        android:textStyle="bold"
                        android:paddingRight="@dimen/zxing_half_padding"/>

                    <TextView android:id="@+id/zxing_type_text_view"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/zxing_result_minor_text"/>

                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <TextView android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/zxing_msg_default_time"
                        android:textColor="@color/zxing_result_minor_text"
                        android:textStyle="bold"
                        android:paddingRight="@dimen/zxing_half_padding"/>

                    <TextView android:id="@+id/zxing_time_text_view"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/zxing_result_minor_text"/>

                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <TextView android:id="@+id/zxing_meta_text_view_label"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/zxing_msg_default_meta"
                        android:textColor="@color/zxing_result_minor_text"
                        android:textStyle="bold"
                        android:paddingRight="@dimen/zxing_half_padding"/>

                    <TextView android:id="@+id/zxing_meta_text_view"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/zxing_result_minor_text"/>

                </LinearLayout>

            </LinearLayout>

            <ScrollView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView android:id="@+id/zxing_contents_text_view"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/zxing_result_text"
                        android:textColorLink="@color/zxing_result_text"
                        android:textSize="22sp"
                        android:paddingLeft="12dip"
                        android:autoLink="web"
                        android:textIsSelectable="true"/>

                    <TextView android:id="@+id/zxing_contents_supplement_text_view"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/zxing_result_text"
                        android:textColorLink="@color/zxing_result_text"
                        android:paddingLeft="12dip"
                        android:autoLink="web"
                        android:textIsSelectable="true"/>

                </LinearLayout>

            </ScrollView>

        </LinearLayout>

        <LinearLayout android:id="@+id/zxing_result_button_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">

            <Button style="@style/zxing_ResultButton"
                android:visibility="gone"/>

            <Button style="@style/zxing_ResultButton"
                android:visibility="gone"/>

            <Button style="@style/zxing_ResultButton"
                android:visibility="gone"/>

            <Button style="@style/zxing_ResultButton"
                android:visibility="gone"/>

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_gravity="bottom|center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView android:id="@+id/zxing_status_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:background="@color/zxing_transparent"
            android:text="@string/zxing_msg_default_status"
            android:textColor="@color/zxing_status_text"/>

        <Button android:id="@id/zxing_back_button"
            android:layout_marginTop="10dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:height="60dp"
            android:textAlignment="center"
            android:layout_gravity="bottom|center_horizontal"
            android:text="@string/zxing_button_cancel"/>

    </LinearLayout>

</merge>

After that, you can set the above layout into integrator like

IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setCaptureLayout(R.layout.custom_layout);
integrator.initiateScan();

But remember, you have to follow those parameters. It means that you have to use the same name for all the views.

You must initialize the barcodeView.

Try this code:

IntentIntegrator integrator = IntentIntegrator.forSupportFragment(this);
barcodeView.initializeFromIntent(integrator.createScanIntent());

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

Does your app have permission to use the device camera? Add that permission in your manifest, and then after installing the app, go to your phone settings > apps > your app > permissions. And then grant the camera permission

I had this same issue (black screen, i.e, no camera output on the screen). This is because you donot have permission. I solved this by asking permission ,refer this https://developer.android.com/training/permissions/requesting.

I used https://github.com/dlazaro66/QRCodeReaderView ,David Lazaro's code which is a modification of Zxing library.

I had the same issue I just removed

    android:hardwareAccelerated="false"
    android:largeHeap="true"

from the manifest and it worked!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!