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 createState() => _DocScanPageState(); } class _DocScanPageState extends State { bool takingPic = false; GlobalKey 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"), ), ) ], ), ) ], ), ), ); } }