text_recognition_camera 0.0.5
text_recognition_camera: ^0.0.5 copied to clipboard
A Flutter library for text recognition using device camera and Google ML Kit with pattern matching support.
import 'package:flutter/material.dart';
import 'package:text_recognition_camera/text_recognition_camera.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Reconocimiento de Texto',
theme: ThemeData(
primarySwatch: Colors.blue,
),
darkTheme: ThemeData(
brightness: Brightness.dark,
primarySwatch: Colors.blue,
scaffoldBackgroundColor: Colors.black,
appBarTheme: const AppBarTheme(
backgroundColor: Colors.black,
),
),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String recognizedText = "El número de serie reconocido aparecerá aquí";
String? imagePath;
@override
Widget build(BuildContext context) {
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
final colors = isDarkMode ? darkColors : lightColors;
return Scaffold(
appBar: AppBar(
title: const Text('Reconocimiento de Texto'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
margin: const EdgeInsets.symmetric(horizontal: 16),
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Theme.of(context).cardColor,
borderRadius: BorderRadius.circular(8),
border: Border.all(color: Theme.of(context).dividerColor),
),
child: Column(
children: [
Text(
'Número de Serie',
style: TextStyle(
fontSize: 16,
color: Theme.of(context).textTheme.bodyMedium?.color,
fontWeight: FontWeight.w500,
),
),
const SizedBox(height: 8),
Text(
recognizedText,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
letterSpacing: 1,
color: Theme.of(context).textTheme.bodyLarge?.color,
),
textAlign: TextAlign.center,
),
],
),
),
const SizedBox(height: 20),
ElevatedButton.icon(
onPressed: () async {
final result = await showDialog<RecognitionResult>(
context: context,
builder: (context) => TextRecognitionCamera(
key: const Key('textRecognitionCamera'),
title: "Escanear número de serie",
pattern: r'(?:^|\D)(\d{12})(?:\D|$)',
notFoundMessage:
"No se pudo encontrar un número de serie válido.\nPor favor, asegúrese de que la imagen contiene un número de 12 dígitos.",
successMessage: "Número de serie verificado",
width: 450,
height: 700,
backgroundColor: colors['backgroundColor']!,
primaryColor: colors['primaryColor']!,
successColor: colors['successColor']!,
errorColor: colors['errorColor']!,
textColor: colors['textColor']!,
secondaryTextColor: colors['secondaryTextColor']!,
buttonBackgroundColor: colors['buttonBackgroundColor']!,
buttonForegroundColor: colors['buttonForegroundColor']!,
containerBackgroundColor: colors['containerBackgroundColor']!,
),
);
if (result != null) {
setState(() {
recognizedText = result.text;
imagePath = result.imagePath;
});
debugPrint('Path de la imagen: ${result.imagePath}');
}
},
icon: const Icon(Icons.document_scanner_outlined),
label: const Text(
'Escanear Número de Serie',
style: TextStyle(fontSize: 16),
),
style: ElevatedButton.styleFrom(
minimumSize: const Size(250, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
],
),
),
);
}
}
const lightColors = {
'backgroundColor': Color(0xFFFAFAFA),
'primaryColor': Color(0xFF1976D2),
'successColor': Color(0xFF388E3C),
'errorColor': Color(0xFFD32F2F),
'textColor': Colors.black87,
'secondaryTextColor': Colors.black54,
'buttonBackgroundColor': Color(0xFFEEEEEE),
'buttonForegroundColor': Color(0xFF424242),
'containerBackgroundColor': Color(0xFFEEEEEE),
};
const darkColors = {
'backgroundColor': Color(0xFF303030),
'primaryColor': Color(0xFF64B5F6),
'successColor': Color(0xFF81C784),
'errorColor': Color(0xFFE57373),
'textColor': Colors.white,
'secondaryTextColor': Colors.white70,
'buttonBackgroundColor': Color(0xFF424242),
'buttonForegroundColor': Color(0xFFEEEEEE),
'containerBackgroundColor': Color(0xFF424242),
};