Android QR Code Scanner using third party Library

Objective : In this tutorial we will learn how to use QR code scanning using third party library ZXingScanner in Android Application.

We will follow the steps ass below.

Step-1:
               Basic introduction of the QR code scanner.

-QR stands for Quick Response code which is a two dimensional barcode (matrix codes) that allows contents to be decoded at a high speed. QR code system is invented in 1994 by japanese company Denso-Wave.

-Quick Response code is a licence free and QR have the square dots which are arranged in a grid with white color background which are easily read by the scanner or camera which are called the imaging devices.

-QR code have the different different sizes which are starts from the 21X21 pixels which is version 1,25X25 pixels is version 2 and go on up to 177X177 which is version 40.

-QR codes storing the addresses and URLS may appear in newspapers,books,business cards..etc or any type of entity which user want the information.

-QR scanner decode the code so for mobile device its use the camera for decoding the       codes.

Step-2 
             Create new Project in Android Studio go to file new Project.


Step -3   Add library into Dependencies
               Open the build.gradle file and add the following library into file

  compile ' me.dm7.barcodescanner:zxing:1.8.4';

            Add this library and click on sync now.

Step -4  Creating Layout
              - For open camera and getting the QR code decoding we will take one buttong and click on the buttong we will open the camera for getting the QR codes.

  <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="QR Scanner"
        android:layout_margin="20dp"
        android:id="@+id/scanner"
        android:background="@color/colorPrimary"
        android:padding="5dp"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintVertical_bias="0.14" />


Step-5   Add following code  in java file 
      
     You have to implement the ZXingScannerView

       public void QrScanner(View view){


        mScannerView = new ZXingScannerView(this);   // Programmatically initialize the scanner view
        setContentView(mScannerView);

        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();         // Start camera

    }



 @Override
    public void handleResult(Result rawResult) {
        // Do something with the result here

        Log.e("handler", rawResult.getText()); // Prints scan results
        Log.e("handler", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode)

        // show the scanner result into dialog box.
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Scan Result");
        builder.setMessage(rawResult.getText());
        AlertDialog alert1 = builder.create();
        alert1.show();

        // If you would like to resume scanning, call this method below:
        // mScannerView.resumeCameraPreview(this);
    }



Here You can get full code


package com.qrcode.app;

import android.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
    private ZXingScannerView mScannerView;


    private Button scannerbtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scannerbtn = (Button)findViewById(R.id.scanner);

        scannerbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                QrScanner(view);
            }
        });

    }

    public void QrScanner(View view){


        mScannerView = new ZXingScannerView(this);   // Programmatically initialize the scanner view
        setContentView(mScannerView);

        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();         // Start camera

    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();           // Stop camera on pause
    }

    @Override
    public void handleResult(Result rawResult) {
        // Do something with the result here

        Log.e("handler", rawResult.getText()); // Prints scan results
        Log.e("handler", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode)

        // show the scanner result into dialog box.
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Scan Result");
        builder.setMessage(rawResult.getText());
        AlertDialog alert1 = builder.create();
        alert1.show();

        // If you would like to resume scanning, call this method below:
        // mScannerView.resumeCameraPreview(this);
    }

}



output : 



I hope this tutorial is useful for all

         

Comments

  1. Please reply me why you kotlin use now for android app devloping what is specific reason

    ReplyDelete
    Replies
    1. The reason is yesterday i have implemented for QR code scanning so i want to post my blog and i have this only one blog for kotlin tha't why i have posted and i will post only kotlin related post not others

      Delete
  2. nternal storage capacity of this phone is 128GB, for further extendable storage, memory card is supportable in this phone . For other connectivities WLAN, Wi-Fi and Bluetooth are the other services available. scanner

    ReplyDelete

Post a Comment

Popular posts from this blog

Upload Image From Android Application To server

Kotlin Introduction