【问题标题】:Adding Watermark/Text over Image in Flutter - Dart在 Flutter 中的图像上添加水印/文本 - Dart
【发布时间】:2021-01-12 18:58:21
【问题描述】:

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

代码:

    _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(相机)源中获取图像。

【问题讨论】:

    标签: android image flutter dart package


    【解决方案1】:

    您可以将 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);
    

    }

    【讨论】:

    • 使用 RepaintBoundary 后,我遇到了这个问题 - 未处理的异常:FileSystemException:无法打开文件,路径 = '/data/user/0/com.example.app1/app_flutter/screenshot56.png' (操作系统错误:没有这样的文件或目录,errno = 2)
    猜你喜欢
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多