123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:image_size_getter/file_input.dart';
- import 'package:image_size_getter/image_size_getter.dart';
- import 'package:native_vision/edge_detection.dart';
- import 'package:native_vision/widget/edge_detection_shape.dart';
- import 'dart:ui' as ui;
- import 'package:native_vision/native/doc_cropper.dart';
- import 'package:native_vision_example/doc_clean_page.dart';
- import 'loading_overlay.dart';
- class DocCropPage extends StatefulWidget {
- final String imgPath;
- final EdgeDetectionResult edgeDetectionResult;
- const DocCropPage(
- {Key? key, required this.imgPath, required this.edgeDetectionResult})
- : super(key: key);
- @override
- State<StatefulWidget> createState() {
- return DocCropPageState();
- }
- }
- class DocCropPageState extends State<DocCropPage> {
- GlobalKey imageWidgetKey = GlobalKey();
- Future<Map<String, ui.Size>> calculateImgSize() async {
- final screenSize = MediaQuery.of(context).size;
- final imgSize = ImageSizeGetter.getSize(FileInput(File(widget.imgPath)));
- final double width, height;
- if (imgSize.needRotate) {
- width = imgSize.height.toDouble();
- height = imgSize.width.toDouble();
- } else {
- width = imgSize.width.toDouble();
- height = imgSize.height.toDouble();
- }
- final aspectRatio = width / height;
- final imgW = screenSize.width;
- final imgH = imgW / aspectRatio;
- return {
- "renderedImageSize": ui.Size(imgW, imgH),
- "originalImageSize": ui.Size(width, height)
- };
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(title: const Text("裁剪")),
- body: Stack(children: [
- FutureBuilder<Map<String, ui.Size>>(
- future: calculateImgSize(),
- builder: (context, snapshot) {
- final data = snapshot.data;
- if (data != null) {
- final renderedImageSize = data["renderedImageSize"];
- final originalImageSize = data["originalImageSize"];
- return Container(
- alignment: Alignment.center,
- color: Colors.blue,
- child: Stack(
- children: [
- Image.file(
- File(widget.imgPath),
- key: imageWidgetKey,
- width: renderedImageSize!.width,
- height: renderedImageSize.height,
- cacheHeight: (renderedImageSize.height *
- MediaQuery.of(context).devicePixelRatio)
- .toInt(),
- cacheWidth: (renderedImageSize.width *
- MediaQuery.of(context).devicePixelRatio)
- .toInt(),
- ),
- SizedBox(
- width: renderedImageSize.width,
- height: renderedImageSize.height,
- child: EdgeDetectionShape(
- renderedImageSize: renderedImageSize,
- originalImageSize: originalImageSize!,
- edgeDetectionResult: widget.edgeDetectionResult),
- )
- ],
- ),
- );
- }
- return Container();
- }),
- Positioned(
- bottom: 20,
- left: 0,
- right: 0,
- child: Center(
- child: FloatingActionButton(
- child: const Icon(Icons.check),
- onPressed: () async {
- LoadingOverlay.of(context).show();
- final croppedImgPath = await DocCropper.crop(
- widget.imgPath, widget.edgeDetectionResult);
- if (mounted && croppedImgPath != null) {
- debugPrint("croppedImgPath=$croppedImgPath");
- LoadingOverlay.of(context).hide();
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => LoadingOverlay(
- child: DocCleanPage(imgPath: croppedImgPath)),
- ));
- }
- },
- ),
- ),
- )
- ]),
- );
- }
- }
|