Android----识别二维码

引用了

https://blog.csdn.net/AndroidNUll/article/details/68949233

github

https://github.com/bingoogolapple/BGAQRCode-Android

导入包名

    compile 'com.google.zxing:core:3.2.1'
    compile 'cn.bingoogolapple:bga-qrcodecore:1.1.7@aar'
    compile 'cn.bingoogolapple:bga-zxing:1.1.7@aar'

引入权限

    
    
    
    
    
    

布局代码


    

使用代码


public class ScanActivity extends AppCompatActivity {

    private ZXingView zxingview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);

        zxingview = findViewById(R.id.zxingview);
        zxingview.setDelegate(new QRCodeView.Delegate() {
            @Override
            public void onScanQRCodeSuccess(String result) {
                Toast.makeText(ScanActivity.this, "识别结果" + result, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onScanQRCodeOpenCameraError() {
                Toast.makeText(ScanActivity.this, "识别错误", Toast.LENGTH_SHORT).show();
            }
        });
    }

    protected void onStart() {
        super.onStart();
        //打开后置摄像头预览,但并没有开始扫描
        zxingview.startCamera();
        //开启扫描框
        zxingview.showScanRect();
        zxingview.startSpot();
    }

    @Override
    protected void onStop() {
        zxingview.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        zxingview.onDestroy();
        super.onDestroy();
    }
}

补充说明

1.切换到条形码扫描
zxingview.changeToScanBarcodeStyle();
2.切换到二维码扫描
zxingview.changeToScanQRCodeStyle();
3.打开关闭闪光灯
zxingview.openFlashlight(); 
zxingview.closeFlashlight();

你可能感兴趣的:(Android----识别二维码)