将图像签名从颤动保存到设备

问题描述 投票:0回答:2

我有这个代码,我不知道如何将签名图像保存在设备中,任何人都知道我该怎么做?然后我需要再次检索这张照片我需要帮助。

我有这个代码,我不知道如何在设备上保存签名照片。有谁知道我怎么做?然后我需要再次拍摄这张照片,我需要帮助

import 'dart:convert';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter_signature_pad/flutter_signature_pad.dart';


void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  ByteData _img = ByteData(0);


  @override
  Widget build(BuildContext context) {
    var sign = Signature(
      color: Colors.red,
      strokeWidth: 5.0,
    );
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Expanded(child: sign),
              LimitedBox(
                  maxHeight: 200.0,
                  child: Image.memory(_img.buffer.asUint8List())),
              Padding(
                padding: EdgeInsets.all(25.0),
                child:
                Row(mainAxisAlignment: MainAxisAlignment.center, children: [
                  MaterialButton(
                      color: Colors.green,
                      onPressed: () async {
                        //retrieve image data, do whatever you want with it (send to server, save locally...)
                        var data = await sign
                            .getData()
                            .toByteData(format: ui.ImageByteFormat.png);
                        setState(() {
                          _img = data;
                        });
                        debugPrint("onPressed " +
                            base64.encode(data.buffer.asUint8List()));
                      },
                      child: Text("Save")),
                  MaterialButton(
                      color: Colors.grey,
                      onPressed: () {
                        sign.clear();
                        debugPrint("cleared");
                      },
                      child: Text("Clear")),
                ]),
              )
            ],
          ),
        ) // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
flutter signature pad
2个回答
0
投票

flutter示例中的Signature小部件只是在画布上绘制一条路径:

  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..color = Colors.black
      ..strokeCap = StrokeCap.round
      ..strokeWidth = 5.0;
    for (int i = 0; i < points.length - 1; i++) {
      if (points[i] != null && points[i + 1] != null)
        canvas.drawLine(points[i], points[i + 1], paint);
    }
  }

你需要找到一种方法

  • 创建自己的画布
  • 在画布上绘制路径
  • 渲染画布
  • 将渲染导出为png

以下是具体步骤:

  • 创建一个ui.PictureRecorder
  • 创建一个Canvasusing记录器和Size
  • 现在在画布上绘制路径。
  • recorder.endRecording()结束录音
  • ui.Image获取picture.toImage对象
  • 使用image.toByteData(format: ui.ImageByteFormat.png)以png格式获取图像字节
  • 将其保存到文件,将其流式传输到服务器...

我在这里的答案中描述了这些步骤(虽然用例不同):Flutter how to save(re-encode) text overlay on video


0
投票

pngBytes = image.toByteData(格式:ui.ImageByteFormat.png)

现在,你必须将pngBytes转换为List<int>字节。

然后使用File('$path/filename.png').writeAsBytesSync(pngBytes.buffer.asInt8List())将您的画布保存为png存储

这是一个例子

var pngBytes = await image.toByteData(format: ui.ImageByteFormat.png);
// requesting external storage permission
if(!(await checkPermission())) await requestPermission();

// Use plugin [path_provider] to export image to storage
Directory directory = await getExternalStorageDirectory();
String path = directory.path;
print(path);

// create directory on external storage
await Directory('$path/$directoryName').create(recursive: true);

// write to storage as a filename.png
File('$path/$directoryName/filename.png')
    .writeAsBytesSync(pngBytes.buffer.asInt8List());

有关完整示例,请访问https://github.com/vemarav/signature

© www.soinside.com 2019 - 2024. All rights reserved.