android - 在 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(相机)源中获取图像。
解决方案
您可以将 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);
}
推荐阅读
- javascript - Javascript 不适用于 ie 浏览器和 ff
- metal - 想要放弃 MTKView.currentRenderPassDescriptor
- java - 阿帕奇卡夫卡:寻找和分配。从头开始可靠读取
- c# - 使用SharpAVI录屏,为什么我提高FPS后播放的.avi文件好像加速了?
- javascript - 排序 ng-table(空值和自定义排序)
- javascript - FlowJS - 无法识别可选字段何时有值
- unix - 在字符串中查找负数
- python - 什么相当于 Python 3.x 中的“bwlabeln(具有 18 和 26 连接的邻域)”?
- python - 从另一个 Python 脚本导入函数时,如何确保导入的模块正常工作?
- python - 如何操作 Google Ads API 的 Enum 类的对象 - python