how to start Zxing on a Fragment?

孤街浪徒 提交于 2021-02-06 09:28:08

问题


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 >

new IntentIntegrator(this).initiateScan(); // opens up Scan intent > ZXING

how do i do that line but to open up the scan on a fragment ?

Also i get the ZXING results on a reciever like this >

//results when activity enters a callback sent out to another activity
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {

how will i get them on my Fragment that i'm going to run Zxing on ?

THNX


回答1:


how do i do that line but to open up the scan on a fragment ?

Use getActivity() to pass Context in IntentIntegrator as:

new IntentIntegrator(getActivity()).initiateScan(); 

how will i get them on my Fragment that i'm going to run Zxing on ?

override onActivityResult in both Fragment container Activity with super.onActivityResult(requestCode, resultCode, data); line and in Fragment just override onActivityResult method.




回答2:


If you really need to open it in a support fragment you can use:

IntentIntegrator.forSupportFragment(MyFragment.this).initiateScan();

In your Fragment:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
  IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); 
  String barcode = result.getContents();
}

Try it!




回答3:


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!




回答4:


Following code works good. -

IntentIntegrator integrator = new IntentIntegrator(getActivity()) {
    @Override
    protected void startActivityForResult(Intent intent, int code) {
        EditorFragment.this.startActivityForResult(intent, 312); // REQUEST_CODE override
    }
};

Then you can override the onActivityResult, and everything works fine.

More info - here you go.

And then you can call your fragment's onActivityResult as

Fragment fragment = getSupportFragmentManager().findFragmentById(fragmentId);
    if(fragment instanceof ConsDetailUpdateFragment)
        ((ConsDetailUpdateFragment) fragment).onActivityResult(requestCode, resultCode, data);



回答5:


All the above answers are correct and i wanted to add how I did it.

Step 1:

implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
implementation 'com.google.zxing:core:3.2.1'

Step 2: In My activity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // This is important, otherwise the result will not be passed to the fragment
    super.onActivityResult(requestCode, resultCode, data);
}

Step 3: In my fragment

requestCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Request camera
            IntentIntegrator integrator = IntentIntegrator.forSupportFragment(ScanFrag.this);
            integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
            integrator.setPrompt("Scan QR code");
            integrator.setCameraId(0);
            integrator.setBeepEnabled(true);
            integrator.setBarcodeImageEnabled(false);
            integrator.initiateScan();
        }
    });

And I override

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if(requestCode == IntentIntegrator.REQUEST_CODE) {
        if (result != null) {
            if (result.getContents() == null) {
                Log.d("ScanFrag", "Cancelled scan");
                Toast.makeText(getContext(), "Cancelled", Toast.LENGTH_LONG).show();
            } else {
                Log.d("ScanFrag", "Scanned | " + result.getContents());
                Toast.makeText(getContext(), "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
            }
        }
    }
}

Hope it will help someone looking how to use xzing from a fragment



来源:https://stackoverflow.com/questions/37251583/how-to-start-zxing-on-a-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!