0

I'm implementing a feature in an android app that scans a QR code and does some stuff with the data returned.

My current solution is to request the scan activity from the zxing package.

Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.setPackage("com.google.zxing.client.android");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, REQUEST_QR_SCAN);

However when the app isn't present on a user's phone, this will not work and causes an error. I want to avoid checking if the user has this specific app installed, as they may have opted for a different QR Scanner.

I can't seem to find a way of making the intent say "I want to scan a QR code" and then allowing the user to choose a suitable app. e.g. as they would if selecting an image from a gallery app.

Are there any solutions?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Mrshll1001
  • 347
  • 4
  • 18

2 Answers2

1

You can use the ZXing lib on your project, add it trough gradle (unfortunatelly only ports with 3rd-pt is available) How to integrate ZXing Library to Android Studio for Barcode Scanning? or wrap yourself the library inside the application and declare the Activity into the AndroidManifest.XML

Then your intent will be for your own application.

Also, you can open google play on the ZXing app so the user installs it.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
0

This example using the library - https://github.com/dlazaro66/QRCodeReaderView

 public class PayQR extends Fragment implements QRCodeReaderView.OnQRCodeReadListener {

    public static QRCodeReaderView mydecoderview;
    public static String QRData;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pay_qr, container, false);
        getActivity().setTitle(R.string.Pay_QR);
        try {
            mydecoderview = (QRCodeReaderView) view.findViewById(R.id.qrdecoderview);
            mydecoderview.setOnQRCodeReadListener(this);

            // Use this function to enable/disable decoding
            mydecoderview.setQRDecodingEnabled(true);

            // Use this function to change the autofocus interval (default is 5 secs)
            mydecoderview.setAutofocusInterval(2000L);

            // Use this function to enable/disable Torch
            mydecoderview.setTorchEnabled(true);

            // Use this function to set front camera preview
            mydecoderview.setFrontCamera();

            // Use this function to set back camera preview
            mydecoderview.setBackCamera();
        }catch (RuntimeException ex){

        }
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        mydecoderview.startCamera();
    }

    @Override
    public void onQRCodeRead(String text, PointF[] points) {
        try{
        QRData = text.replaceAll("\u00A0"," ");
        Fabric.with(getActivity(), new Crashlytics());
        mydecoderview.stopCamera();
        getActivity().setTitle(R.string.Order_payment);
        }
        catch (RuntimeException ex){

        }
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
Ihor Tovkach
  • 124
  • 1
  • 1
  • 11