【问题标题】:Android XzingScanner : How to customise ZxingScanner layout ? (add button)Android Zxing Scanner:如何自定义 Zxing Scanner 布局? (添加按钮)
【发布时间】:2020-01-30 13:23:06
【问题描述】:

我正在使用 Zxing Scanner 实现条形码扫描仪,这部分一切正常,但现在我想在布局上添加一个按钮(打开/关闭手电筒)。我在互联网上到处检查,但一无所获。

所以,这是我的问题:

  • 是否可以通过修改 .xml 来添加按钮?如果是,如何找到这个文件?

  • 是否已经在某处实现了在布局中添加一些元素的功能?

这就是我调用 ZXingScannerView 的方式:

scannerView = new ZXingScannerView(this);
setContentView(scannerView);

【问题讨论】:

    标签: java android xml layout zxing


    【解决方案1】:

    用 ZXing 库自定义二维码/条码扫描器很不错

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

    您可以在那里查看示例应用项目并自行自定义(添加按钮、设置闪光灯开/关)。

    还有另一个库,但它没有使用 ZXing。

    https://github.com/googlesamples/android-vision

    它已被弃用,现在是 ML Kit 的一部分,但仍然可以使用。

    希望对你有所帮助。谢谢。

    [更新]

    请将此库导入您的项目。 (您可以从下面的链接中查看如何导入它。)

    https://github.com/journeyapps/zxing-android-embedded#adding-aar-dependency-with-gradle

    导入后,您可以更新条码扫描器活动的 layout.xml。

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/topLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:keepScreenOn="true">
    
        <com.journeyapps.barcodescanner.DecoratedBarcodeView
            android:id="@+id/barcode_scanner"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center" />
    
        <Button
            android:id="@+id/btn_flash"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center"
            android:text="Flash"/>
    
    </FrameLayout>
    

    com.journeyapps.barcodescanner.DecoratedBarcodeView 是 BarcodeScanner 视图。而Button只是用来开/关闪光灯。

    这是 BarcodeScanner 的活动。

    public class ScanQRActivity extends BaseActivity {
    
        private static final String TAG = "ScanQRActivity";
    
        private DecoratedBarcodeView barcodeView;
    
        private boolean isFlashOn;
    
        /**
         * Initializes the UI and creates the detector pipeline.
         */
        @Override
        public void onCreate(Bundle bundle) {
            super.onCreate(bundle);
            setContentView(R.layout.activity_scan_qr);
    
            isFlashOn = false;
    
            barcodeView = findViewById(R.id.barcode_scanner);
            Collection<BarcodeFormat> formats = Arrays.asList(BarcodeFormat.CODE_39); // Set barcode type
            barcodeView.getBarcodeView().setDecoderFactory(new DefaultDecoderFactory(formats));
            barcodeView.initializeFromIntent(getIntent());
            barcodeView.decodeContinuous(callback);
    
            Button btnFlash = findViewById(R.id.btn_flash);
            if (!hasFlash()) {
                btnFlash.setVisibility(View.GONE);
            }
            btnFlash.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switchFlashlight();
                }
            });
        }
    
        private BarcodeCallback callback = new BarcodeCallback() {
            @Override
            public void barcodeResult(BarcodeResult result) {
                Log.e(TAG, result.getText()); // QR/Barcode result
            }
    
            @Override
            public void possibleResultPoints(List<ResultPoint> resultPoints) {
            }
        };
    
        /**
         * Check if the device's camera has a Flashlight.
         *
         * @return true if there is Flashlight, otherwise false.
         */
        private boolean hasFlash() {
            return getApplicationContext().getPackageManager()
                    .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        }
    
        public void switchFlashlight() {
            if (isFlashOn) {
                isFlashOn = false;
                barcodeView.setTorchOff();
            } else {
                isFlashOn = true;
                barcodeView.setTorchOn();
            }
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            barcodeView.resume();
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            barcodeView.pause();
        }
    }
    

    扫描二维码/条码时,可以从 BarcodeCallback.barcodeResult 函数中获取结果。

    希望对您有所帮助。 如果您有任何问题,请在此处发表评论。

    【讨论】:

    • 感谢您的回复!我检查了第一个链接,我真的不明白他们在做什么,以及本教程的目标是什么。我再看看
    • github.com/journeyapps/zxing-android-embedded/blob/master/… 这是自定义 QR 扫描仪示例活动。 github.com/journeyapps/zxing-android-embedded/blob/master/… 这是布局文件。您可以在此处添加按钮并在活动中定义 onClickListener。如果您有任何问题,请在此处发表评论。
    • 感谢您的帮助!所以我只需要这两个文件?
    • 抱歉描述不佳。让我再回答一次。
    • 以及如何更新布局等?我的意思是,导入库后如何访问这些文件?真的很抱歉:/ 编辑:我注意到在 CustomScanner 中没有“BarcodeCallback”功能我真的迷路了啊谢谢你的耐心
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    相关资源
    最近更新 更多