【问题标题】:ZXing barcode scanner in custom layout in fragmentZXing 条码扫描器在片段中自定义布局
【发布时间】:2015-09-17 09:41:06
【问题描述】:

我在Android Studio上跟随this page开发了ZXing条码连续扫描器。

我的应用程序build.gradle 包含:

repositories {
    mavenCentral()

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

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile files('src/main/jniLibs/scanditsdk-android-4.7.5.jar')
    compile files('src/main/jniLibs/httpclient-4.0.jar')

    compile 'com.journeyapps:zxing-android-embedded:3.0.3@aar'
    compile 'com.google.zxing:core:3.2.0'
}

还有我的Fragment.xml 的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#00CC00"
    android:orientation="vertical"
    android:weightSum="100">

    <com.journeyapps.barcodescanner.CompoundBarcodeView
        android:id="@+id/barcode_scanner"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40"
        >

    </com.journeyapps.barcodescanner.CompoundBarcodeView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:weightSum="100"
        style="?android:attr/buttonBarStyle"
        >


        <Button
            android:id="@+id/btnStartScan"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="25"
            android:text="Start"
            android:background="@drawable/buttonstyle"
            style="@style/button_style"/>

        <Button
            android:id="@+id/btnStopScan"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="25"
            android:text="Stop"
            android:background="@drawable/buttonstyle"
            style="@style/button_style"/>

        <Button
            android:id="@+id/btnPauseScan"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="25"
            android:text="Pause"
            android:background="@drawable/buttonstyle"
            style="@style/button_style"/>

        <Button
            android:id="@+id/btnResumeScan"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="25"
            android:text="Resume"
            android:background="@drawable/buttonstyle"
            style="@style/button_style"/>
    </LinearLayout>

</LinearLayout>

然后,我的 Fragment 代码如下所示:

public class CMCSMOFragment extends Fragment implements View.OnClickListener {

    private CompoundBarcodeView barcodeView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }

        View v;
        v = inflater.inflate(R.layout.cmcsmo_layout, container, false);

        barcodeView = (CompoundBarcodeView) v.findViewById(R.id.barcode_scanner);
        barcodeView.decodeContinuous(callback);

        return v;
    }

    private BarcodeCallback callback = new BarcodeCallback() {
        @Override
        public void barcodeResult(BarcodeResult result) {
            if (result.getText() != null) {
                barcodeView.setStatusText(result.getText());
            }

            //Do something with code result
        }

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

当我构建我的应用程序时,CompoundBarcodeView 只显示带有 ZXing 文本的 黑色视图

在取景器矩形内放置条形码以进行扫描。

编辑:

遵循 Lennon 的建议,我使用了zxing-minimum,但它不允许在纵向模式下工作:(。

我应该如何解决这个问题?感谢大家的帮助!

【问题讨论】:

  • 为什么要实现没有onclick功能的view.onClicklistener?
  • 我的课太长了,所以我只是为我的问题发布代码。

标签: android android-fragments zxing barcode-scanner


【解决方案1】:

很简单,ZXing的老板说只需要在onResumeonPause覆盖的方法中添加以下代码:

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

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

【讨论】:

  • 我复制了你所有的代码,但我仍然只是黑屏
  • 更多详情请关注https://github.com/journeyapps/zxing-android-embedded
【解决方案2】:

你必须初始化barcodeView。

试试这个代码:

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

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

【讨论】:

    【解决方案3】:

    尝试使用zxing的最小库,如下链接:https://github.com/andreipro/zxing-android-minimal

    这很容易。您只需将这些行添加到您的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'
    }
    

    然后只需使用此调用条形码

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

    在您的情况下,您想使用自定义布局,因此您必须根据一些参数创建自定义布局,例如以下布局:

    <?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>
    

    之后就可以将上面的布局设置成integratorlike

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

    但请记住,您必须遵循这些参数。这意味着您必须为所有视图使用相同的名称。

    【讨论】:

    • 我已经做到了。非常感谢。但是我可以在纵向屏幕上使用 ZXing 最小化吗?
    • 没有。不幸的是,我尝试在肖像上使用它,但没有成功。
    【解决方案4】:

    您的应用是否有权使用设备摄像头? 在您的清单中添加该权限,然后在安装应用程序后,转到您的手机设置 > 应用程序 > 您的应用程序 > 权限。 然后授予相机权限

    【讨论】:

      【解决方案5】:

      我刚刚删除了同样的问题

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

      从清单中,它起作用了!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-28
        • 1970-01-01
        • 2013-04-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多