0

Android Studio 3.6

I try to recgnized text on check (paper). I use this lib CameraView for get picture and use Firebase ML Kit to recognize image.

here image: enter image description here

implementation "com.otaliastudios:cameraview:2.6.0"
implementation 'com.google.firebase:firebase-ml-vision:24.0.1'

in my xml layout:

<com.otaliastudios.cameraview.CameraView
            android:id="@+id/cameraView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:keepScreenOn="true"
            app:cameraAudio="off"
            app:cameraMode="picture"
            app:cameraPictureFormat="jpeg"
            app:cameraRequestPermissions="false"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/profileDetailsToolbar" />

So in my Activity:

private fun initLogic() {
       dataBinding.cameraView.setLifecycleOwner(this);

        dataBinding.cameraView.addFrameProcessor(object : FrameProcessor {
            override fun process(frame: Frame) {
                Debug.d(TAG, "addFrameProcessor: process: frame = $frame")
                var bitmap: Bitmap? = null
                if (frame.dataClass == ByteArray::class.java) {
                    val out = ByteArrayOutputStream()
                    val yuvImage = YuvImage(
                        frame.getData(),
                        ImageFormat.NV21,
                        frame.getSize().getWidth(),
                        frame.getSize().getHeight(),
                        null
                    )
                    yuvImage.compressToJpeg(
                        Rect(0, 0, frame.getSize().getWidth(), frame.getSize().getHeight()),
                        90,
                        out
                    )
                    val imageBytes = out.toByteArray()
                    Debug.d(
                        TAG,
                        "addFrameProcessor: process_dataClass: ByteArray, imageBytes_size = " + imageBytes.size
                    )
                    bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
                } else if (frame.dataClass == Image::class.java) {
                    Debug.d(TAG, "addFrameProcessor: process_dataClass: Image")
                    val image: Image = frame.getData()
                    bitmap = AndroidUtil.image2Bitmap(image)
                }
                Debug.d(TAG, "addFrameProcessor: process: bitmap_result = $bitmap")
                if (bitmap != null) {
                    runDetector(bitmap)
                }
            }
        })
    }

    private fun runDetector(bitmap: Bitmap?) {
        Debug.d(TAG, "runDetector: bitmap = $bitmap")
        val image = FirebaseVisionImage.fromBitmap(bitmap!!)
        val detector = FirebaseVision.getInstance().onDeviceTextRecognizer
        detector.processImage(image)
            .addOnSuccessListener { firebaseVisionText ->
                Debug.d(TAG, "runDetector: processImage: firebaseVisionText = $firebaseVisionText")
                processTextResult(firebaseVisionText)
            }
            .addOnFailureListener { e ->
                Debug.e(TAG, "runDetector: processImage: e = $e")
            }
    }

    private fun processTextResult(firebaseVisionText: FirebaseVisionText) {
        Debug.d(
            TAG,
            "processTextResult: textBlocks_size = " + firebaseVisionText.textBlocks.size
        )
        var detectedText = "\n"
        firebaseVisionText.textBlocks.forEach {
            detectedText += it.text + "\n"
        }
        Debug.d(TAG, "processTextResult: detectedText_start\n")
        Debug.d(TAG, detectedText)
        Debug.d(TAG, "processTextResult: detectedText_end")
}

And here logcat:

activity.ScanCheckActivity: addFrameProcessor: process_dataClass: ByteArray, imageBytes_size = 137056

activity.ScanCheckActivity( 8140): runDetector: processImage: firebaseVisionText = com.google.firebase.ml.vision.text.FirebaseVisionText@e34f1b5
activity.ScanCheckActivity( 8140): processTextResult: textBlocks_size = 0
activity.ScanCheckActivity( 8140): processTextResult: detectedText_start
activity.ScanCheckActivity( 8140): 
activity.ScanCheckActivity( 8140): processTextResult: detectedText_end


ScanCheckActivity( 8140): addFrameProcessor: process_dataClass: ByteArray, imageBytes_size = 211184
activity.ScanCheckActivity( 8140): processTextResult: textBlocks_size = 0
activity.ScanCheckActivity( 8140): processTextResult: detectedText_start
activity.ScanCheckActivity( 8140): 
activity.ScanCheckActivity( 8140): processTextResult: detectedText_end

As you can see the imageBytes_size > 0 but textBlocks_size = 0

Alexei
  • 14,350
  • 37
  • 121
  • 240
  • `try to recgnized text on check from coffee` ??? What are you trying to tell us? – blackapps Jan 02 '20 at 10:14
  • @blackapps it's invoice (check) paper. I take picture from it and try to recognize it – Alexei Jan 02 '20 at 10:40
  • Are you displaying the bitmap in an ImageView to check if it is ok? Nobody can help you as this is not reproducable code. You produce a jpg. Save it to file. Put it somewhere on the internet so anybody interested can download the file. Then post only code to ocr the file once it is on an Android device. – blackapps Jan 02 '20 at 10:47
  • @blackapps I was updated my post. Added image – Alexei Jan 02 '20 at 11:27
  • Ok. Hope someone tries it out. I do not use that lib, sorry. – blackapps Jan 02 '20 at 11:35

0 Answers0