0

I have two classes: QrActivity, where camera is turned on to scan qr codes, and BarcodeAnalyzer, where image analyze process is going. I am doing it with cameraX and ML Kit.

That's my QrActivity:

package ge.softservice.nfcwithactivties

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.*
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_qr.*
import java.io.File
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors


class QrActivity : AppCompatActivity() {
    private var preview: Preview? = null
    private var imageAnalyzer: ImageAnalysis? = null
    private var camera: Camera? = null

    private lateinit var outputDirectory: File
    private lateinit var cameraExecutor: ExecutorService

    override fun onRequestPermissionsResult(
        requestCode: Int, permissions: Array<String>, grantResults:
        IntArray
    ) {
        if (requestCode == REQUEST_CODE_PERMISSIONS) {
            if (allPermissionsGranted()) {
                startCamera()
            } else {
                Toast.makeText(
                    this,
                    "Permissions not granted by the user.",
                    Toast.LENGTH_SHORT
                ).show()
                finish()
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_qr)

        // Request camera permissions
        if (allPermissionsGranted()) {
            startCamera()
        } else {
            ActivityCompat.requestPermissions(
                this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS
            )
        }

        //      outputDirectory = getOutputDirectory()

        cameraExecutor = Executors.newSingleThreadExecutor()
    }

    private fun startCamera() {

        val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
        cameraProviderFuture.addListener(Runnable {
            // Used to bind the lifecycle of cameras to the lifecycle owner
            val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()


            // Preview
            preview = Preview.Builder()
                .build()

            // Select back camera
            val cameraSelector =
                CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build()

            try {
                // Unbind use cases before rebinding
                cameraProvider.unbindAll()

                // Bind use cases to camera
                camera = cameraProvider.bindToLifecycle(
                    this, cameraSelector, preview
                )
                preview?.setSurfaceProvider(viewFinder.createSurfaceProvider(/*camera?.cameraInfo*/))
            } catch (exc: Exception) {
                Log.e(TAG, "Use case binding failed", exc)
            }

        }, ContextCompat.getMainExecutor(this))
    }

    private fun takePhoto() {
        // TODO
    }

    private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all {
        ContextCompat.checkSelfPermission(
            baseContext, it
        ) == PackageManager.PERMISSION_GRANTED
    }

/*    fun getOutputDirectory(): File {
        val mediaDir = externalMediaDirs.firstOrNull()?.let {
            File(it, resources.getString(R.string.app_name)).apply { mkdirs() } }
        return if (mediaDir != null && mediaDir.exists())
            mediaDir else filesDir
    }*/

    companion object {
        private const val TAG = "CameraXBasic"
        private const val FILENAME_FORMAT = "yyyy-MM-dd-HH-mm-ss-SSS"
        private const val REQUEST_CODE_PERMISSIONS = 10
        private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA)
    }
}

Thats' my Barcode analyzer:

package ge.softservice.nfcwithactivties

import android.annotation.SuppressLint
import android.content.Context
import android.widget.Toast
import androidx.camera.core.ImageAnalysis
import androidx.camera.core.ImageProxy
import com.google.firebase.ml.vision.FirebaseVision
import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcode
import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcodeDetectorOptions
import com.google.firebase.ml.vision.common.FirebaseVisionImage
import com.google.firebase.ml.vision.common.FirebaseVisionImageMetadata

class BarcodeAnalyzer : ImageAnalysis.Analyzer {

   lateinit var context: Context;


    private fun degreesToFirebaseRotation(degrees: Int): Int = when(degrees) {
        0 -> FirebaseVisionImageMetadata.ROTATION_0
        90 -> FirebaseVisionImageMetadata.ROTATION_90
        180 -> FirebaseVisionImageMetadata.ROTATION_180
        270 -> FirebaseVisionImageMetadata.ROTATION_270
        else -> throw Exception("Rotation must be 0, 90, 180, or 270.")
    }


    @SuppressLint("UnsafeExperimentalUsageError")
    override fun analyze(imageProxy: ImageProxy) {
 //     val degrees by Delegates.notNull<Int>()
        val mediaImage = imageProxy.image
        val imageRotation = degreesToFirebaseRotation(imageProxy.imageInfo.rotationDegrees)
        if (mediaImage != null) {
            val image = FirebaseVisionImage.fromMediaImage(mediaImage, imageRotation)
            // Pass image to an ML Kit Vision API
            val options = FirebaseVisionBarcodeDetectorOptions.Builder()
                .setBarcodeFormats(
                    FirebaseVisionBarcode.FORMAT_QR_CODE
                )
                .build()
            val detector = FirebaseVision.getInstance().getVisionBarcodeDetector(options)

            val result = detector.detectInImage(image)
                .addOnSuccessListener { barcodes ->
                    // Task completed successfully
                    Toast.makeText(context, "it works", Toast.LENGTH_SHORT).show()
                }
                .addOnFailureListener {
                    // Task failed with an exception
                    Toast.makeText(context, "something went wrong", Toast.LENGTH_SHORT).show()
                }
        }
    }
}

I also tried to put this code instead of BarcodeAnalyzer into QrActivity class, but then image value is not recognized.

    val options = FirebaseVisionBarcodeDetectorOptions.Builder()
        .setBarcodeFormats(
            FirebaseVisionBarcode.FORMAT_QR_CODE
        )
        .build()
    val detector = FirebaseVision.getInstance().getVisionBarcodeDetector(options)

    val result = detector.detectInImage(image)
        .addOnSuccessListener { barcodes ->
            // Task completed successfully
            Toast.makeText(context, "it works", Toast.LENGTH_SHORT).show()
        }
        .addOnFailureListener {
            // Task failed with an exception
            Toast.makeText(context, "something went wrong", Toast.LENGTH_SHORT).show()
        }

if I put it in BarcodeAnalyzer, where it is now, there is no error but result value is gray color and not used.

I found this project and tried to do similar but in my project it shows me errors: https://github.com/Lavanyagaur22/Visitor-Card

I also tried this and other tutorials but lots of things are unclear for me: https://www.bignerdranch.com/blog/using-firebasemlkit-with-camerax/

I tried to do instance in QrActivty class of BarcodeAnalyzer but is shows me errors.

I am following google's firebase ML Kit tutorial but still doesn't work for me: https://firebase.google.com/docs/ml-kit/android/read-barcodes#kotlin+ktx_1

So, how can I connect BarcodeAnalyzer class to QrActivity class or how to make an instance to recognize qr code?

X Y
  • 119
  • 9

1 Answers1

1

To connect your image analyzer to your camera, you need to add a use case where you've already added one; the preview use case here: cameraProvider.bindToLifecycle(this, cameraSelector, preview)

You can create an instance of the analyzer like this:

val analyzer = ImageAnalysis.Builder()
    .setTargetAspectRatio(AspectRatio.RATIO_16_9)
    .setTargetRotation(previewView.display.rotation)
    .build().also {
         it.setAnalyzer(Executors.newSingleThreadExecutor(), BarcodeAnalyzer())
     }

And then bind this use case:

cameraProvider.bindToLifecycle(this, cameraSelector, preview, analyzer)
Saurabh Thorat
  • 18,131
  • 5
  • 53
  • 70
  • Where should I put that code? I put into startCamera method after preview. but here: .setTargetRotation(rotation) rotation is unresolved reference. and here: cameraProvider.bindToLifecycle(this, cameraSelector, preview, analyzer) cameraSelector is unresolved reference. and analyzer is Unit instead of UseCase. Nay be I should use imageanalyzer instead of analyzer. But what should I do about these unresolved references? – X Y Jun 09 '20 at 16:01
  • Check my updated answer. `rotation` is the rotation of the device, you can put `previewView.display.rotation` for it. – Saurabh Thorat Jun 09 '20 at 16:33
  • now previewView is unresolved reference. I added it as: lateinit var previewView: PreviewView but it crashes on null pointer exception. I tried: var previewView: PreviewView? = null but it crashes again – X Y Jun 09 '20 at 16:46
  • 1
    Use whatever variable you’ve used for your viewfinder instead of previewView – Saurabh Thorat Jun 09 '20 at 19:00
  • I changed it and there is no more error. but in BarcodeAnalyzer class "result" value is still grey and not used. How Can I have access to this variable into QrActivity class or How should I make it work? – X Y Jun 13 '20 at 06:34
  • I would suggest take a look at [this sample](https://github.com/android/camera-samples/blob/master/CameraXBasic/app/src/main/java/com/android/example/cameraxbasic/fragments/CameraFragment.kt) to check how to return result from an analyzer to your activity. – Saurabh Thorat Jun 13 '20 at 07:41