How to make QR code in android using EditText entered string or integer value at application run time on button click.
QR code also known as Quick Response Code obtain an array of white and black color small blocks mostly used to store or convert any kind of URL , string name and integer value into blocks mode. QR code is now very much popular in android development because with the use of it android developers can easily determine any android device and connect them with each other without typing their username or password. So here is the complete step by step tutorial for Generate QR code in android using Zxing library in Android Studio example tutorial.
What we are doing in this project : We are creating an android application with the use of Zxing library and inside the app user can just simply enter any kind of value ( URL, NAME, NUMBER ) and click on button, After click it will automatically convert the entered value to QR code.
Follow all the below steps very carefully to add Zxing library in your project.
1. Open your Project’s build.gradle ( Module : app ) and add com.android.support:appcompat-v7:23.4.0 library file.
2. Here is the code to add appcompat library inside build.gradle file.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.google.zxing:core:3.2.1'
}
Screenshot after add this code into build.gradle file.
Here you go now our library is successfully imported next step is to start coding.
How to Generate QR code in android using Zxing library in Android Studio example tutorial.
Code for MainActivity.java file.
package com.android_examples.generateqrcode_android_examplescom; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import com.google.zxing.BarcodeFormat; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; public class MainActivity extends AppCompatActivity { ImageView imageView; Button button; EditText editText; String EditTextValue ; Thread thread ; public final static int QRcodeWidth = 500 ; Bitmap bitmap ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView)findViewById(R.id.imageView); editText = (EditText)findViewById(R.id.editText); button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { EditTextValue = editText.getText().toString(); try { bitmap = TextToImageEncode(EditTextValue); imageView.setImageBitmap(bitmap); } catch (WriterException e) { e.printStackTrace(); } } }); } Bitmap TextToImageEncode(String Value) throws WriterException { BitMatrix bitMatrix; try { bitMatrix = new MultiFormatWriter().encode( Value, BarcodeFormat.DATA_MATRIX.QR_CODE, QRcodeWidth, QRcodeWidth, null ); } catch (IllegalArgumentException Illegalargumentexception) { return null; } int bitMatrixWidth = bitMatrix.getWidth(); int bitMatrixHeight = bitMatrix.getHeight(); int[] pixels = new int[bitMatrixWidth * bitMatrixHeight]; for (int y = 0; y < bitMatrixHeight; y++) { int offset = y * bitMatrixWidth; for (int x = 0; x < bitMatrixWidth; x++) { pixels[offset + x] = bitMatrix.get(x, y) ? getResources().getColor(R.color.QRCodeBlackColor):getResources().getColor(R.color.QRCodeWhiteColor); } } Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444); bitmap.setPixels(pixels, 0, 500, 0, 0, bitMatrixWidth, bitMatrixHeight); return bitmap; } }
Code for activity_main.xml layout file.