Text Recognition Using Google ML Kit in Flutter

Updated 27 April 2023

Save

In this article, we are going to learn Text recognition from the camera in Flutter.

Text Recognition

Recognizes the text from the image/ real-time camera. The ML Kit Text Recognition API can recognize text in any Latin-based character set. Please check Google ML Kit for more on Text recognition.

Check out more about our Flutter app development.

Using ML Kit for text recognition in Flutter

For text recognition in Flutter, we are going to use these flutter plugins: google_ml_kit and camera.

google_ml_kit: for the text recognition and many more, please check the official pub here.

camera: for accessing the device cameras, image stream which we are going to use later and for more check here.

Before we get started read about the platform-specific requirements here and known issues of the google_ml_kit plugin here.

And for the camera plugin platform-specific requirements check here and for the known issues check here.

We start by adding these plugins to our project’s pubspec.yaml file:

After adding the plugins, import them into your camera_screen.dart file like:

Now define your camera controller and initialize it inside initState method and Initialize TextDetector like this:

Once the camera is initialized, we start streaming images from the platform camera using the startImageStream method and pass the _processCameraImage method which is defined below as the parameter to it:

As you can see in the above code, textDetector.processImage(inputImage) is the main method for recognizing the text from the image and requires an InputImage type image as a parameter so first, we need to get the InputImage for the recognition from the CameraImage and we are getting this using getInputImage(image) method defined below:

Now create your CameraPreview widget for creating a preview widget for the given camera controller. and pass the above-defined CameraController like:

And put the above camera preview widget inside your build method like any other widget and that’s it.

Thanks for reading.

Happy Coding 🙂

For more Flutter tutorials visit Mobikul Blogs.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • jade
    • anchit (Moderator)
  • Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home