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

时间:2018-09-06 20:50:46

标签: flutter signature pad

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

Tengo este codigo y no se como puedo guardar la foto de la firma en el dispositivo alguien sabe como puedo hacerlo吗? necesito ayuda的nedesito obtener esta照片

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.
    );
  }
}

2 个答案:

答案 0 :(得分: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
  • 使用记录器和Canvas创建一个Size
  • 现在在画布上绘制路径。
  • recorder.endRecording()
  • 结束录音
  • 使用ui.Image获取picture.toImage对象
  • 使用image.toByteData(format: ui.ImageByteFormat.png)获取png格式的图像字节
  • 将其保存到文件,然后将其流式传输到服务器...

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

答案 1 :(得分:0)

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

现在,您必须将 $(document).ready(function() { var table = $('.export-table').DataTable({ dom: 'Bfrtip', 'bServerSide': true, 'sAjaxSource': 'api', buttons: ['copy', 'excel', 'pdf'] }); table.buttons().container().appendTo('#export-table_wrapper .col-sm-6:eq(0)'); }); 转换为pngBytes字节。

然后使用List<int>将画布存储为png

这是一个例子

File('$path/filename.png').writeAsBytesSync(pngBytes.buffer.asInt8List())

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