java Android XzingScanner:如何自定义ZxingScanner布局?(添加按钮)

5cg8jx4n  于 7个月前  发布在  Java
关注(0)|答案(1)|浏览(58)

我正在使用Zxing扫描仪实现一个条形码扫描仪,这部分的一切都在工作,但现在我想在布局上添加一个按钮(打开/关闭手电筒)。
我的问题是

  • 有没有可能找到或修改.xml文件来添加按钮?如果有,如何找到这个文件?
  • 是否已经在某个地方实现了一个功能,可以在布局中添加一些元素?

这就是我如何调用ZXingScannerView:

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

字符串

ffscu2ro

ffscu2ro1#

这是很好的自定义QR/条形码扫描器与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视图。按钮只是打开/关闭闪光灯。
这是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();
    }
}


当QR/Barcode被扫描时,您可以从BarcodeCallback.barcodeResult函数获得结果。

相关问题