Firebase

How to use Firebase ML Kit for on-device text recognition?

Discover how to harness Firebase ML Kit for on-device text recognition. Dive into our easy-to-follow guide and enhance your mobile apps with efficient text recognition in no time!

Developer profile skeleton
a developer thinking

Overview

Firebase ML Kit offers strong on-device text recognition to swiftly and precisely pull text from images. It allows real-time text processing with no need for server-side computing, perfect for apps needing swift and offline operation. Adding ML Kit to Android or iOS apps lets you tap into its pre-trained machine learning models to spot and read text in different languages right on the gadget. This entails configuring your Firebase project, pulling in the needed ML Kit libraries, and coding the text recognition feature into your app.

Get a Free No-Code Consultation
Meet with Will, CEO at Bootstrapped to get a Free No-Code Consultation
Book a Call
Will Hawkins
CEO at Bootstrapped

How to use Firebase ML Kit for on-device text recognition?

Step 1: Set Up Firebase in Your Project

  1. Create a New Firebase Project:
  • Head over to the Firebase Console.
  • Click on the Add project button and just follow the steps on the screen.
  1. Add Your App to the Firebase Project:
  • Choose your platform (iOS, Android, Web).
  • Follow the instructions to register your app with Firebase.
  • Download the google-services.json or GoogleService-Info.plist file and put it in the right directory in your project.
  1. Add Firebase SDK to Your Project:
  • For Android:
    ```gradle
    // Add this in your root-level build.gradle file
    classpath 'com.google.gms:google-services:4.3.10'
    ```
    ```gradle
    // Add this in your app-level build.gradle file
    apply plugin: 'com.google.gms.google-services'
    implementation 'com.google.firebase:firebase-ml-vision:24.1.0'
    ```

  • For iOS:
    ```ruby

    Add this in your Podfile

    pod 'Firebase/MLVisionTextModel'
    ```

Step 2: Initialize Firebase in Your Application

  • Android:
    ```java
    // Initialize Firebase in your main activity
    import com.google.firebase.FirebaseApp;

    public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FirebaseApp.initializeApp(this);
    }
    }
    ```

  • iOS:
    ```swift
    // Initialize Firebase in your AppDelegate
    import Firebase

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()
    return true
    }
    }
    ```

Step 3: Add Image Input for Text Recognition

  • Android XML Layout:
    ```xml

  • iOS Storyboard:

  • Add an UIImageView and a UIButton to your storyboard.

  • Create IBOutlet and IBAction connections for both.

Step 4: Capture Image Using Camera or Choose from Gallery

  • Android:
    ```java
    // Inside your Activity
    Button captureButton = findViewById(R.id.button_capture);
    captureButton.setOnClickListener(v -> {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
    });

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
    Bundle extras = data.getExtras();
    Bitmap imageBitmap = (Bitmap) extras.get("data");
    // Set image to your ImageView
    ImageView imageView = findViewById(R.id.image_view);
    imageView.setImageBitmap(imageBitmap);
    // Pass imageBitmap to the text recognition function
    recognizeText(imageBitmap);
    }
    }
    ```

  • iOS:
    ```swift
    @IBAction func captureImage(_ sender: UIButton) {
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .camera
    present(imagePicker, animated: true, completion: nil)
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    picker.dismiss(animated: true, completion: nil)
    if let image = info[.originalImage] as? UIImage {
    imageView.image = image
    // Pass image to text recognition function
    recognizeText(from: image)
    }
    }
    ```

Step 5: Implement Text Recognition Using Firebase ML Kit

  • Android:
    ```java
    import com.google.firebase.ml.vision.FirebaseVision;
    import com.google.firebase.ml.vision.common.FirebaseVisionImage;
    import com.google.firebase.ml.vision.text.FirebaseVisionText;
    import com.google.firebase.ml.vision.text.FirebaseVisionTextRecognizer;

    private void recognizeText(Bitmap bitmap) {
    FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
    FirebaseVisionTextRecognizer detector = FirebaseVision.getInstance().getOnDeviceTextRecognizer();

    detector.processImage(image)
    .addOnSuccessListener(texts -> processResultText(texts))
    .addOnFailureListener(e -> {
    // Task failed with an exception
    e.printStackTrace();
    });
    }

    private void processResultText(FirebaseVisionText firebaseVisionText) {
    List<FirebaseVisionText.TextBlock> blocks = firebaseVisionText.getTextBlocks();
    if (blocks.size() == 0) {
    // No text found
    return;
    }
    for (FirebaseVisionText.TextBlock block : blocks) {
    String blockText = block.getText();
    // Do something with the recognized text
    Log.d("Text Recognition", blockText);
    }
    }
    ```

  • iOS:
    ```swift
    import FirebaseMLVision

    func recognizeText(from image: UIImage) {
    let vision = Vision.vision()
    let textRecognizer = vision.onDeviceTextRecognizer()

    let visionImage = VisionImage(image: image)
    textRecognizer.process(visionImage) { result, error in
    guard error == nil, let result = result else {
    // Error handling
    print("Error in text recognition: (error?.localizedDescription ?? "No error description")")
    return
    }
    for block in result.blocks {
    let blockText = block.text
    // Do something with the recognized text
    print("Recognized Text: (blockText)")
    }
    }
    }
    ```

Explore more Firebase tutorials

Complete Guide to Firebase: Tutorials, Tips, and Best Practices

Explore our Firebase tutorials directory - an essential resource for learning how to create, deploy and manage robust server-side applications with ease and efficiency.

Why are companies choosing Bootstrapped?

40-60%

Faster with no-code

Nocode tools allow us to develop and deploy your new application 40-60% faster than regular app development methods.

90 days

From idea to MVP

Save time, money, and energy with an optimized hiring process. Access a pool of experts who are sourced, vetted, and matched to meet your precise requirements.

1 283 apps

built by our developers

With the Bootstrapped platform, managing projects and developers has never been easier.

hero graphic

Our capabilities

Bootstrapped offers a comprehensive suite of capabilities tailored for startups. Our expertise spans web and mobile app development, utilizing the latest technologies to ensure high performance and scalability. The team excels in creating intuitive user interfaces and seamless user experiences. We employ agile methodologies for flexible and efficient project management, ensuring timely delivery and adaptability to changing requirements. Additionally, Bootstrapped provides continuous support and maintenance, helping startups grow and evolve their digital products. Our services are designed to be affordable and high-quality, making them an ideal partner for new ventures.

Engineered for you

1

Fast Development: Bootstrapped specializes in helping startup founders build web and mobile apps quickly, ensuring a fast go-to-market strategy.

2

Tailored Solutions: The company offers customized app development, adapting to specific business needs and goals, which ensures your app stands out in the competitive market.

3

Expert Team: With a team of experienced developers and designers, Bootstrapped ensures high-quality, reliable, and scalable app solutions.

4

Affordable Pricing: Ideal for startups, Bootstrapped offers cost-effective development services without compromising on quality.

5

Supportive Partnership: Beyond development, Bootstrapped provides ongoing support and consultation, fostering long-term success for your startup.

6

Agile Methodology: Utilizing agile development practices, Bootstrapped ensures flexibility, iterative progress, and swift adaptation to changes, enhancing project success.

Yes, if you can dream it, we can build it.