how to start Zxing on a Fragment?

后端 未结 5 761
无人共我
无人共我 2021-02-06 15:30

i have an activity that holds Two Fragments, i want to run ZXING scanner on one of the fragments,

currently i do this on another activity like this >

         


        
5条回答
  •  野的像风
    2021-02-06 16:11

    Step 1 : Include Dependencies in 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'
    

    }

    Step 2: in the OnCreateView, let a button be clicked to initiate the scan of the qr code

    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();
             }
        });
    

    Step 3: in the parent activity

    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());
        }
    }
    

    Now the qr code's decoded content appears in the log files and as a toast!

提交回复
热议问题