I’m new to Kotlin and I need to make an application that reads QR codes, I did this using the following code:
import androidx.camera.core.ImageAnalysis
import androidx.camera.core.ImageProxy
import com.google.zxing.BinaryBitmap
import com.google.zxing.MultiFormatReader
import com.google.zxing.Reader
import com.google.zxing.common.HybridBinarizer
class QRCodeAnalyzer(private val qrCodeListener: (String) -> Unit) : ImageAnalysis.Analyzer {
private val reader: Reader = MultiFormatReader()
override fun analyze(imageProxy: ImageProxy) {
val buffer = imageProxy.planes[0].buffer
val data = ByteArray(buffer.capacity())
buffer.get(data)
val source = com.google.zxing.PlanarYUVLuminanceSource(
data,
imageProxy.width,
imageProxy.height,
0,
0,
imageProxy.width,
imageProxy.height,
false
)
println(source.height)
val bitmap = BinaryBitmap(HybridBinarizer(source))
println(source.height)
try {
val result = reader.decode(bitmap)
qrCodeListener.invoke(result.text)
} catch (e: Exception) {
println("============================ ${e.message} ==========================")
} finally {
imageProxy.close()
}
}
}
My project has a minimum version of Android SDK 21 and I am using the following dependencies:
implementation("androidx.core:core-ktx:1.10.1")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.9.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.mediarouter:mediarouter:1.6.0")
implementation("androidx.camera:camera-camera2:1.1.0-alpha10")
implementation("androidx.camera:camera-view:1.3.1")
implementation("androidx.camera:camera-lifecycle:1.3.1")
implementation("com.google.zxing:core:3.4.1")
implementation("com.squareup.okhttp3:okhttp:4.9.1")
implementation("com.google.code.gson:gson:2.8.8")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
This code reads the QR code perfectly in tests on the Android Studio simulator and on an 8.7-inch Android 14 tablet.
However, when trying to run it on a 10-inch Android 12 tablet, it cannot read the QR code and returns no error
What can be done to fix this?