encodeToBase64 static method
Future<String?>
encodeToBase64({
- required Image image,
- MPFlutterImageByteFormat format = MPFlutterImageByteFormat.png,
- double compressQuality = 0.92,
保存 Image 成 Base64 字符串
Implementation
static Future<String?> encodeToBase64({
required ui.Image image,
MPFlutterImageByteFormat format = MPFlutterImageByteFormat.png,
double compressQuality = 0.92,
}) async {
final rawRgba = await image.toByteData(format: ui.ImageByteFormat.rawRgba);
if (rawRgba == null) return null;
final completer = Completer<String?>();
(mpjs.context["encodeImage"] as mpjs.JSFunction).call([
rawRgba.buffer.asUint8List(),
image.width,
image.height,
"image/${format.name}",
compressQuality,
(String result) {
try {
completer.complete(result.split("base64,")[1]);
} catch (e) {
completer.complete(null);
}
}
]);
return completer.future;
}