Skip to main content

Free Source Code PREMIUM BARCODE Reader Project Android studio

To provide convenience and convenience for Android users, there are millions of apps available in the play store. The Android barcode reader is an application that allows users to read barcodes. This tutorial explains how you can create a barcode reader app for Android.

WHAT IS A BARCODE READER?


Barcode readers are electronic devices that can read barcodes and output on devices that display like computers or Android. With the help of Android code reader you can scan the barcode on the product. There are hundreds of android barcode reader apps available in the play store. Now you can create your own just following this tutorial. It's easy because Google has launched a free barcode reader library named ZXing that can be accessed through Intent in apps.

Source Code BARCODE Reader Project Android studio


EXAMPLE OF ANDROID BARCODE READING


Each android device has the ability to read barcodes to decode much information. This tutorial is a complete guide for creating apps on android for barcode readers. We will implement Android barcode reader app with ZXing (Zebra Crossing) library usage, which will be used to scan barcode on Android.

ZXing (Zebra Crossing) is an open source, 1D / 2D image processing library implemented in Java.


Go to here for ZXing library http://github.com/zxing

Things you need to develop android barcode reader

  • Android Studio
  • Android device
  • USB cable
  • A product with barcode
  • ZXing library

NOW FOLLOW THESE STEPS TO CREATE NEW PROJECTS

Go to Android Studio, go to File => New Project

Enter your application name and select your project directory / path / location


Click next, leave the default settings as they are and complete the process.

Download the ZXing package and include it in your project development path.

Create an Empty Activity named Main Activity

Open the open res folder acitivity_main.xml

Switch to design view

Create a linear layout, button (to start reading barcode) and text view (to display information) in this activity



HERE IS THE CODE FOR ACTIVITY_MAIN.XML

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

<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    xmlns:app = "http://schemas.android.com/apk/res-auto"
    xmlns:tools = "http://schemas.android.com/tools"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    android:paddingBottom = "@dimen/activity_vertical_margin"
    android:paddingLeft = "@dimen/activity_horizontal_margin"
    android:paddingRight = "@dimen/activity_horizontal_margin"
    android:paddingTop = "@dimen/activity_vertical_margin"
    app:layout_behavior = "@string/appbar_scrolling_view_behavior"
    tools:context = "com.example.admin.barcodereader.MainActivity"
    tools:showIn = "@layout/activity_main" >

    < LinearLayout
        android:orientation = "vertical"
        android:layout_width = "match_parent"
        android:layout_height = "match_parent"
        android:layout_centerVertical = "true"
        android:layout_alignParentLeft = "true"
        android:layout_alignParentStart = "true"
        android:weightSum = "1" >

        < TextView
            android:layout_width = "192dp"
            android:layout_height = "wrap_content"
            android:layout_marginTop = "20dp"
            android:text = "SCAN"
            android:textAlignment = "center"
            android:textSize = "30dp"
            android:id = "@+id/textView"
            android:layout_gravity = "center_horizontal"
            android:layout_weight = "0.21" />

        < Button
            android:layout_width = "187dp"
            android:layout_height = "wrap_content"
            android:text = "Bar Code"
            android:textSize = "20dp"
            android:id = "@+id/button"
            android:layout_gravity = "center_horizontal"
            android:layout_weight = "0.08"
            android:onClick = "ScanBar" />
    </ LinearLayout >
</ RelativeLayout >

============================================================

So now we have to code it, implement onClick() function in MainActivity.java class

To implement this code we need Zebra Cross library  at com.google.zxing.client.android

HERE IS THE COMPLETE CODE OF MAINACTIVITY.JAVA

=======================================================================
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    static final String SCAN = "com.google.zxing.client.android";
    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        Toolbar toolbar = ( Toolbar ) findViewById( R.id.toolbar );
        setSupportActionBar( toolbar );

        FloatingActionButton fab = ( FloatingActionButton ) findViewById( R.id.fab );
        fab.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick( View view ) {
                Snackbar.make( view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction( "Action", null ).show();
            }
        });
    }

    // fucntion to scan barcode
    public void ScanBar ( View view ) {
        try {
            //this intent is used to call start for bar code
            Intent in = new Intent( SCAN );
            in.putExtra( "SCAN_MODE", "PRODUCT_MODE" );
            startActivityForResult( in, 0 );
        } catch ( ActivityNotFoundException e) {
            showDialog( MainActivity.this,"No scanner found", "Download Scanner code Activity?"," Yes", "No" ).show();

        }
    }


    private Dialog showDialog ( final Activity act, CharSequence title,CharSequence message, CharSequence yes, CharSequence no ) {

        // a subclass of dialog that can display buttons and message
        AlertDialog.Builder download = new AlertDialog.Builder( act );
        download.setTitle( title );
        download.setMessage ( message );
        download.setPositiveButton ( yes, new DialogInterface.OnClickListener ( ) {
            @Override
            public void onClick( DialogInterface dialog, int i ) {
                // TODO Auto-generated method stub
                //uri to download barcode scanner
                Uri uri = Uri.parse( "market://search?q=pname:" + "com.google.zxing.client.android" );
                Intent in = new Intent ( Intent.ACTION_VIEW, uri );
                try {
                    act.startActivity ( in );
                } catch ( ActivityNotFoundException e) {

                }
            }
        });
            download.setNegativeButton ( no, new DialogInterface.OnClickListener() {
                @Override
                public void onClick ( DialogInterface dialog, int i ) {
                    // TODO Auto-generated method stub
                }
            });
            return download.show();
    }

    @Override
    protected void onActivityResult ( int requestCode, int resultCode, Intent in ) {
        // TODO Auto-generated method stub
        if( requestCode == 0 ){
            if( resultCode == RESULT_OK ){
                //use to get scan result
                String contents = in.getStringExtra( "SCAN_RESULT" );
                String format =  in.getStringExtra( "SCAN_RESULT_FORMAT" ) ;
                Toast toast = Toast.makeText( this, "Content:" + contents + " Format:" + format, Toast.LENGTH_LONG );
                toast.show();
            }
        }
    }
}


========================================================================

After completing the code section now run your application in the emulator to check but it will not work for the emulator because the emulator does not have a scanner device.

So plug in a USB cable and create an apk file for your Android device. This will work fine for your Android device.

By importing ZXing integration classes into our apps, we can make user scans easier and focus our development efforts on handling scan results.

CONCLUSION
In this tutorial we explain how to create a barcode scanner application for your Android device. Since we use ZXing library so there's no need to install a barcode scanner on our device, ZXing will do this work for us.

There are many other ways to create a barcode reader application but this one is the simplest. You can now scan, decode barcodes of any product.

You can use this feature in many other applications, often in building complex Android apps, we need to read barcodes, and you can integrate this code into your app.


Comment Policy: Silahkan tuliskan komentar Anda yang sesuai dengan topik postingan halaman ini. Komentar yang berisi tautan tidak akan ditampilkan sebelum disetujui.
Buka Komentar
Tutup Komentar
-->