Utilize the ML Kit Text Recognition API to detect text in images.
How we can use Firebase ML Kit SDK in Android with Machine learning capability.
- Get FirebaseVisionText object from Bitmap:
private fun runTextRecognition() {
val image = mSelectedImage?.let { FirebaseVisionImage.fromBitmap(it) }
val detector = FirebaseVision.getInstance().visionTextDetector
button_text.isEnabled = false
image?.let {
detector.detectInImage(it)
.addOnSuccessListener {
button_text.isEnabled = true
processTextRecognitionResult(it)
}
.addOnFailureListener {
button_text.isEnabled = true
showToast(it.localizedMessage)
}
}
}
- Get texts elements result from FirebaseVisionText :
private fun processTextRecognitionResult(firebaseVisionText: FirebaseVisionText?) {
firebaseVisionText?.let {
val blocks = firebaseVisionText.blocks
when (blocks.size) {
0 -> showToast("No texts found")
else -> {
graphic_overlay.clear()
for (block in blocks.indices) {
val lines = blocks[block].lines
for (line in lines.indices) {
val elements = lines[line].elements
for (element in elements.indices) {
val textGraphic = TextGraphic(graphic_overlay, elements[element])
graphic_overlay.add(textGraphic)
}
}
}
}
}
}
}
Image One result | Image Two result | Image Three result |
---|---|---|
To successfully sync this project, need to set up this app into your Firebase project. Kindly follow below link to "Add Firebase to Your Android Project": https://firebase.google.com/docs/android/setup
- Android Studio - The Official IDE for Android
- Kotlin - The Official Language for Android ❤️
- Gradle - Build tool for Android Studio
I welcome and encourage all pull requests to learn something new.
Support it by clicking the ⭐️ button on the upper right of this page. ✌️