Java – QR codes cannot be scanned using the Zxing library

QR codes cannot be scanned using the Zxing library… here is a solution to the problem.

QR codes cannot be scanned using the Zxing library

I’m trying to integrate Zxing into my android app so that users can scan the QR code and return the content of the QR code. I was able to turn on the barcode scanner, but although it looked like it was doing something, it didn’t scan the QR code. I’ve tested it on barcodes and it works fine, so it looks like this issue is QR code specific. I’ve included some code fragments below.

list file

<pre class=”lang-xml prettyprint-override”><activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="portrait"
tools:replace="android:screenOrientation"
android:stateNotNeeded="true"/>

QR code scanner fragment

package com.example.ntuevent.ui.qrScanner;

import android. Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.ntuevent.R;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class QRScannerFragment extends Fragment implements View.OnClickListener {

private QRScannerViewModel qrScannerViewModel;

public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        qrScannerViewModel =
                ViewModelProviders.of(this).get(QRScannerViewModel.class);
        View root = inflater.inflate(R.layout.fragment_qr_scanner, container, false);
        final TextView textView = root.findViewById(R.id.text_qr_scanner);
        qrScannerViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });

root.findViewById(R.id.qr_scanner_button).setOnClickListener(this);

return root;
    }

@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.qr_scanner_button:
                /* Request camera access */
                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, 1);
                launchQrScanner();
        }
    }

private void launchQrScanner() {
        if (validateCameraPermission()) {
            /* Start the scanner */
            IntentIntegrator intentIntegrator = new IntentIntegrator(getActivity());

/* Customisation options */
            intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
            intentIntegrator.setPrompt("Scan a barcode");
            intentIntegrator.setCameraId(0);   Use a specific camera of the device
            intentIntegrator.setBeepEnabled(false);
            intentIntegrator.setOrientationLocked(true);

/* Start QR scanner */
            intentIntegrator.initiateScan();
        }
    }

private boolean validateCameraPermission() {
        /* Validates if app has access to camera */
        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getContext().getApplicationContext(), "Enable camera permissions to access this feature", Toast.LENGTH_SHORT).show();
            return false;
        }

return true;
    }

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

if (intentResult != null) {
            if (intentResult.getContents() == null)
                Toast.makeText(getContext().getApplicationContext(), "Scan was cancelled", Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(getContext().getApplicationContext(), intentResult.getContents(), Toast.LENGTH_SHORT).show();
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}

Any help would be great!

Solution

Using a QR code scanner like I did, I used this code for 2 projects and without any issues.

First, add the following libraries to your Gradle:

  implementation 'me.dm7.barcodescanner:zxing:1.9.13'
  implementation 'com.journeyapps:zxing-android-embedded:[email protected]'
  implementation 'com.google.zxing:core:3.3.3'

Secondly, add the following code to your QR code scanning activity:

private IntentIntegrator qrScan;

Add under onCreate:

    qrScan = new IntentIntegrator(this);
    qrScan.setOrientationLocked(false);
    qrScan.initiateScan();

After onCreate, add the following:

  @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   IntentResult result = IntentIntegrator.parseActivityResult(requestCode, 
   resultCode, data);
    if (result != null) {
        if (result.getContents() == null) {
         scan have an error 
        } else {
           scan is successful 
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

Related Problems and Solutions