简体   繁体   English

未来<uint8list>到 Flutter 中的 Base64 或 PNG</uint8list>

[英]Future<Uint8List> to Base64 or PNG in Flutter

I need to convert a Future<Uint8List> to Base64 or PNG in Flutter, I am using this pub to get signature and export it but when I call toPngBytes() method (method in pub) it returns a Future<Uint8List> and I need to convert it to Base64 format or List<int> at least ByteData format, I can not convert it to a more usable format for me, can anyone help me to resolve this.我需要在Future<Uint8List>转换为 Base64 或 PNG,我正在使用这个 pub来获取签名并导出它,但是当我调用toPngBytes()方法(pub 中的方法)它返回一个Future<Uint8List>我需要要将其转换为Base64格式或List<int>至少ByteData格式,我无法将其转换为对我更有用的格式,谁能帮我解决这个问题。

_controller.toPngBytes(); // _controller is a variable that holds info about my signature.
import 'dart:convert';

// async variant
final imageData = await _controller.toPngBytes(); // must be called in async method
final imageEncoded = base64.encode(imageData); // returns base64 string

// callback variant
_controller.toPngBytes().then((data) {
 final imageEncoded = base64.encode(data); 
});

This function made me upload an image to Xano correctly:这个 function 让我正确地将图像上传到Xano

import 'dart:convert';
import 'dart:typed_data';

String uint8ListTob64(Uint8List uint8list) {
  String base64String = base64Encode(uint8list);
  String header = "data:image/png;base64,";
  return header + base64String;
}

You can transform an image into Uint8List in the following way:您可以通过以下方式将图像转换为 Uint8List:

String path = "image.png";
File file = File(path);
Uint8List uint8list = file.readAsBytesSync();
//or
Uint8List uint8list = await file.readAsBytes();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM