一尘不染

如何在Android中扫描QRCode

java

我找到有关如何扫描条形码的教程。但是在我的应用程序中,我必须扫描QR码。如何在Android中扫描QR码?


阅读 391

收藏
2020-12-03

共1个答案

一尘不染

try {
    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
    intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); // "PRODUCT_MODE for bar codes

    startActivityForResult(intent, 0);
} catch (Exception e) {    
    Uri marketUri = Uri.parse("market://details?id=com.google.zxing.client.android");
    Intent marketIntent = new Intent(Intent.ACTION_VIEW,marketUri);
    startActivity(marketIntent);
}

和在 onActivityResult():

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String contents = data.getStringExtra("SCAN_RESULT");
        }
        if(resultCode == RESULT_CANCELLED){
            //handle cancel
        }
    }
}
2020-12-03