android - fragment 中自定义布局中的 ZXing 条码扫描器

标签 android android-fragments zxing barcode-scanner

我根据this page开发了ZXing条码连续扫描仪|在 Android Studio 上。

我的应用 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 文本的黑色 View :

Place a barcode inside the viewfinder rectangle to scan it.

编辑:

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

我应该怎么做才能解决这个问题?感谢所有帮助!

最佳答案

很简单,ZXing的老板说只要在onResumeonPause重写的方法中加入如下代码即可:

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

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

关于android - fragment 中自定义布局中的 ZXing 条码扫描器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32627145/

相关文章:

android - 在Android上播放阵列中的声音

android - 如何通过点击号码调用电话

Android:Fragment与直接在其之上的另一个Fragment之间的通信

android - 如何从android中的子 fragment 更新 fragment 中的 View

android - zxing 相机人像模式和风景在 android 上

android - 如何在 Android 中显示来自字符串的一维条形码?

android - 图像格式RGB888和ARGB8888的区别

Android 方向更改 : Different Layout, 相同的 fragment

ios - ios 7.1 上的 zxingwidget 构建错误

android - 更新 Android Studio 代码模板