How To Make Generate QR Code with ZXing in android studio

How to make Generate QR Code with ZXing in android studio 

Assaamu'laikum Wr. Wb.

Good Morning all, Alhamdulillah this time I will get my latest article that is about Make QR Code with ZXing in Android Studio, why I make this article, because I was there work that I hold is making ID Card and using QR Code. For making QR Code this time is very simple and can be read easily understood.

1.  We just make our project in android studio, with the name that you want.


2. After we make our project ZXing plugin module into the application module.


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:26.+'    compile 'com.android.support.constraint:constraint-layout:1.0.2'    compile 'com.google.zxing:core:3.2.1'
    compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
    testCompile 'junit:junit:4.12'}

3. After we add please sync first after that we create the view in activity_main.xml. Please copy the code below to view activity_main.xml.

<?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:padding="16dp"
    tools:context="com.sahidu.android.makeqrbarcode.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Generate" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName" />
</RelativeLayout>



4. After we create a view to main activity we create a new activity to display its QR Code
according to the name you want here I use the name QrActivity and enter its QrActivity view

<?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:padding="16dp"
    tools:context="com.sahidu.android.makeqrbarcode.QrActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

5. After making the display Qr Codenya we make the process of making Qr codenya. Please go to MainActivity.class
and enter the code below into your MainActivity.class


package com.sahidu.android.makeqrbarcode;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.journeyapps.barcodescanner.BarcodeEncoder;

public class MainActivity extends AppCompatActivity {

    private Button generate;
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Context context = this;
        editText = (EditText) this.findViewById(R.id.editText);
        generate = (Button) this.findViewById(R.id.button);
        generate.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View view) {
                String textQr = editText.getText().toString();
                MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
                try {
                    BitMatrix bitMatrix = multiFormatWriter.encode(textQr, BarcodeFormat.QR_CODE, 200, 200);
                    BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
                    Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
                    Intent intent = new Intent(context, QrActivity.class);
                    intent.putExtra("pic", bitmap);
                    context.startActivity(intent);
                } catch (WriterException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}


6. After completion of the Qr codenya process creation, then we will make the process appear Qr codenya in QrActivity.class and enter the code below into QrActivity.class in it.

 
package com.sahidu.android.makeqrbarcode;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class QrActivity extends AppCompatActivity {

    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qr);

        imageView = (ImageView) findViewById(R.id.imageView);
        Bitmap bitmap = getIntent().getParcelableExtra("pic");
        imageView.setImageBitmap(bitmap);

    }
}

7.Run your application on your virtual device or it can be on your phone.

Just enough for my article this time, hopefully useful and can help you to make Qr Code in android and do not forget to read other articles to add insight and science you thank for the visit: D
Do not forget to share in your social media to help others as well :)

0 komentar