doc_crop_page.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:image_size_getter/file_input.dart';
  4. import 'package:image_size_getter/image_size_getter.dart';
  5. import 'package:native_vision/edge_detection.dart';
  6. import 'package:native_vision/widget/edge_detection_shape.dart';
  7. import 'dart:ui' as ui;
  8. import 'package:native_vision/native/doc_cropper.dart';
  9. import 'package:native_vision_example/doc_clean_page.dart';
  10. import 'loading_overlay.dart';
  11. class DocCropPage extends StatefulWidget {
  12. final String imgPath;
  13. final EdgeDetectionResult edgeDetectionResult;
  14. const DocCropPage(
  15. {Key? key, required this.imgPath, required this.edgeDetectionResult})
  16. : super(key: key);
  17. @override
  18. State<StatefulWidget> createState() {
  19. return DocCropPageState();
  20. }
  21. }
  22. class DocCropPageState extends State<DocCropPage> {
  23. GlobalKey imageWidgetKey = GlobalKey();
  24. Future<Map<String, ui.Size>> calculateImgSize() async {
  25. final screenSize = MediaQuery.of(context).size;
  26. final imgSize = ImageSizeGetter.getSize(FileInput(File(widget.imgPath)));
  27. final double width, height;
  28. if (imgSize.needRotate) {
  29. width = imgSize.height.toDouble();
  30. height = imgSize.width.toDouble();
  31. } else {
  32. width = imgSize.width.toDouble();
  33. height = imgSize.height.toDouble();
  34. }
  35. final aspectRatio = width / height;
  36. final imgW = screenSize.width;
  37. final imgH = imgW / aspectRatio;
  38. return {
  39. "renderedImageSize": ui.Size(imgW, imgH),
  40. "originalImageSize": ui.Size(width, height)
  41. };
  42. }
  43. @override
  44. Widget build(BuildContext context) {
  45. return Scaffold(
  46. appBar: AppBar(title: const Text("裁剪")),
  47. body: Stack(children: [
  48. FutureBuilder<Map<String, ui.Size>>(
  49. future: calculateImgSize(),
  50. builder: (context, snapshot) {
  51. final data = snapshot.data;
  52. if (data != null) {
  53. final renderedImageSize = data["renderedImageSize"];
  54. final originalImageSize = data["originalImageSize"];
  55. return Container(
  56. alignment: Alignment.center,
  57. color: Colors.blue,
  58. child: Stack(
  59. children: [
  60. Image.file(
  61. File(widget.imgPath),
  62. key: imageWidgetKey,
  63. width: renderedImageSize!.width,
  64. height: renderedImageSize.height,
  65. cacheHeight: (renderedImageSize.height *
  66. MediaQuery.of(context).devicePixelRatio)
  67. .toInt(),
  68. cacheWidth: (renderedImageSize.width *
  69. MediaQuery.of(context).devicePixelRatio)
  70. .toInt(),
  71. ),
  72. SizedBox(
  73. width: renderedImageSize.width,
  74. height: renderedImageSize.height,
  75. child: EdgeDetectionShape(
  76. renderedImageSize: renderedImageSize,
  77. originalImageSize: originalImageSize!,
  78. edgeDetectionResult: widget.edgeDetectionResult),
  79. )
  80. ],
  81. ),
  82. );
  83. }
  84. return Container();
  85. }),
  86. Positioned(
  87. bottom: 20,
  88. left: 0,
  89. right: 0,
  90. child: Center(
  91. child: FloatingActionButton(
  92. child: const Icon(Icons.check),
  93. onPressed: () async {
  94. LoadingOverlay.of(context).show();
  95. final croppedImgPath = await DocCropper.crop(
  96. widget.imgPath, widget.edgeDetectionResult);
  97. if (mounted && croppedImgPath != null) {
  98. debugPrint("croppedImgPath=$croppedImgPath");
  99. LoadingOverlay.of(context).hide();
  100. Navigator.push(
  101. context,
  102. MaterialPageRoute(
  103. builder: (context) => LoadingOverlay(
  104. child: DocCleanPage(imgPath: croppedImgPath)),
  105. ));
  106. }
  107. },
  108. ),
  109. ),
  110. )
  111. ]),
  112. );
  113. }
  114. }