0

I'm new to Android development and currently working on an app which lets the user scan the QR code using either camera or choosing a picture from Gallery using Barcode scanning kit. The app works completely fine for Android 9 (Api level 28) and above. The app works fine for all versions while scanning using camera. But for android 8 and below, I'm unable to scan any QR code if I choose a picture from gallery. I searched everywhere but didn't find any solution.

Here's the code I tried: -

mChoosePhoto = registerForActivityResult(new ActivityResultContracts.GetContent(), result -> {
    InputImage image;
    BarcodeScannerOptions options =
            new BarcodeScannerOptions.Builder()
                    .setBarcodeFormats(Barcode.FORMAT_QR_CODE)
                    .build();
    BarcodeScanner scanner = BarcodeScanning.getClient(options);
    Log.v("myUri", String.valueOf(result));
    try {
        image = InputImage.fromFilePath(this, result);
        scanner.process(image)
            .addOnSuccessListener(MainActivity.this::processResult)
            .addOnFailureListener(e -> {
                // Task failed with an exception
                Toast.makeText(MainActivity.this, "Failed to scan.", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            });
    } catch (IOException e) {
        e.printStackTrace();
    }
});
And then in processResult() Method

private void processResult(List<Barcode> barcodes){
        for (Barcode barcode: barcodes) {

            Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(100);

//            Rect bounds = barcode.getBoundingBox();
//            Point[] corners = barcode.getCornerPoints();
//
//            String rawValue = barcode.getRawValue();

            int valueType = barcode.getValueType();
            switch (valueType) {
                case Barcode.TYPE_WIFI:
                    String ssid = barcode.getWifi().getSsid();
                    String password = barcode.getWifi().getPassword();
                    int type = barcode.getWifi().getEncryptionType();
                    break;
                case Barcode.TYPE_URL:
                    String title = barcode.getUrl().getTitle();
                    String url = barcode.getUrl().getUrl();
                    Toast.makeText(this, title + " " + url, Toast.LENGTH_SHORT).show();
                    break;
                case Barcode.TYPE_TEXT:
                    String text = barcode.getDisplayValue();
                    Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
            }
        }
    }
Yatik
  • 66
  • 6
  • Please show the used intent and the launch(). – blackapps Oct 26 '22 at 15:45
  • `InputImage.fromFilePath(this, result);` That does not look correct. – blackapps Oct 26 '22 at 15:48
  • @blackapps thanks for replying. Look at this [link](https://developers.google.com/ml-kit/vision/barcode-scanning/android#using-a-file-uri) as they clearly mentioned the way to convert Uri into InputImage. "result" is the content style Uri e.g. content://com.android.providers.media.documents/document/image%3A328944 – Yatik Oct 26 '22 at 16:54
  • @blackapps here's the launch(): - `binding.selectFromGallery.setOnClickListener(view -> mChoosePhoto.launch("image/*"));` – Yatik Oct 26 '22 at 16:56
  • `ActivityResultLauncher mChoosePhoto;` – Yatik Oct 26 '22 at 17:01
  • You should put that and all code in your post of course. – blackapps Oct 26 '22 at 19:30
  • `I'm unable to scan any QR code if I choose a picture from gallery.` Too vague. What happens exactly? Or what exactly does not happen. Does an InputImage display the picture normally? At which statement things go wrong? – blackapps Oct 26 '22 at 19:38
  • @blackapps yea I should have added that code. Okay lemme explain assuming two scenarios. Suppose I'm using android 9 or above, I clicked the button to choose QR from Gallery. It'll return me the `result` Uri and then in **try** block it'll convert Uri into InputImage and then [here](https://gist.github.com/yatiksihag01/d3554c8c6a66ae86c645876d382a26cd/#file-mainactivity-java-L24-L30) it returns me the `List barcodes` of size one ofcousre. But for android 8.1 or below, I get `result` Uri, in **try** block it convert it into InputImage `image` but then... – Yatik Oct 27 '22 at 03:32
  • ... [here](https://gist.github.com/yatiksihag01/d3554c8c6a66ae86c645876d382a26cd/#file-mainactivity-java-L24-L30) it returns me empty List barcodes and I've no idea why this returns empty List of barcodes even when the image contains a QR Code. The same is working completely fine in Android 9 and above. – Yatik Oct 27 '22 at 03:35
  • Instead of 'here' you could just have said 'in onResponse()'. – blackapps Oct 27 '22 at 06:20
  • What is the resolution of the picture that goes wrong? Is it the same picture that you use in other devices? – blackapps Oct 27 '22 at 06:22
  • @blackapps thank you so much. Actually, I was using different pictures on those two devices. Then I tried the same picture which I was using in Android 9 in Android 8 and it worked. The one I was using in android 8 doesn't work in Android 9 and 12 too. The problem I found is that it scans [this](https://ibb.co/cX7bzsr) type of picture but not [this](https://ibb.co/mNR97X3) . The QR code size in both pictures is 512*512. I mean it scans if QR code doesn't fill the whole picture. Can you please give me some hint that what I should do? – Yatik Oct 27 '22 at 14:09
  • Inform Google. File a bug report. – blackapps Oct 27 '22 at 14:57
  • You could place the bitmap with qr code on a white bitmap that is slightly larger so it resembles the good one with the extra white borders. – blackapps Oct 27 '22 at 14:59
  • Hi @blackapps, I filed the bug report. But how can I place a QR code bitmap over a white image? You mean something like [bitmapOverlayToCenter()](https://stackoverflow.com/questions/10616777/how-to-merge-to-two-bitmap-one-over-another) method? – Yatik Oct 28 '22 at 04:21
  • I have no idea what that code does. But you only have to copy your bitmap over a slightly bigger white or black bitmap. – blackapps Oct 28 '22 at 06:09
  • Yes that code you can use. – blackapps Oct 28 '22 at 06:12
  • Hi @blackapps, google recognized this issue and they said they'll fix it soon. Thank you so much for your help:) – Yatik Oct 28 '22 at 17:27

0 Answers0