0

I'm using zxing android to san QR code.

To initialise scan object in HomeFragment in onCreateView:

//intializing scan object
//qrScan = new IntentIntegrator(this.getActivity()); // this is for activity
qrScan = IntentIntegrator.forSupportFragment(this); // this is for fragment

To get the scanner result:

// Get the results:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
    if(result.getContents() == null) {
        Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
    }
} else {
    super.onActivityResult(requestCode, resultCode, data);
}

However, I'm getting:

error: onActivityResult(int,int,Intent) in HomeFragment cannot override onActivityResult(int,int,Intent) in Fragment attempting to assign weaker access privileges; was public

After changing it from "protected" to public, onActivityResult was not called in HomeFragment.

user1872384
  • 6,886
  • 11
  • 61
  • 103
  • https://stackoverflow.com/questions/20013213/zxing-onactivityresult-not-called-in-fragment-only-in-activity – AskNilesh Nov 28 '18 at 05:09
  • 1
    Have you overridden `onActivityResult()` in the hosting `Activity`? If so, are you calling the `super` method there? – Mike M. Nov 28 '18 at 05:10
  • Thanks Mike M. I've missed out calling the super method in the Activity. Able to capture the QR code content. However it's rather hard for me to scan a simple QR code ("hello world android" text), is there any ways to improve the speed of capturing? I've added "android:hardwareAccelerated="true"" in AndroidManifest.xml but it's still slow. – user1872384 Nov 28 '18 at 05:30
  • Sorry, but that's a separate question. I've not used ZXing. I just checked your question for the `Fragment` result issue. – Mike M. Nov 28 '18 at 05:41
  • 1
    Thanks @MikeM. Found out it's cause by the debugger. Running smoothly now – user1872384 Nov 28 '18 at 05:42

1 Answers1

0

Try this

IntentIntegrator scanIntegrator = IntentIntegrator.forSupportFragment(YourFragmentClass.this);
scanIntegrator.setPrompt("Scan");
scanIntegrator.setBeepEnabled(true);
scanIntegrator.setBarcodeImageEnabled(true);
scanIntegrator.initiateScan();

Then in onActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (scanningResult != null) {
        if (scanningResult.getContents() != null) {
            scanContent = scanningResult.getContents().toString();
            scanFormat = scanningResult.getFormatName().toString();
        }

        Toast.makeText(getActivity(), scanContent + "   type:" + scanFormat, Toast.LENGTH_SHORT).show();

        textView.setText(scanContent + "    type:" + scanFormat);

    } else {
        Toast.makeText(getActivity(), "Nothing scanned", Toast.LENGTH_SHORT).show();
    }
}

You can check this repo

user1872384
  • 6,886
  • 11
  • 61
  • 103
Mithun Sarker Shuvro
  • 3,902
  • 6
  • 32
  • 64