123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:fluttertoast/fluttertoast.dart';
- import 'package:image_picker/image_picker.dart';
- import 'package:image_size_getter/file_input.dart';
- import 'package:native_vision/edge_detection.dart';
- import 'package:native_vision/native/doc_scanner.dart';
- import 'package:native_vision_example/doc_crop_page.dart';
- import 'package:native_vision/widget/doc_preview.dart';
- import 'package:path_provider/path_provider.dart';
- import 'package:image_size_getter/image_size_getter.dart';
- import 'loading_overlay.dart';
- class DocScanPage extends StatefulWidget {
- const DocScanPage({Key? key}) : super(key: key);
- @override
- State<DocScanPage> createState() => _DocScanPageState();
- }
- class _DocScanPageState extends State<DocScanPage> {
- bool takingPic = false;
- GlobalKey<DocPreviewState> globalKey = GlobalKey();
- @override
- Widget build(BuildContext context) {
- final size = MediaQuery.of(context).size;
- final displayW = size.width;
- final displayH = displayW / 0.75;
- return Scaffold(
- appBar: AppBar(title: const Text("文档扫描")),
- body: SafeArea(
- child: Stack(
- fit: StackFit.expand,
- children: [
- DocPreview(key: globalKey, displayW: displayW, displayH: displayH),
- Positioned(
- left: 50,
- right: 50,
- bottom: 10,
- child: Column(
- children: [
- SizedBox(
- width: 200,
- height: 60,
- child: TextButton(
- onPressed: () async {
- if (takingPic) return;
- LoadingOverlay.of(context).show();
- takingPic = true;
- final ret = await globalKey.currentState?.takePic();
- if (!mounted) return;
- takingPic = false;
- if (ret != null) {
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => LoadingOverlay(
- child: DocCropPage(
- imgPath: ret["ImgPath"],
- edgeDetectionResult:
- ret["EdgeDetectionResult"]),
- )),
- );
- } else {
- Fluttertoast.showToast(
- msg: "This is Center Short Toast",
- toastLength: Toast.LENGTH_SHORT,
- gravity: ToastGravity.CENTER,
- timeInSecForIosWeb: 1,
- backgroundColor: Colors.red,
- textColor: Colors.white,
- fontSize: 16.0);
- }
- LoadingOverlay.of(context).hide();
- },
- style:
- TextButton.styleFrom(backgroundColor: Colors.green),
- child: const Text("Take Picture"),
- ),
- ),
- SizedBox(
- width: 200,
- height: 60,
- child: TextButton(
- onPressed: () async {
- final ImagePicker imagePicker = ImagePicker();
- // Pick an image
- final XFile? image = await imagePicker.pickImage(
- source: ImageSource.gallery);
- if (image != null) {
- final size = ImageSizeGetter.getSize(
- FileInput(File(image.path)));
- final tmpDir = (await getTemporaryDirectory()).path;
- final outPath =
- "$tmpDir/roi_${DateTime.now().millisecondsSinceEpoch}.jpg";
- DocScanner docScanner = DocScanner();
- await docScanner.init(0.85);
- var h = size.height;
- var w = size.width;
- if (size.needRotate) {
- h = size.width;
- w = size.height;
- }
- final result = await docScanner.singleScan(image.path,
- outPath, ROI.create(0, 0, w, h, 0, 0));
- await docScanner.release();
- if (result != null) {
- if (!mounted) return;
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => LoadingOverlay(
- child: DocCropPage(
- imgPath: outPath,
- edgeDetectionResult: result),
- )),
- );
- } else {
- Fluttertoast.showToast(
- msg: "未识别到文档",
- toastLength: Toast.LENGTH_SHORT,
- gravity: ToastGravity.CENTER,
- timeInSecForIosWeb: 1,
- backgroundColor: Colors.red,
- textColor: Colors.white,
- fontSize: 16.0);
- }
- }
- },
- // style:
- // TextButton.styleFrom(backgroundColor: Colors.green),
- child: const Text("Select Photo"),
- ),
- )
- ],
- ),
- )
- ],
- ),
- ),
- );
- }
- }
|