1

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

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

2 回答 2

3

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

现在,您必须转换pngBytesList<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

于 2018-09-12T05:08:45.770 回答
0

颤振示例中的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
  • 获取 png 格式的图像字节image.toByteData(format: ui.ImageByteFormat.png)
  • 将其保存到文件中,将其流式传输到服务器...

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

于 2018-09-06T22:59:24.700 回答