从图片中获取用户Singnature Flutter

g9icjywg  于 2023-05-30  发布在  Flutter
关注(0)|答案(1)|浏览(169)

你好任何方式来获得用户签名从图像,我想要求获得用户签名从图像,所以有可能做到这一点你的文字
上面提到的问题在Python中通过Signature Extractor解决了,但是如何解决Flutter中的问题呢
签名提取器github链接:https://github.com/ahmetozlu/signature_extractor

db2dz4w8

db2dz4w81#

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:flutter_image/flutter_image.dart' as img;
import 'package:path_provider/path_provider.dart';

class SignatureExtractor extends StatefulWidget {
  @override
  _SignatureExtractorState createState() => _SignatureExtractorState();
}

class _SignatureExtractorState extends State<SignatureExtractor> {
  File? _imageFile;

  Future<void> _getImageFromGallery() async {
    final pickedImage =
        await ImagePicker().getImage(source: ImageSource.gallery);
    if (pickedImage != null) {
      setState(() {
        _imageFile = File(pickedImage.path);
      });
    }
  }

  Future<void> _extractSignature() async {
    if (_imageFile == null) return;

    final signatureImage = await img.decodeImageFromList(
      await _imageFile!.readAsBytes(),
      colorSpace: ColorSpace.srgb,
    );

    // Perform image processing and signature extraction here
    // Apply techniques like thresholding, edge detection, contour detection, etc.

    // Once you have the signature image, you can save it to a file or use it as needed
    final appDir = await getApplicationDocumentsDirectory();
    final signaturePath = '${appDir.path}/signature.png';
    await signatureImage.saveTo(signaturePath);
    print('Signature extracted and saved to: $signaturePath');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Signature Extractor'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            if (_imageFile != null)
              Image.file(_imageFile!, width: 200, height: 200),
            ElevatedButton(
              onPressed: _getImageFromGallery,
              child: Text('Select Image'),
            ),
            ElevatedButton(
              onPressed: _extractSignature,
              child: Text('Extract Signature'),
            ),
          ],
        ),
      ),
    );
  }
}

void main() {
  runApp(MaterialApp(
    home: SignatureExtractor(),
  ));
}

相关问题