Scan QR Android Kotlin
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_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp"
android:layout_centerInParent="true"
>
<com.journeyapps.barcodescanner.CompoundBarcodeView
android:id="@+id/barcode_scanner"
android:layout_width="match_parent"
android:layout_height="500dp"
app:zxing_preview_scaling_strategy="fitXY" />
<TextView
android:id="@+id/scanResultTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:padding="12dp"
android:background="@android:drawable/editbox_background"
android:textColor="@color/black"
android:textIsSelectable="true"
android:layout_marginTop="10dp"
/>
</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.scanqrkotlin.scanqrkotlin
import android.os.Bundle
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.google.zxing.ResultPoint
import com.journeyapps.barcodescanner.BarcodeCallback
import com.journeyapps.barcodescanner.BarcodeResult
import com.journeyapps.barcodescanner.CompoundBarcodeView
class MainActivity : AppCompatActivity() {
lateinit var barcodeScanner:CompoundBarcodeView
lateinit var scanResultTextView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
barcodeScanner=findViewById(R.id.barcode_scanner)
scanResultTextView=findViewById(R.id.scanResultTextView)
barcodeScanner.decodeContinuous(object : BarcodeCallback {
override fun barcodeResult(result: BarcodeResult?) {
result?.let {
scanResultTextView.text = "${it.text}"
}
}
override fun possibleResultPoints(resultPoints: List<ResultPoint>?) {}
})
}
override fun onResume() {
super.onResume()
barcodeScanner.resume()
}
override fun onPause() {
super.onPause()
barcodeScanner.pause()
}
}