install android studio

How to put QR code scanner in Android

QR Code :

QR code is one of the private keys which can not be read by human eyes. QR code has the data or information inside a barcode which can be used as an image to install or put where the data is required. It is very simple and easy process because it has a barcode which can be read by the scanner and that pictures or bar code data will go directly to the system. This process has no need of human efforts to read and write manually reduces the time consumption for that work. Because of its ease, QR code is used widely throughout the world to do the tasks at higher speed.

Example: In Shopping malls, you have seen the products like groceries products, garments, utensils etc. all they have tags with them which have price list, size etc for customers to see them and they all have a barcode which is used by shopkeeper in order to do the faster billings without any mistake of quantity, price etc. and also saves the time of customers.

QR code scanner:

We can make the QR code scanner in Android studio for android mobile. The mechanism of the app works like that the user opens the app in which a square box has invoked on which camera is automatically open in that square to read the bar code And when the position is good to scan, it will immediately show the result in your text field or where you want to show it .

For implementation, you need to put some code in your android studio.so, follow the steps :

1. Permissions :

<uses-feature android:name = android.hardware.camera"/>
<uses-permission android:name"android.permission.CAMERA"/>

2. dependency :

Add this dependency to build. gradle file of the project in Android Studio.


compile 'com.journeyapps:zxing-android-embedded:3.4.0'

3. activity_main.xml :


<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:orientation="vertical"/>
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Name" />
 
        <TextView
            android:id="@+id/textViewName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="demo1"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Address" />
 
        <TextView
            android:id="@+id/textViewAddress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="demo2"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />
 
 
    </LinearLayout>

4. Mainactivity.java :

//Declaration of scanner object.
    private IntentIntegrator qrScan;

//Intializing the scanner
qrScan = new IntentIntegrator(this);
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            //if qrcode has nothing in it
            if (result.getContents() == null) {
                Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
            } else {
                //if qr contains data
                try {
                    //converting the data to json
                    JSONObject obj = new JSONObject(result.getContents());
                    //setting values to textviews
                    textViewName.setText(obj.getString("name"));
                    textViewAddress.setText(obj.getString("address"));
                } catch (JSONException e) {
                    e.printStackTrace();
                    //if control comes here
                    //that means the encoded format not matches
                    //in this case you can display whatever data is available on the qrcode
                    //to a toast
                    Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
    @Override
    public void onClick(View view) {
        //initiating the qr code scan
        qrScan.initiateScan();
    }

You have to intialize the button and textviews in MainActivity to complete this task for the practise and to understand the things.


Problems will be discussed in Comment section.

About the author

Generic Bytes

Hi! we are from Generic Bytes an aspiring bloggers with an obsession of things tech.This blog is dedicated to people who want to learn the technology.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *