如何始终显示条形码扫描屏幕?

时间:2017-11-28 09:26:23

标签: android barcode-scanner

如何在android中显示条形码扫描屏幕。

$orderlists = Orderlist::whereMonth('currentdate', 'LIKE','%'.$month.'%')
                                ->whereYear('currentdate', 'LIKE', '%'.$year.'%')
                                ->get();

1 个答案:

答案 0 :(得分:1)

首先,您需要在FrameLayout添加layout.xml作为barcodeScannet的容器​​:

<FrameLayout
        android:id="@+id/barcode_scanner"
        android:layout_width="match_parent"
        android:layout_height="350dp" />

之后,您需要在您的活动或片段中实施ZXingScannerView.ResultHandler

然后您需要将扫描仪添加到此视图

private ZXingScannerView mScannerView;

ViewGroup v = (ViewGroup) mainView.findViewById(R.id.barcode_scanner);
mScannerView = new ZXingScannerView(getActivity());
    v.addView(mScannerView);

您还需要覆盖onResumeonPause才能开始停止相机:

@Override
public void onResume() {
    super.onResume();
    mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
// aslso check for camera permission here too
}

@Override
public void onPause() {
    mScannerView.stopCamera();           // Stop camera on pause
    super.onPause();
}

然后:

@Override
public void handleResult(Result rawResult) {

    AppLog.logE("result content", rawResult.getText()); // Prints scan results
    AppLog.logE("result name", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)

   // the resault of barcode will be given as an string rawResult.getText()
   // and you can do whatEver you want with it
   // handleBarcodeResult(rawResult.getText());

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            mScannerView.resumeCameraPreview(BillPaymentFragment.this);
        }
    }, 2000);
}

最后,只要您想启动条形码扫描器,只需调用以下代码:

mScannerView.startCamera();