Skip to content

Instantly share code, notes, and snippets.

@ram-prasad-1
Last active December 15, 2019 14:58
Show Gist options
  • Select an option

  • Save ram-prasad-1/7ec3e3dc6b7670b431d53367706f994f to your computer and use it in GitHub Desktop.

Select an option

Save ram-prasad-1/7ec3e3dc6b7670b431d53367706f994f to your computer and use it in GitHub Desktop.

Basic processing steps:

    private fun runTextRecognition(bitmap: Bitmap) {
        val image = FirebaseVisionImage.fromBitmap(bitmap)
        val recognizer = FirebaseVision.getInstance().onDeviceTextRecognizer

        recognizer.processImage(image).addOnSuccessListener {
            processTextRecognitionResult(it)
            isOcrImageFlowLocked = false
        }.addOnFailureListener { e: Exception ->
            e.printStackTrace()
            handleError(e)
            isOcrImageFlowLocked = false
        }
    }

    private fun processTextRecognitionResult(texts: FirebaseVisionText) {
        var txt = ""
        var maxFloat = 0f
        for (block in texts.textBlocks) {
            for (line in block.lines) {
                line.text.toFloatOrNull()?.let {
                    maxFloat = maxOf(maxFloat, it)
                }
                for (element in line.elements) {
                    txt += element.text + " "
                }
            }
        }
        Timber.d("Temperature Detected: $maxFloat")
        Timber.d("Text Detected: $txt")

        ocrImageObserver!!.value = if (floor(maxFloat) == ceil(maxFloat)) {
            maxFloat.toInt().toString()
        } else {
            maxFloat.toString()
        }

        if (maxFloat == 0f) {
            Toast.makeText(requireContext(), "No Numbers found", Toast.LENGTH_SHORT).show()
        }
    }
@ram-prasad-1
Copy link
Author

Also, Tweak image resolution accordingly and see which is best for our use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment