How To Start Zxing On A Fragment?
Solution 1:
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!
Solution 2:
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.
Solution 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!
Solution 4:
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
Solution 5:
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);
Post a Comment for "How To Start Zxing On A Fragment?"