flutter_ocr_text_recognization 0.0.1 flutter_ocr_text_recognization: ^0.0.1 copied to clipboard
依赖 google_mlkit_text_recognition 实现的 OCR 文本识别相机,支持局部识别
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_ocr_text_recognization/flutter_ocr_text_recognization.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'OCR Text Scan',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'OCR Text Scan'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String text = "";
final StreamController<String> controller = StreamController<String>();
void setText(value) {
controller.add(value);
}
@override
void dispose() {
controller.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(
vertical: 10,
horizontal: 10,
),
child: TextOrcScan(
paintboxCustom: Paint()
..style = PaintingStyle.stroke
..strokeWidth = 4.0
..color = const Color.fromARGB(153, 102, 160, 241),
boxRadius: 12,
painBoxLeftOff: 5,
painBoxBottomOff: 2.5,
painBoxRightOff: 5,
painBoxTopOff: 2.5,
widgetHeight: MediaQuery.of(context).size.height / 3,
getScannedText: (value) {
setText(value);
},
),
),
StreamBuilder<String>(
stream: controller.stream,
builder:
(BuildContext context, AsyncSnapshot<String> snapshot) {
return ScanResult(
text: snapshot.data != null ? snapshot.data! : "");
},
)
],
),
));
}
}
class ScanResult extends StatelessWidget {
const ScanResult({
Key? key,
required this.text,
}) : super(key: key);
final String text;
@override
Widget build(BuildContext context) {
return Text("识别结果 : $text");
}
}