首页 > 解决方案 > 在 Flutter 中的图像上添加水印/文本 - Dart

问题描述

我正在尝试在图像上添加文本,然后将其保存到图库。

代码:

    _originalImage = File(pickedFile.path);
    ui.Image Img = ui.decodeImage(_originalImage.readAsBytesSync());
    ui.drawString(Img, ui.arial_24, 100, 120, 'Add Text');
    List<int> wmImage = ui.encodeJpg(Img);
    _watermarkedImage = File.fromRawPath(Uint8List.fromList(wmImage));

但这会引发错误:

FileSystemException:无法打开文件,路径 = ???

我正在从 ImagePicker(相机)源中获取图像。

标签: androidimageflutterdartpackage

解决方案


您可以将 RepaintBoundary 与全局键一起使用。这只是一个示例代码,可以让您有所了解。这是可能有用的视频链接 - https://www.youtube.com/watch?v=jljv7yNzqJw

  takeScreenshot() async {
RenderRepaintBoundary boundary =
    globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage();
final directory = (await getApplicationDocumentsDirectory()).path;
byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
print(pngBytes);
File imgFile = new File('$directory/screenshot${rng.nextInt(200)}.png');
setState(() {
  _imageFile = imgFile;
});
_savefile(_imageFile);
//saveFileLocal();
imgFile.writeAsBytes(pngBytes);

}


推荐阅读