image_to_text_mlkit 1.0.1 copy "image_to_text_mlkit: ^1.0.1" to clipboard
image_to_text_mlkit: ^1.0.1 copied to clipboard

A Flutter package to convert images to text using Google MLKit with support for gallery, camera, and network URLs

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:image_to_text_mlkit/image_to_text_mlkit.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Image to Text Demo',
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  final ImageToTextMLKit _imageToTextMLKit = ImageToTextMLKit();
  String _extractedText = 'No text extracted yet';
  final TextEditingController _urlController = TextEditingController();

  /// Pick an image from the gallery
  Future<void> _pickImageFromGallery() async {
    String? text = await _imageToTextMLKit.pickImageFromGallery();
    setState(() {
      _extractedText = text ?? 'No text found';
    });
  }

  /// Capture an image from the camera
  Future<void> _pickImageFromCamera() async {
    String? text = await _imageToTextMLKit.pickImageFromCamera();
    setState(() {
      _extractedText = text ?? 'No text found';
    });
  }

  /// Process an image from a URL
  Future<void> _processImageFromUrl() async {
    if (_urlController.text.isEmpty) {
      setState(() {
        _extractedText = 'Please enter a valid URL.';
      });
      return;
    }

    String? text =
    await _imageToTextMLKit.processImageFromUrl(_urlController.text);
    setState(() {
      _extractedText = text ?? 'No text found';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Image to Text ML Kit')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: SingleChildScrollView(
          child: Column(
            children: [
              ElevatedButton(
                onPressed: _pickImageFromGallery,
                child: const Text('Pick Image from Gallery'),
              ),
              ElevatedButton(
                onPressed: _pickImageFromCamera,
                child: const Text('Pick Image from Camera'),
              ),
              const SizedBox(height: 20),
              TextField(
                controller: _urlController,
                decoration: const InputDecoration(
                  labelText: 'Enter Image URL',
                  border: OutlineInputBorder(),
                ),
              ),
              const SizedBox(height: 10),
              ElevatedButton(
                onPressed: _processImageFromUrl,
                child: const Text('Process Image from URL'),
              ),
              const SizedBox(height: 20),
              Text(
                _extractedText,
                textAlign: TextAlign.left,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
9
likes
0
points
57
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package to convert images to text using Google MLKit with support for gallery, camera, and network URLs

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, google_ml_kit, http, image_picker, path_provider

More

Packages that depend on image_to_text_mlkit