0

安卓工作室 3.6

我尝试在支票(纸)上识别文本。我使用这个 lib CameraView来获取图片并使用Firebase ML Kit来识别图像。

这里的图片: 在此处输入图像描述

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

在我的 xml 布局中:

<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" />

所以在我的活动中:

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")
}

这里是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

如您所见,imageBytes_size > 0textBlocks_size = 0

4

0 回答 0