利用com.journeyapps:zxing-android-embedded:3.6.0实现扫码
在配置文件中进行如下配置:
implementation 'com.google.zxing:core:3.4.0'
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
给integrator添加RequestCode,如下红色
private void scan_qrcode()
{
IntentIntegrator integrator = new IntentIntegrator(ScanActivity.this);
integrator.setCaptureActivity(CaptureActivityAnyOrientation.class)
.setRequestCode(SCAN_CODE)
.setOrientationLocked(true)
.setPrompt(getString(R.string.s_scan_text))
.setBarcodeImageEnabled(false)
.setBeepEnabled(false)
.initiateScan();
}
发现在onActivityResult中怎么也获取不到二维码数据,后来发现是用错了方法。
不应该用IntentIntegrator.parseActivityResult(requestCode,resultCode, intent)
而是要用IntentIntegrator.parseActivityResult(resultCode, intent)
onActivityResult代码如下:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (resultCode == RESULT_OK)
{
if (requestCode == SCAN_CODE)
{
IntentResult scanResult = IntentIntegrator.parseActivityResult(resultCode, intent);
final String qrContent = scanResult.getContents();
tv_result.setText(qrContent);
}
}
super.onActivityResult(requestCode, resultCode, intent);
}