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!
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.
Add project
button and just follow the steps on the screen.google-services.json
or GoogleService-Info.plist
file and put it in the right directory in 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
pod 'Firebase/MLVisionTextModel'
```
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
}
}
```
Android XML Layout:
```xml
```
iOS Storyboard:
Add an UIImageView
and a UIButton
to your storyboard.
Create IBOutlet and IBAction connections for both.
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)
}
}
```
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 our Firebase tutorials directory - an essential resource for learning how to create, deploy and manage robust server-side applications with ease and efficiency.
Nocode tools allow us to develop and deploy your new application 40-60% faster than regular app development methods.
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.
With the Bootstrapped platform, managing projects and developers has never been easier.
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.
Fast Development: Bootstrapped specializes in helping startup founders build web and mobile apps quickly, ensuring a fast go-to-market strategy.
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.
Expert Team: With a team of experienced developers and designers, Bootstrapped ensures high-quality, reliable, and scalable app solutions.
Affordable Pricing: Ideal for startups, Bootstrapped offers cost-effective development services without compromising on quality.
Supportive Partnership: Beyond development, Bootstrapped provides ongoing support and consultation, fostering long-term success for your startup.
Agile Methodology: Utilizing agile development practices, Bootstrapped ensures flexibility, iterative progress, and swift adaptation to changes, enhancing project success.