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

14 comments:

  1. Not even close to working.

    ReplyDelete
  2. it work, but scanner is too long scanning. it must take a long time until the app show the result. any suggestion?

    ReplyDelete
    Replies
    1. Its A Good Question i have solution checkout these smartphone for best body scanner supported Processor inbuilt New Mobile

      Delete
  3. I am glad to read this post, it's a good one. I am always looking for quality posts and articles and this is what I found here, I hope you will be adding more in the future. Thanks for sharing. Buy Ean Barcodes

    ReplyDelete
  4. I haven’t any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. XE88

    ReplyDelete
  5. Glad to chat your blog, I seem to be forward to more reliable articles and I think we all wish to thank so many good articles, blog to share with us. Tubemate APK Here Download Latest & Updated File

    ReplyDelete
  6. It's late finding this act. At least, it's a thing to be familiar with that there are such events exist. I agree with your Blog and I will be back to inspect it more in the future so please keep up your act. Tubidy APK 2020 Latest Update Download Free

    ReplyDelete
  7. After reading this article, you will come to know much more about the functionality of this printers, their types and applications. Best bar code printer to buy online in India

    ReplyDelete
  8. I really like your take on the issue. I now have a clear idea on what this matter is all about.. portable photo scanner

    ReplyDelete
  9. This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you. we buy toner

    ReplyDelete
  10. SEO Malaysia Agency in Kuala Lumpur, Malaysia. Expert in web design, social media, SEO, & Performance marketing. Call 03-2935 9035 for consultation.

    ReplyDelete
  11. 918kiss/Kiss918 Free Download APK IOS 2021. ⚡918kiss Register Login ID online by Whatsapp⚡24/7 Customer support. ⚡Cuci 24 Jam! 918Kiss is back!

    ReplyDelete
  12. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! videochat brasov

    ReplyDelete
  13. Our experts have reviewed the best stock trading scanners and screeners on the market. Finviz Scanner Review

    ReplyDelete

Powered by Blogger.