ads_header

How To Create Barcode Scanner in Android Studio


How To Create Barcode Scanner in Android Studio



Preview



Hello friends, in this post I will discuss about how to create a barcode code detection program in Android Studio

So, this program will scan a barcode using a camera and will display the results. First, open Android Studio and create a new project. When done, go to dependencies section and enter the following libraries

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'
    compile 'com.journeyapps:zxing-android-embedded:3.4.0'
}

Next, create a layout and name it activity_main, then enter the following code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:fitsSystemWindows="true"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingTop="16dp"
    android:paddingRight="16dp"
    tools:context="id.ndiappink.scannerbarcode.MainActivity">

    <Button
        android:id="@+id/buttonScan"
        android:layout_alignParentBottom="true"
        android:text="Scan Your Barcode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

Mobile Landing Page

Aura Premium Mobile Theme

AURA Mobile Theme is created with full customizable theme system. While coding this template we try to give you better mobile experience.
Aura have perfect mobile blog system with post format support. Also custom portfolio, gallery, video and Sound Cloud supports.



Get it Now

Next, create a new class and name it Portrait so that the program can be run in portrait mode. then extends with CaptureActivity

import com.journeyapps.barcodescanner.CaptureActivity;   

public class Portrait extends CaptureActivity {

}


Next, go to Main_Activity class and enter the following code

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnscan = (Button)findViewById(R.id.buttonScan);
        btnscan.setOnClickListener(this);
    }

    @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,"Result Not Found", Toast.LENGTH_SHORT).show();
            }
            else{
                AlertDialog.Builder alertdialogbuilder = new AlertDialog.Builder(this);
                alertdialogbuilder.setMessage(result.getContents()+"\n\nScan Again ?");
                alertdialogbuilder.setTitle("Result Scanned");
                alertdialogbuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {    

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        scanow();
                    }
                });
                alertdialogbuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
                AlertDialog alertDialog = alertdialogbuilder.create();
                alertDialog.show();
            }
        }
        else{
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    public void scanow(){
        IntentIntegrator integrator = new IntentIntegrator(this);
        integrator.setCaptureActivity(Portrait.class);
        integrator.setOrientationLocked(false);
        integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
        integrator.setPrompt("Scan Your Barcode");
        integrator.initiateScan();
    }

    @Override
    public void onClick(View v) {
        scanow();
    }
}





And finally, open Manifest.xml and add activity for the portrait class

<activity android:name=".Portrait"
        android:screenOrientation="fullSensor"
        android:stateNotNeeded="true"
        android:windowSoftInputMode="stateAlwaysHidden">  
</activity>

When finished, the program is ready to run
Powered by Blogger.