在flutter中将图像转换为base64

时间:2020-10-04 11:13:38

标签: image flutter dart base64

如何将其转换为base64?我可以存储它,但是如何从那里获取base64?

我正在使用此插件https://pub.dev/packages/signature_pad/example

  final image = await sign.getData();
  var data = await image.toByteData(format: ui.ImageByteFormat.png);
  var savedDir = await getApplicationDocumentsDirectory();
  String appDocPath = savedDir.path;
  
  await Directory('$appDocPath/Signature/').create(recursive: true);
  var file = File(file).writeAsBytesSync(data.buffer.asInt8List());

1 个答案:

答案 0 :(得分:0)

我得到文件并这样做

List<int> imageBytes = await File(file).readAsBytes();
String base64Image = base64Encode(imageBytes);
print(base64Image);

完整代码

final image = await sign.getData();
var data = await image.toByteData(format: ui.ImageByteFormat.png);
var savedDir = await getApplicationDocumentsDirectory();
String appDocPath = savedDir.path;
await Directory('$appDocPath/Signature/').create(recursive: true);
var file = File(file).writeAsBytesSync(data.buffer.asInt8List());

List<int> imageBytes = await File(file).readAsBytes();
String base64Image = base64Encode(imageBytes);
print(base64Image);

有没有更清洁的方法?