步驟1:包含的build.gradle依賴
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.journeyapps:zxing-android-embedded:3.5.0'
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
}
第2步:在OnCreateView,讓一個按鈕被點擊來啓動的掃描二維碼
scan_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(getActivity());
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt("Please focus the camera on the QR Code");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
步驟3:在父項活動
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if(scanResult != null){
Toast.makeText(this, " >>>>"+scanResult.toString(), Toast.LENGTH_LONG).show();
Log.e(">>>>"," "+scanResult.getContents().toString());
}
}
現在qr代碼的解碼內容出現在日誌文件中,並作爲祝酒!
如何在片段本身使用回調 –