美文网首页
android扫码功能实现

android扫码功能实现

作者: 不略 | 来源:发表于2022-11-12 18:19 被阅读0次
    1.
     // 二维码扫描库
    implementation('com.journeyapps:zxing-android-embedded:4.3.0') { transitive = false }
    implementation 'com.google.zxing:core:3.3.0'
    
    2.
    <com.journeyapps.barcodescanner.DecoratedBarcodeView
            android:id="@+id/barcode_scanner"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintVertical_weight="1"
            app:zxing_scanner_layout="@layout/custom_barcode_scanner"
            app:layout_constraintTop_toTopOf="parent"
            />
    
    3.
    barcodeView = findViewById(R.id.barcode_scanner);
    Collection<BarcodeFormat> formats = Arrays.asList(BarcodeFormat.QR_CODE, BarcodeFormat.CODE_39);
    barcodeView.getBarcodeView().setDecoderFactory(new DefaultDecoderFactory(formats));
    barcodeView.initializeFromIntent(getIntent());
    CameraSettings cameraSettings = barcodeView.getCameraSettings();
    cameraSettings.setRequestedCameraId(0);
    barcodeView.setCameraSettings(cameraSettings);
    barcodeView.decodeContinuous(callback);
    
    4.
    // 扫描结果回调
        private BarcodeCallback callback = new BarcodeCallback() {
            @Override
            public void barcodeResult(BarcodeResult result) {
                if(result.getText() == null || result.getText().equals(lastText)) {
                    // Prevent duplicate scans
                    return;
                }
    
                lastText = result.getText();
    //            barcodeView.setStatusText(result.getText());
                Log.d(TAG, "scan content=" + result.getText());
    
                beepManager.playBeepSoundAndVibrate();
                //Added preview of scanned barcode
    //            ImageView imageView = findViewById(R.id.barcodePreview);
    //            imageView.setImageBitmap(result.getBitmapWithResultPoints(Color.YELLOW));
                handleScanResult(result);
            }
    
            @Override
            public void possibleResultPoints(List<ResultPoint> resultPoints) {
            }
        };
    
    
    5.
      @Override
        protected void onResume() {
            super.onResume();
            barcodeView.resume();
        }
    
        @Override
        protected void onPause() {
            super.onPause();
    
            barcodeView.pause();
        }
    
        public void pause(View view) {
            barcodeView.pause();
        }
    
        public void resume(View view) {
            barcodeView.resume();
        }
    
        public void triggerScan(View view) {
            barcodeView.decodeSingle(callback);
        }
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            return barcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
        }
    

    相关文章

      网友评论

          本文标题:android扫码功能实现

          本文链接:https://www.haomeiwen.com/subject/kjeqxdtx.html