- Add
zxing-android-embedded
dependency in the module's build.gradle
file:
app/build.gradle
dependencies {
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
}
- Add
ImageView
element in the layout XML file:
app/src/main/res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="QR Code" />
</RelativeLayout>
- Generate QR code:
app/src/main/java/com/example/app/MainActivity.kt
package com.example.app
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.google.zxing.BarcodeFormat
import com.journeyapps.barcodescanner.BarcodeEncoder
class MainActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val text = "Hello"
val encoder = BarcodeEncoder()
val bitmap = encoder.encodeBitmap(text, BarcodeFormat.QR_CODE, 400, 400)
val myImageView: ImageView = findViewById(R.id.myImageView)
myImageView.setImageBitmap(bitmap)
}
}
Leave a Comment
Cancel reply