QR Code Generation

While typing user identification we might do some mistakes, To avoid that we will can store the identification details inside a QR code. Which will reduce human error.

Watch this here


We are going to user google's "zxing" library for generating QR Code. Following code will generate the QR Code and display it on a ImageView.

                
    fun encodeAsBitmap(str: String, WIDTH: Int, HEIGHT: Int, ctx: Context): Bitmap? {
        val result: BitMatrix
        try {
            result = MultiFormatWriter().encode(str,
                 BarcodeFormat.QR_CODE, WIDTH, HEIGHT, null)
        } catch (iae: IllegalArgumentException) {
            return null
        }
        val width = result.width
        val height = result.height
        val pixels = IntArray(width * height)
        for (y in 0 until height) {
            val offset = y * width
            for (x in 0 until width) {
                pixels[offset + x] = if (result.get(x, y)) -0x1000000 else -0x1
            }
        }
        val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height)
        createImageFile(bitmap)
        return bitmap
    }
    
                
In some case we might need to store the image. Following code will generate a file using bitmap.
                
  fun createImageFile(bitmapScaled : Bitmap){
        val bytes = ByteArrayOutputStream()
        bitmapScaled.compress(Bitmap.CompressFormat.PNG, 40, bytes)
        val filepath = Environment.getExternalStorageDirectory().absolutePath
             + File.separator + "test.png"
        val f = File(filepath)
        f.createNewFile()
        val fo = FileOutputStream(f)
        fo.write(bytes.toByteArray())
        fo.close()
    }
  
                

Library Details :

We can use either a jar( download here ) file or we can add a dependency for the library. In this tutorial, will be using the jar file.
                
    implementation 'com.google.zxing:zxingorg:3.3.3'
    
                

Final Code :

                

    import android.content.Context
    import android.support.v7.app.AppCompatActivity
    import android.os.Bundle
    import android.graphics.Bitmap
    import android.os.Environment
    import com.google.zxing.BarcodeFormat
    import com.google.zxing.MultiFormatWriter
    import com.google.zxing.common.BitMatrix
    import kotlinx.android.synthetic.main.activity_qrcode_generator.*
    import java.io.ByteArrayOutputStream
    import java.io.File
    import java.io.FileOutputStream


    class QRCodeGenerator : AppCompatActivity() {

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_qrcode_generator)

            iv_qr_code.setImageBitmap(encodeAsBitmap("CodeAndroid",256,256,this))
        }


        fun encodeAsBitmap(str: String, WIDTH: Int, HEIGHT: Int, ctx: Context): Bitmap? {
            val result: BitMatrix
            try {
                result = MultiFormatWriter().encode(str,
                     BarcodeFormat.QR_CODE, WIDTH, HEIGHT, null)
            } catch (iae: IllegalArgumentException) {
                return null
            }
            val width = result.width
            val height = result.height
            val pixels = IntArray(width * height)
            for (y in 0 until height) {
                val offset = y * width
                for (x in 0 until width) {
                    pixels[offset + x] = if (result.get(x, y)) -0x1000000 else -0x1
                }
            }
            val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height)
            createImageFile(bitmap)
            return bitmap
        }

        fun createImageFile(bitmapScaled : Bitmap){
            val bytes = ByteArrayOutputStream()
            bitmapScaled.compress(Bitmap.CompressFormat.PNG, 40, bytes)
            val filepath = Environment.getExternalStorageDirectory().absolutePath
                 + File.separator + "test.png"
            val f = File(filepath)
            f.createNewFile()
            val fo = FileOutputStream(f)
            fo.write(bytes.toByteArray())
            fo.close()
        }

    }
 
                

12800 Subscribers