Simplest and Fastest BARCODE / QR CODE Scanner Android Example

Please follow and like us:

simple QR CODE or BARCODE scanner or reader android example using google play service libraryAndroid App with Barcode / QR Code scanner functionality made easy using Google play service library. This library allows us to scan Barcodes and QRCodes quickly and locally which making it really fast in few steps. Let’s see simple QR Code scanner Android Example Step by Step…

DOWNLOAD SIMPLE QR CODE SCANNER EXAMPLE

Simple QR Code Scanner Android Example Step by Step Explained

Here the magic things which you need is

compile 'com.google.android.gms:play-services-vision:10.2.0'

Add the library in your build.gradle file

</pre>
<pre>apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.nkdroid.qrcode.scanner"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.google.android.gms:play-services-vision:10.2.0'
    testCompile 'junit:junit:4.12'
}</pre>
<pre>

Add one button in your layout file to trigger scanner screen and one textview to show reasult in it

</pre>
<pre><?xml version="1.0" encoding="utf-8"?>
<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/scan_barcode_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:background="@color/colorAccent"

        android:text="START SCAN"
        android:textColor="@android:color/white" />

    <TextView
        android:id="@+id/result_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:freezesText="true"
        android:gravity="center"
        android:text="Your Scan Result will be here"
        android:textSize="22sp" />


</LinearLayout></pre>
<pre>

QR Code capture screen layout

</pre>
<pre><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/topLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true"
    android:orientation="vertical">

    <com.nkdroid.qrcode.scanner.camera.CameraSourcePreview
        android:id="@+id/preview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
</LinearLayout></pre>
<pre>

Main logic of QR Code Scanning using Google play service library

</pre>
<pre>package com.nkdroid.qrcode.scanner;

import android.content.Intent;
import android.graphics.Point;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.vision.barcode.Barcode;
import com.nkdroid.qrcode.scanner.barcode.BarcodeCaptureActivity;

public class MainActivity extends AppCompatActivity {


    private static final int BARCODE_READER_REQUEST_CODE = 1;
    private TextView scanResult;
    private Button scanButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scanResult = (TextView) findViewById(R.id.result_textview);

        scanButton = (Button) findViewById(R.id.scan_barcode_button);
        scanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), BarcodeCaptureActivity.class);
                startActivityForResult(intent, BARCODE_READER_REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == BARCODE_READER_REQUEST_CODE) {
            if (resultCode == CommonStatusCodes.SUCCESS) {
                if (data != null) {
                    Barcode barcode = data.getParcelableExtra(BarcodeCaptureActivity.BarcodeObject);
                    Point[] p = barcode.cornerPoints;
                    scanResult.setText(barcode.displayValue);
                } else {
                    scanResult.setText("No Result Found");
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}</pre>
<pre>

Add required permission and declare required activities in your Mainifest file

</pre>
<pre><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nkdroid.qrcode.scanner">

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".barcode.BarcodeCaptureActivity"
            android:theme="@style/Theme.AppCompat.NoActionBar" />
    </application>

</manifest></pre>
<pre>

DOWNLOAD SIMPLE QR CODE SCANNER EXAMPLE

Hope you like our simple android tutorial for integrating QR Code Scanner features using Google play service library. Stay connected for upcoming new Android tutorials.

nirav kalola