I'm looking for a few days how I can read a QR code using ML Kit and CameraX. The process of the input image returns always a failure "IllegalStateException: Image is already closed". I have looked on different forums, but I can not find what I'm doing wrong. I think I'm following the Google documentation, but it won't work.
This is what I'm doing in my MainActivity:
- I create a textureView which shows me the input of my camera
- I start my camera (after checking the necessary rights)
- I setup a "preview" use case
- I setup a "Image Analysis" use case
- I set an analyzer which creates an InputImage which is passed to the Barcode scanner process
- Set my LifeCycle with the use cases "preview" and "image analysis"
I'm using CameraX version "1.0.0-alpha04" and "com.google.mlkit:barcode-scanning:16.0.3".
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "be.lapit.qrcodescanner"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
// ML Kit Barcode scanner
implementation 'com.google.mlkit:barcode-scanning:16.0.3'
implementation 'com.google.android.gms:play-services-mlkit-barcode-scanning:16.1.2'
// CameraX
def camerax_version = "1.0.0-alpha04"
// The following line is optional, as the core library is included indirectly by camera-camera2
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
// If you want to additionally use the CameraX Lifecycle library
//implementation "androidx.camera:camera-lifecycle:${camerax_version}"
// If you want to additionally use the CameraX View class
//implementation "androidx.camera:camera-view:1.0.0-alpha17"
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="250px"
android:layout_height="250px"
android:layout_marginBottom="48dp"
android:src="@drawable/thumb_up" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test textview"/>
<TextureView
android:id="@+id/texture_view"
android:layout_width="600px"
android:layout_height="600px" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package be.lapit.qrcodescanner
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import android.util.Size
import android.view.TextureView
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.*
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.LifecycleOwner
import com.google.android.gms.tasks.OnFailureListener
import com.google.mlkit.vision.barcode.Barcode
import com.google.mlkit.vision.barcode.BarcodeScannerOptions
import com.google.mlkit.vision.barcode.BarcodeScanning
import com.google.mlkit.vision.common.InputImage
import java.time.Instant
import java.time.format.DateTimeFormatter
import kotlin.system.measureTimeMillis
class MainActivity : AppCompatActivity() {
// PLA: Variable needed to check if user has granted camera permission
companion object {
private const val REQUEST_CAMERA_PERMISSION = 10
}
private lateinit var textureView: TextureView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textureView = findViewById(R.id.texture_view)
// Request camera permissions
if (isCameraPermissionGranted()) {
textureView.post { startCamera() }
} else {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.CAMERA),
REQUEST_CAMERA_PERMISSION
)
}
}
private fun startCamera() {
// -----------------------
// Setup use case Preview
// -----------------------
val previewConfig : PreviewConfig = PreviewConfig.Builder()
// We want to show input from front camera of the device
.setLensFacing(CameraX.LensFacing.FRONT)
.build()
val preview = Preview(previewConfig)
preview.setOnPreviewOutputUpdateListener { previewOutput ->
textureView.setSurfaceTexture(previewOutput.surfaceTexture)
}
// ------------------------------
// Setup use case Image Analysis
// ------------------------------
val imageAnalysisConfig = ImageAnalysisConfig.Builder()
.setTargetResolution(Size(1280, 720))
.setLensFacing(CameraX.LensFacing.FRONT)
.setImageReaderMode(ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
.setImageQueueDepth(1)
.build()
val imageAnalysis = ImageAnalysis(imageAnalysisConfig)
// -------------------
// Set image analyzer
// -------------------
imageAnalysis.setAnalyzer { imageProxy: ImageProxy, rotationDegrees: Int ->
Log.i("Status", "In setAnalyzer on " + System.currentTimeMillis())
val mediaImage = imageProxy.image
if (mediaImage != null) {
val image1 = InputImage.fromMediaImage(mediaImage, rotationDegrees)
// --------------------------
// Configure barcode scanner
// --------------------------
val barcodeScannerOptions: BarcodeScannerOptions = BarcodeScannerOptions.Builder()
.setBarcodeFormats(
Barcode.FORMAT_QR_CODE,
Barcode.FORMAT_AZTEC,
Barcode.FORMAT_EAN_13
)
.build()
// Get an instance of BarcodeScanner
val scanner = BarcodeScanning.getClient(barcodeScannerOptions)
scanner.process(image1)
.addOnSuccessListener ({ barcodes ->
// Task completed successfully
Log.i("Status", "In success listener on timestamp " + System.currentTimeMillis())
for (barcode in barcodes) {
Log.i("Barcode", barcode.toString())
}
Toast.makeText(this,"Ik ben hier eindelijk",Toast.LENGTH_LONG).show();
})
.addOnFailureListener(OnFailureListener {
// Process failed
Log.i("Status", "In failure listener on " + System.currentTimeMillis() + " because: $it")
Log.i("Failure", "$it")
it.printStackTrace()
})
.addOnCompleteListener {
Log.i("Status", "In complete listener on " + System.currentTimeMillis())
//imageProxy.close()
}
}
}
// Bind
CameraX.bindToLifecycle(this as LifecycleOwner, preview, imageAnalysis)
}
private fun isCameraPermissionGranted(): Boolean {
val selfPermission = ContextCompat.checkSelfPermission(
baseContext,
Manifest.permission.CAMERA
)
return selfPermission == PackageManager.PERMISSION_GRANTED
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (isCameraPermissionGranted()) {
textureView.post { startCamera() }
} else {
Toast.makeText(this, "Camera permission is required.", Toast.LENGTH_SHORT).show()
finish()
}
}
}
}
Logging info
I/Status: In setAnalyzer on 1602190069609
I/tflite: Initialized TensorFlow Lite runtime.
I/native: barcode_detector_client.cc:238 Not using NNAPI
I/Status: In failure listener on 1602190069612 because: com.google.mlkit.common.MlKitException: Internal error has occurred when executing ML Kit tasks
I/Failure: com.google.mlkit.common.MlKitException: Internal error has occurred when executing ML Kit tasks
W/System.err: com.google.mlkit.common.MlKitException: Internal error has occurred when executing ML Kit tasks
at com.google.mlkit.common.sdkinternal.ModelResource.zza(com.google.mlkit:common@@17.0.0:32)
at com.google.mlkit.common.sdkinternal.zzl.run(com.google.mlkit:common@@17.0.0)
W/System.err: at com.google.mlkit.common.sdkinternal.zzp.run(com.google.mlkit:common@@17.0.0:3)
at com.google.mlkit.common.sdkinternal.MlKitThreadPool.zzd(com.google.mlkit:common@@17.0.0:24)
at com.google.mlkit.common.sdkinternal.MlKitThreadPool.zza(com.google.mlkit:common@@17.0.0:30)
at com.google.mlkit.common.sdkinternal.zzh.run(com.google.mlkit:common@@17.0.0)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)
Caused by: java.lang.IllegalStateException: Image is already closed
at android.media.Image.throwISEIfImageIsInvalid(Image.java:68)
at android.media.ImageReader$SurfaceImage$SurfacePlane.getBuffer(ImageReader.java:803)
at com.google.mlkit.vision.barcode.internal.zzf.zza(com.google.android.gms:play-services-mlkit-barcode-scanning@@16.1.2:72)
at com.google.mlkit.vision.barcode.internal.zzf.zzb(com.google.android.gms:play-services-mlkit-barcode-scanning@@16.1.2:83)
at com.google.mlkit.vision.barcode.internal.zzf.run(com.google.android.gms:play-services-mlkit-barcode-scanning@@16.1.2:143)
at com.google.mlkit.vision.common.internal.MobileVisionBase.zza(com.google.mlkit:vision-common@@16.1.0:23)
at com.google.mlkit.vision.common.internal.zzc.call(com.google.mlkit:vision-common@@16.1.0)
at com.google.mlkit.common.sdkinternal.ModelResource.zza(com.google.mlkit:common@@17.0.0:29)
... 8 more
I/Status: In complete listener on 1602190069614
In failure listener on 1602190069614 because: com.google.mlkit.common.MlKitException: Internal error has occurred when executing ML Kit tasks
I/Failure: com.google.mlkit.common.MlKitException: Internal error has occurred when executing ML Kit tasks
W/System.err: com.google.mlkit.common.MlKitException: Internal error has occurred when executing ML Kit tasks
at com.google.mlkit.common.sdkinternal.ModelResource.zza(com.google.mlkit:common@@17.0.0:32)
at com.google.mlkit.common.sdkinternal.zzl.run(com.google.mlkit:common@@17.0.0)
at com.google.mlkit.common.sdkinternal.zzp.run(com.google.mlkit:common@@17.0.0:3)
at com.google.mlkit.common.sdkinternal.MlKitThreadPool.zzd(com.google.mlkit:common@@17.0.0:24)
at com.google.mlkit.common.sdkinternal.MlKitThreadPool.zza(com.google.mlkit:common@@17.0.0:30)
at com.google.mlkit.common.sdkinternal.zzh.run(com.google.mlkit:common@@17.0.0)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)
W/System.err: Caused by: java.lang.IllegalStateException: Image is already closed
at android.media.Image.throwISEIfImageIsInvalid(Image.java:68)
at android.media.ImageReader$SurfaceImage$SurfacePlane.getBuffer(ImageReader.java:803)
at com.google.mlkit.vision.barcode.internal.zzf.zza(com.google.android.gms:play-services-mlkit-barcode-scanning@@16.1.2:72)
at com.google.mlkit.vision.barcode.internal.zzf.zzb(com.google.android.gms:play-services-mlkit-barcode-scanning@@16.1.2:83)
at com.google.mlkit.vision.barcode.internal.zzf.run(com.google.android.gms:play-services-mlkit-barcode-scanning@@16.1.2:143)
at com.google.mlkit.vision.common.internal.MobileVisionBase.zza(com.google.mlkit:vision-common@@16.1.0:23)
at com.google.mlkit.vision.common.internal.zzc.call(com.google.mlkit:vision-common@@16.1.0)
at com.google.mlkit.common.sdkinternal.ModelResource.zza(com.google.mlkit:common@@17.0.0:29)
... 8 more
I/Status: In complete listener on 1602190069615
Just to help other people, I post my latest (working) code here
Build.gradle
activity_main.xml
MainActivity.kt