ステップ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で、ボタンはのスキャンを開始するためにクリックさせqrコード
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コードのデコードされた内容がログファイルに表示され、トーストとして表示されます。
フラグメント自体にコールバックを使用する方法 –