doc_clean_page.dart 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:fluttertoast/fluttertoast.dart';
  4. import 'package:gallery_saver/gallery_saver.dart';
  5. import 'package:native_vision/native/native_vision.dart';
  6. import 'loading_overlay.dart';
  7. class DocCleanPage extends StatefulWidget {
  8. final String imgPath;
  9. const DocCleanPage({Key? key, required this.imgPath}) : super(key: key);
  10. @override
  11. State<StatefulWidget> createState() {
  12. return DocCleanPageState();
  13. }
  14. }
  15. class DocCleanPageState extends State<DocCleanPage> {
  16. String? cleanedImgPath;
  17. void cleanImage(String imgPath) async {
  18. LoadingOverlay.of(context).show();
  19. int docFilterPtr = await DocCleanFilter.init();
  20. final cleanedImgPath = await DocCleanFilter.process(imgPath, docFilterPtr);
  21. await DocCleanFilter.release(docFilterPtr);
  22. if (!mounted) return;
  23. setState(() {
  24. this.cleanedImgPath = cleanedImgPath;
  25. });
  26. LoadingOverlay.of(context).hide();
  27. }
  28. @override
  29. Widget build(BuildContext context) {
  30. return Scaffold(
  31. appBar: AppBar(title: const Text("文档增强"), actions: [
  32. TextButton(
  33. onPressed: () async {
  34. if (cleanedImgPath == null) {
  35. Fluttertoast.showToast(msg: "请先做增强处理");
  36. return;
  37. }
  38. final succ = await GallerySaver.saveImage(cleanedImgPath!);
  39. Fluttertoast.showToast(msg: succ != null && succ ? "成功" : "失败");
  40. },
  41. child: const Text('保存',
  42. style: TextStyle(
  43. fontSize: 18,
  44. color: Colors.white,
  45. )),
  46. ),
  47. ]),
  48. body: Stack(fit: StackFit.expand, children: [
  49. Image.file(File(cleanedImgPath ?? widget.imgPath)),
  50. Positioned(
  51. bottom: 20,
  52. left: 0,
  53. right: 0,
  54. child: Center(
  55. child: FloatingActionButton(
  56. child: const Icon(Icons.check),
  57. onPressed: () {
  58. cleanImage(widget.imgPath);
  59. },
  60. ),
  61. ),
  62. )
  63. ]));
  64. }
  65. }