Blog Details

img
Design

How i can Android QR Code / Barcode Scanner

Spoke Right / 18 Nov, 2023

A QR code or barcode scanner is sometimes required in an android application. There are various ways to scan the QR code programmatically:

  • Using a web-based API solution:

The QR code or barcode is uploaded to the server, and the result is returned by the server.

  • Using a web-based application:

The camera is accessed to scan the QR code or barcode and the result is returned.

  • By Integrating the Mobile Vision API of Google Play Service.

Android QR Code Scanner Example:

In the below example, we will use the Mobile Vision API of Google Play Service to scan the QR code that supports the below formats of the barcode.

  • 1D barcodes: EAN-8, UPC-A, EAN-13, EAN-8, UPC-E, Code-93, Code-128, Code-39, Codabar, ITF.
  • 2D barcodes: QR Code, Data Matrix, AZTEC, PDF-417.

activity_main.xml:

In the activity_main.xml file, we will write the below code.



build.gradle:

In the build.gradle file, we will add the Google Mobile Vision API.

implementation 'com.google.zxing:core:3.2.1'
implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar'

MainActivity.java:

In the MainActivity.java file, we will add the code to call the ScannedBarcodeActivity.java class, on the button click.

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class MainActivity extends AppCompatActivity {
   Button btnBarcode;
   TextView textView;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      btnBarcode = findViewById(R.id.button);
      textView = findViewById(R.id.txtContent);
      btnBarcode.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            IntentIntegrator intentIntegrator = new IntentIntegrator(MainActivity.this);
            intentIntegrator.setDesiredBarcodeFormats(intentIntegrator.ALL_CODE_TYPES);
            intentIntegrator.setBeepEnabled(false);
            intentIntegrator.setCameraId(0);
            intentIntegrator.setPrompt("SCAN");
            intentIntegrator.setBarcodeImageEnabled(false);
            intentIntegrator.initiateScan();
         }
      });
   }
   @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_SHORT).show();
         } else {
            Log.d("MainActivity", "Scanned");
            Toast.makeText(this, "Scanned -> " + Result.getContents(), Toast.LENGTH_SHORT).show();
textView.setText(String.format("Scanned Result: %s", Result));
         }
      } else {
         super.onActivityResult(requestCode, resultCode, data);
      }
   }
}

AndroidManifest.xml:

In the AndroidManifest.xml file, we will add the below code that includes these uses-features and uses permissions.


File: AndroidManifest.xml:



   
      
         
            
            
         
      





0 comments

Warning: PHP Startup: Unable to load dynamic library 'imagick.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0