Create QR Code Android Kotlin example

06-Dec-2024

Create QR Code Android Kotlin example

Create QR Code Android Kotlin example


1/  Create A New Project .

main_activity.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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@color/white"
>

<LinearLayout
android:layout_centerInParent="true"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<EditText
android:id="@+id/inputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text to generate QR code"
android:padding="12dp"
android:layout_marginBottom="16dp"
android:background="@android:drawable/editbox_background"
android:textColorHint="@color/black"
android:textColor="@color/black"
/>

<Button
android:id="@+id/generateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Generate QR Code"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="16dp"/>

<ImageView
android:id="@+id/qrCodeImageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:contentDescription="QR Code will be displayed here"
android:scaleType="fitCenter"
android:layout_gravity="center"
/>

</LinearLayout>


</RelativeLayout>



2/ Add dependencies

>>>build.gradle.kts(Module:app)




implementation ("com.google.zxing:core:3.4.1")
implementation ("com.journeyapps:zxing-android-embedded:4.3.0")




3/MainActivity.kt


package com.qrcodekotlin.qrcodekotlin

import android.graphics.Bitmap
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.qrcode.QRCodeWriter
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel

class MainActivity : AppCompatActivity() {


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

// References to UI elements
val inputEditText: EditText = findViewById(R.id.inputEditText)
val generateButton: Button = findViewById(R.id.generateButton)
val qrCodeImageView: ImageView = findViewById(R.id.qrCodeImageView)


// Set click listener for the button
generateButton.setOnClickListener {
val inputData = inputEditText.text.toString()

if (inputData.isBlank()) {
// Show a message if input is empty
Toast.makeText(this, "Please enter text to generate QR code", Toast.LENGTH_SHORT).show()
} else {
// Generate QR code
val bitmap = generateQRCode(inputData)
// Display QR code
qrCodeImageView.setImageBitmap(bitmap)
}
}

}

// Function to generate QR code
private fun generateQRCode(data: String): Bitmap {
val writer = QRCodeWriter()
val hints = hashMapOf<EncodeHintType, Any>()
hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.L
hints[EncodeHintType.MARGIN] = 1

val bitMatrix = writer.encode(data, BarcodeFormat.QR_CODE, 512, 512, hints)

// Convert BitMatrix to Bitmap
val width = bitMatrix.width
val height = bitMatrix.height
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565)

for (x in 0 until width) {
for (y in 0 until height) {
bitmap.setPixel(x, y, if (bitMatrix[x, y]) android.graphics.Color.BLACK else android.graphics.Color.WHITE)
}
}

return bitmap
}

}




Comments