|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:typed_data'; |
| 3 | +import 'dart:ui' as ui; |
| 4 | + |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | +import 'package:flutter/rendering.dart'; |
| 7 | +import 'package:google_mlkit_text_recognition/google_mlkit_text_recognition.dart'; |
| 8 | + |
| 9 | +class TextFromWidgetView extends StatefulWidget { |
| 10 | + const TextFromWidgetView({Key? key}) : super(key: key); |
| 11 | + |
| 12 | + @override |
| 13 | + State<TextFromWidgetView> createState() => _TextFromWidgetViewState(); |
| 14 | +} |
| 15 | + |
| 16 | +class _TextFromWidgetViewState extends State<TextFromWidgetView> { |
| 17 | + final _textRecognizer = TextRecognizer(script: TextRecognitionScript.latin); |
| 18 | + final _widgetKey = GlobalKey(); |
| 19 | + String _extractedText = 'Recognized text will appear here'; |
| 20 | + bool _isBusy = false; |
| 21 | + |
| 22 | + @override |
| 23 | + void dispose() { |
| 24 | + _textRecognizer.close(); |
| 25 | + super.dispose(); |
| 26 | + } |
| 27 | + |
| 28 | + @override |
| 29 | + Widget build(BuildContext context) { |
| 30 | + return Scaffold( |
| 31 | + appBar: AppBar( |
| 32 | + title: const Text('Text From Widget Example'), |
| 33 | + ), |
| 34 | + body: Column( |
| 35 | + children: [ |
| 36 | + Expanded( |
| 37 | + child: Center( |
| 38 | + child: RepaintBoundary( |
| 39 | + key: _widgetKey, |
| 40 | + child: Container( |
| 41 | + padding: const EdgeInsets.all(16.0), |
| 42 | + decoration: BoxDecoration( |
| 43 | + color: Colors.white, |
| 44 | + border: Border.all(color: Colors.blue, width: 2), |
| 45 | + borderRadius: BorderRadius.circular(12), |
| 46 | + ), |
| 47 | + child: const Text( |
| 48 | + 'This is sample text\nthat will be captured\nand processed using\nthe ML Kit Text Recognizer.\n\nTry different fonts\nand styles to test\nthe recognition capabilities!', |
| 49 | + style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), |
| 50 | + ), |
| 51 | + ), |
| 52 | + ), |
| 53 | + ), |
| 54 | + ), |
| 55 | + Padding( |
| 56 | + padding: const EdgeInsets.all(16.0), |
| 57 | + child: ElevatedButton( |
| 58 | + onPressed: _isBusy ? null : _extractTextFromWidget, |
| 59 | + child: const Text('Capture and Recognize Text'), |
| 60 | + ), |
| 61 | + ), |
| 62 | + Expanded( |
| 63 | + child: SingleChildScrollView( |
| 64 | + padding: const EdgeInsets.all(16.0), |
| 65 | + child: Column( |
| 66 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 67 | + children: [ |
| 68 | + const Text( |
| 69 | + 'Recognition Result:', |
| 70 | + style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18), |
| 71 | + ), |
| 72 | + const SizedBox(height: 8), |
| 73 | + Container( |
| 74 | + padding: const EdgeInsets.all(12), |
| 75 | + decoration: BoxDecoration( |
| 76 | + color: Colors.grey[200], |
| 77 | + borderRadius: BorderRadius.circular(8), |
| 78 | + ), |
| 79 | + width: double.infinity, |
| 80 | + child: Text(_extractedText), |
| 81 | + ), |
| 82 | + ], |
| 83 | + ), |
| 84 | + ), |
| 85 | + ), |
| 86 | + ], |
| 87 | + ), |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + Future<void> _extractTextFromWidget() async { |
| 92 | + if (_isBusy) return; |
| 93 | + |
| 94 | + setState(() { |
| 95 | + _isBusy = true; |
| 96 | + _extractedText = 'Processing...'; |
| 97 | + }); |
| 98 | + |
| 99 | + try { |
| 100 | + // Get the RenderObject from the GlobalKey |
| 101 | + final RenderRepaintBoundary? boundary = _widgetKey.currentContext |
| 102 | + ?.findRenderObject() as RenderRepaintBoundary?; |
| 103 | + |
| 104 | + if (boundary == null) { |
| 105 | + setState(() { |
| 106 | + _extractedText = 'Error: Unable to find widget render object'; |
| 107 | + _isBusy = false; |
| 108 | + }); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + // Capture the widget as an image |
| 113 | + final ui.Image image = await boundary.toImage(pixelRatio: 3.0); |
| 114 | + |
| 115 | + // Convert to byte data in raw RGBA format |
| 116 | + final ByteData? byteData = |
| 117 | + await image.toByteData(format: ui.ImageByteFormat.rawRgba); |
| 118 | + |
| 119 | + if (byteData == null) { |
| 120 | + setState(() { |
| 121 | + _extractedText = 'Error: Failed to get image byte data'; |
| 122 | + _isBusy = false; |
| 123 | + }); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + final Uint8List bytes = byteData.buffer.asUint8List(); |
| 128 | + |
| 129 | + // Create InputImage from bitmap data with dimensions |
| 130 | + final inputImage = InputImage.fromBitmap( |
| 131 | + bitmap: bytes, |
| 132 | + width: image.width, |
| 133 | + height: image.height, |
| 134 | + ); |
| 135 | + |
| 136 | + // Process the image with the text recognizer |
| 137 | + final RecognizedText recognizedText = |
| 138 | + await _textRecognizer.processImage(inputImage); |
| 139 | + |
| 140 | + setState(() { |
| 141 | + _extractedText = recognizedText.text.isNotEmpty |
| 142 | + ? recognizedText.text |
| 143 | + : 'No text recognized'; |
| 144 | + _isBusy = false; |
| 145 | + }); |
| 146 | + } catch (e) { |
| 147 | + setState(() { |
| 148 | + _extractedText = 'Error processing image: $e'; |
| 149 | + _isBusy = false; |
| 150 | + }); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments