Android QRCode

QR code, short for Quick Response code, is a two-dimensional barcode that can be quickly scanned by a smartphone camera. It is widely used for various purposes such as advertising, storing URLs, payment transactions, and more. In this article, we will explore how to generate and scan QR codes in Android using the ZXing library.

Generating QR Code

To generate a QR code in Android, we can use the ZXing library. First, we need to add the library to our project by adding the following dependency to the app-level build.gradle file:

implementation 'com.google.zxing:core:3.3.3'
implementation 'com.journeyapps:zxing-android-embedded:4.2.0'

Next, we can use the following code to generate a QR code:

import android.graphics.Bitmap
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.qrcode.QRCodeWriter
import com.journeyapps.barcodescanner.BarcodeEncoder

fun generateQRCode(text: String): Bitmap {
    val hints = mapOf(
        EncodeHintType.CHARACTER_SET to "UTF-8",
        EncodeHintType.ERROR_CORRECTION to ErrorCorrectionLevel.H
    )
    val writer = QRCodeWriter()
    val bitMatrix = writer.encode(text, BarcodeFormat.QR_CODE, 512, 512, hints)
    val encoder = BarcodeEncoder()
    return encoder.createBitmap(bitMatrix)
}

In the above code, we first create a QRCodeWriter and encode the given text into a BitMatrix. Then, we create a BarcodeEncoder and convert the BitMatrix into a Bitmap. Finally, we return the generated QR code as a Bitmap object.

Scanning QR Code

To scan a QR code in Android, we can use the ZXing library as well. We need to add the camera permission to the AndroidManifest.xml file:

<uses-permission android:name="android.permission.CAMERA" />

Next, we can use the following code to scan a QR code:

import com.google.zxing.integration.android.IntentIntegrator
import com.google.zxing.integration.android.IntentResult

fun scanQRCode(activity: Activity) {
    val integrator = IntentIntegrator(activity)
    integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE)
    integrator.setPrompt("Scan a QR code")
    integrator.setCameraId(0)
    integrator.setBeepEnabled(false)
    integrator.initiateScan()
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    val result: IntentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
    if (result != null) {
        val scannedText = result.contents
        // Do something with the scanned text
    }
}

In the above code, we first create an IntentIntegrator object and set the desired barcode formats, prompt message, camera ID, and beep settings. Then, we initiate the scanning process by calling the initiateScan method.

After the scanning process is completed, the result is returned in the onActivityResult method. We can retrieve the scanned text using result.contents and perform any required actions.

Conclusion

In this article, we have learned how to generate and scan QR codes in Android using the ZXing library. Generating a QR code involves encoding the text into a BitMatrix and converting it into a Bitmap. Scanning a QR code involves initiating the scanning process and handling the result. QR codes have numerous applications in the digital world, and with the help of the ZXing library, integrating QR code functionality into an Android app becomes easy and straightforward.

journey
    title Generating and Scanning QR Codes in Android
    section Generating QR Code
    Generating QR code->Scanning QR code: Generating a QR code involves encoding the text into a `BitMatrix` and converting it into a `Bitmap`.
    section Scanning QR Code
    Scanning QR code->Performing Actions: After the scanning process is completed, the result is returned and we can perform any required actions.
    Performing Actions-->Generating QR code: QR codes have numerous applications in the digital world, making it useful to generate QR codes.

By following the steps and code examples in this article, you can easily incorporate QR code functionality into your Android app and enhance user experience.