ocr_plugin_flutter 1.0.0 ocr_plugin_flutter: ^1.0.0 copied to clipboard
ocr_plugin_flutter project.
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:ocr_plugin_flutter_example/ocr_scan.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(home: HomePage());
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _MyAppState();
}
class _MyAppState extends State<HomePage> {
Image _imageFront = Image.asset("images/pic_avatar.png",
width: 300, height: 160, fit: BoxFit.cover);
Image _imageBack = Image.asset("images/pic_emblem.png",
width: 300, height: 160, fit: BoxFit.cover);
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('易盾身份证ocr检测'),
),
body: Center(
child: _buildContent(context),
),
),
);
}
Widget _buildContent(BuildContext context) {
return Column(children: [
showText(),
GestureDetector(
onTap: () {
if (defaultTargetPlatform == TargetPlatform.android) {
checkPermission().then((value) => {
if (value) {push(context, "front")}
});
} else {
push(context, "front");
}
},
child: _imageFront),
showTextFront(),
GestureDetector(
onTap: () {
if (defaultTargetPlatform == TargetPlatform.android) {
checkPermission().then((value) => {
if (value) {push(context, "back")}
});
} else {
push(context, "back");
}
},
child: _imageBack),
showTextBack()
]);
}
Widget showText() {
return Container(
padding: const EdgeInsets.fromLTRB(0, 20, 0, 20),
alignment: Alignment.topCenter,
child: const Text("身份证OCR体验Demo",
style: TextStyle(
color: Colors.black,
fontSize: 24,
fontFamily: "PingFangSC-Semibold")));
}
Widget showTextFront() {
return Container(
padding: const EdgeInsets.fromLTRB(0, 20, 0, 20),
alignment: Alignment.topCenter,
child: const Text("拍摄人面像",
style: TextStyle(
color: Color(0xFF666666),
fontSize: 16,
fontFamily: "PingFangSC-Semibold")));
}
Widget showTextBack() {
return Container(
padding: const EdgeInsets.fromLTRB(0, 20, 0, 20),
alignment: Alignment.topCenter,
child: const Text("拍摄国徽面",
style: TextStyle(
color: Color(0xFF666666),
fontSize: 16,
fontFamily: "PingFangSC-Semibold")));
}
void push(BuildContext context, String scanType) {
Future result = Navigator.push(
context,
MaterialPageRoute(
builder: (context) => OcrScan(
scanType: scanType,
)));
var that = this;
result.then((value) => that.setState(() {
if ("front" == scanType) {
that._imageFront = Image.file(File(value),
width: 300, height: 160, fit: BoxFit.cover);
} else {
that._imageBack = Image.file(File(value),
width: 300, height: 160, fit: BoxFit.cover);
}
}));
}
// ignore: missing_return
Future<bool> checkPermission() async {
//当前权限
Permission permission = Permission.camera;
//权限的状态
PermissionStatus status = await permission.status;
if (status.isDenied) {
//发起权限申请
PermissionStatus status = await permission.request();
if (status.isGranted) {
return true;
} else {
return false;
}
} else {
return true;
}
}
}
/// 封装 按钮
class CustomButton extends StatelessWidget {
final VoidCallback onPressed;
final String title;
// ignore: use_key_in_widget_constructors
const CustomButton({required this.onPressed, required this.title});
@override
Widget build(BuildContext context) {
// TODO: implement build
return new TextButton(
onPressed: onPressed,
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.resolveWith((states) => Colors.blue)),
child: new Text(
"$title",
style: TextStyle(fontSize: 13, color: Colors.white),
),
);
}
}