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
}
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()
}
implementation 'com.google.zxing:zxingorg:3.3.3'
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()
}
}