Kotlin Generate QR Code tutorial with example is what you are learning now.
In this tutorial, we have used ZXing library to generate qr code from string.
We have also explained how you can save generated qr code in android device.
If you are making app related to grocery products or so you need to maintain unique qr code for each product.
This is where this tutorial will help you a lot.
User will enter any string which he likes and app will generate qr code for that particular string.
If you want to generate qr code using Java then follow android qr code generator example.
After implementing all the steps, you should get following output
Step 1. ZXing Dependencies
ZXing dependencies are required to be added to use their classes.
Add below line in the build.grdale(Module:app) file
compile 'com.google.zxing:core:3.2.1'
Full code will look like
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 27 defaultConfig { applicationId "com.example.parsaniahardik.kotlin_generate_qr" minSdkVersion 15 targetSdkVersion 22 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' compile 'com.google.zxing:core:3.2.1' }
Step 3. Add black and white colors
As you already know that every qr code image includes only two colors – Black and White
So let’s add these colors in the colors.xml file
Go to res->values->colors.xml directory and add following
<color name="black">#000000</color> <color name="white">#ffffff</color>
Whole code for colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="black">#000000</color> <color name="white">#ffffff</color> </resources>
Step 4. Permissions in manifest
This tutorial required read and write permissions for internal storage of android device.
Put below lines in AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
So final code for AndroidManifest.xml is like below
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.parsaniahardik.kotlin_generate_qr"> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Step 5. Updating Main Activity
Finally, change main activity codes and our example is ready.
replace your whole code for activity_main.xml with below one
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context="com.example.parsaniahardik.kotlin_generate_qr.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/etqr" android:layout_marginTop="10dp" android:layout_marginLeft="5dp" android:hint="Enter String" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn" android:text="Create Image" android:layout_marginTop="10dp"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:id="@+id/iv"/> </LinearLayout>
Now it is time to add code to generate qr code image.
Add some kotlin coding into the MainActivity.kt file
import android.graphics.Bitmap import android.media.MediaScannerConnection import android.os.Environment import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.util.Log import android.widget.Button import android.widget.EditText import android.widget.ImageView import android.widget.Toast import com.google.zxing.BarcodeFormat import com.google.zxing.MultiFormatWriter import com.google.zxing.WriterException import com.google.zxing.common.BitMatrix import java.io.ByteArrayOutputStream import java.io.File import java.io.FileOutputStream import java.io.IOException import java.util.Calendar class MainActivity : AppCompatActivity() { internal var bitmap: Bitmap? = null private var etqr: EditText? = null private var iv: ImageView? = null private var btn: Button? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) iv = findViewById(R.id.iv) as ImageView etqr = findViewById(R.id.etqr) as EditText btn = findViewById(R.id.btn) as Button btn!!.setOnClickListener { if (etqr!!.text.toString().trim { it <= ' ' }.length == 0) { Toast.makeText(this@MainActivity, "Enter String!", Toast.LENGTH_SHORT).show() } else { try { bitmap = TextToImageEncode(etqr!!.text.toString()) iv!!.setImageBitmap(bitmap) val path = saveImage(bitmap) //give read write permission Toast.makeText(this@MainActivity, "QRCode saved to -> $path", Toast.LENGTH_SHORT).show() } catch (e: WriterException) { e.printStackTrace() } } } } fun saveImage(myBitmap: Bitmap?): String { val bytes = ByteArrayOutputStream() myBitmap!!.compress(Bitmap.CompressFormat.JPEG, 90, bytes) val wallpaperDirectory = File( Environment.getExternalStorageDirectory().toString() + IMAGE_DIRECTORY) // have the object build the directory structure, if needed. if (!wallpaperDirectory.exists()) { Log.d("dirrrrrr", "" + wallpaperDirectory.mkdirs()) wallpaperDirectory.mkdirs() } try { val f = File(wallpaperDirectory, Calendar.getInstance() .timeInMillis.toString() + ".jpg") f.createNewFile() //give read write permission val fo = FileOutputStream(f) fo.write(bytes.toByteArray()) MediaScannerConnection.scanFile(this, arrayOf(f.path), arrayOf("image/jpeg"), null) fo.close() Log.d("TAG", "File Saved::--->" + f.absolutePath) return f.absolutePath } catch (e1: IOException) { e1.printStackTrace() } return "" } @Throws(WriterException::class) private fun TextToImageEncode(Value: String): Bitmap? { val bitMatrix: BitMatrix try { bitMatrix = MultiFormatWriter().encode( Value, BarcodeFormat.QR_CODE, QRcodeWidth, QRcodeWidth, null ) } catch (Illegalargumentexception: IllegalArgumentException) { return null } val bitMatrixWidth = bitMatrix.getWidth() val bitMatrixHeight = bitMatrix.getHeight() val pixels = IntArray(bitMatrixWidth * bitMatrixHeight) for (y in 0 until bitMatrixHeight) { val offset = y * bitMatrixWidth for (x in 0 until bitMatrixWidth) { pixels[offset + x] = if (bitMatrix.get(x, y)) resources.getColor(R.color.black) else resources.getColor(R.color.white) } } val bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444) bitmap.setPixels(pixels, 0, 500, 0, 0, bitMatrixWidth, bitMatrixHeight) return bitmap } companion object { val QRcodeWidth = 500 private val IMAGE_DIRECTORY = "/QRcodeDemonuts" } }
Understanding above code
Following method is responsible for QR Code generation from given string.
@Throws(WriterException::class) private fun TextToImageEncode(Value: String): Bitmap? { val bitMatrix: BitMatrix try { bitMatrix = MultiFormatWriter().encode( Value, BarcodeFormat.QR_CODE, QRcodeWidth, QRcodeWidth, null ) } catch (Illegalargumentexception: IllegalArgumentException) { return null } val bitMatrixWidth = bitMatrix.getWidth() val bitMatrixHeight = bitMatrix.getHeight() val pixels = IntArray(bitMatrixWidth * bitMatrixHeight) for (y in 0 until bitMatrixHeight) { val offset = y * bitMatrixWidth for (x in 0 until bitMatrixWidth) { pixels[offset + x] = if (bitMatrix.get(x, y)) resources.getColor(R.color.black) else resources.getColor(R.color.white) } } val bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444) bitmap.setPixels(pixels, 0, 500, 0, 0, bitMatrixWidth, bitMatrixHeight) return bitmap }
Above method uses various classes from ZXing library.
Now look at the below method, which is saving generated qr code image into the device.
fun saveImage(myBitmap: Bitmap?): String { val bytes = ByteArrayOutputStream() myBitmap!!.compress(Bitmap.CompressFormat.JPEG, 90, bytes) val wallpaperDirectory = File( Environment.getExternalStorageDirectory().toString() + IMAGE_DIRECTORY) // have the object build the directory structure, if needed. if (!wallpaperDirectory.exists()) { Log.d("dirrrrrr", "" + wallpaperDirectory.mkdirs()) wallpaperDirectory.mkdirs() } try { val f = File(wallpaperDirectory, Calendar.getInstance() .timeInMillis.toString() + ".jpg") f.createNewFile() //give read write permission val fo = FileOutputStream(f) fo.write(bytes.toByteArray()) MediaScannerConnection.scanFile(this, arrayOf(f.path), arrayOf("image/jpeg"), null) fo.close() Log.d("TAG", "File Saved::--->" + f.absolutePath) return f.absolutePath } catch (e1: IOException) { e1.printStackTrace() } return "" }
If you are not able to get result like above video, share your questions in comment section.
Keep rocking and have a nice day ahead!
Download source code for kotlin generate qr code example
[sociallocker]Download Kotlin Generate QR Source code[/sociallocker]