Flutter实现水印打卡照片

公司要求拍照以后返回加一层水印在照片上,网上找了几个组件本来都实现了结果发现只有英文不给写中文气得要死

整体来说就是这个方法,思路就是把照片当做画布然后再画一个水印出来设置透明背景把两张图拼在一起 copyImageToDocumentDirectory方法是为了解决ios图片地址的问题 有兴趣的小伙伴可以参考一下

复制代码
  Future<String> createAndSaveImageWithMerge(String imagePath) async {
    final image = await loadImage(imagePath);
    final imageWidth = image.width;
    final imageHeight = image.height;

    final recorder = ui.PictureRecorder();
    final canvas = Canvas(
        recorder,
        Rect.fromPoints(Offset(0, 0),
            Offset(imageWidth.toDouble(), imageHeight.toDouble())));

    final paint = Paint();
    canvas.drawImage(image, Offset.zero, paint);

    final textSpan = TextSpan(
      text:
          '${widget.watermarkModel?.siteName ?? "--"}\n${widget.watermarkModel?.watermarkTime}\n${widget.watermarkModel?.addressName}\n',
      style: const TextStyle(color: Colors.white, fontSize: 50),
    );
    final textPainter = TextPainter(
      text: textSpan,
      textAlign: TextAlign.left,
      textDirection: TextDirection.ltr,
    );
    textPainter.layout(minWidth: 0, maxWidth: imageWidth.toDouble());
    final offset = Offset(10, imageHeight.toDouble() - textPainter.height - 10);
    textPainter.paint(canvas, offset);
    LoadingUtil.hideLoading();
    final picture = recorder.endRecording();
    final img = await picture.toImage(imageWidth, imageHeight);
    final byteData = await img.toByteData(format: ui.ImageByteFormat.png);
    final pngBytes = byteData!.buffer.asUint8List();

    final mergedImage = imgs.decodeImage(pngBytes)!;
    final jpgBytes = imgs.encodeJpg(mergedImage);
    final path = imagePath;
    final file = File(path);
    LoadingUtil.showLoading("加载中",EasyLoadingIndicatorType.fadingCircle);

    await file.writeAsBytes(jpgBytes);

    // 使用 copyImageToDocumentDirectory 方法将图片保存到本地并获取新路径
    List<String> newPaths = await copyImageToDocumentDirectory([path]);
    return newPaths.first; // 返回保存后的新路径
  }

  Future<List<String>> copyImageToDocumentDirectory(
      List<String> oldPathList) async {
    if (Platform.isAndroid) return oldPathList;
    List<String> newListPath = [];
    await Future.forEach<String>(oldPathList, (oldPath) async {
      final oldImageFile = File(oldPath);
      String imageName = oldPath.substring(oldPath.indexOf('tmp') + 3);
      String basicPath = await ImageUtil.pictureInputBasicPath();
      String newPath = basicPath + imageName;
      final newDirectory = Directory(basicPath);
      newDirectory.createSync(recursive: true);
      await oldImageFile.copy(newPath);
      newListPath.add(imageName);
    });

    return newListPath;
  }
相关推荐
玖夜Kty7 小时前
国内环境修改 flutter.bat 来设置 flutter 的网络环境
flutter
LinXunFeng8 小时前
Flutter - GetX Helper 助你规范应用 tag
flutter·github·visual studio code
阅文作家助手开发团队_山神16 小时前
第五章:Flutter Quill渲染原理深度剖析:Delta到RichText的华丽转身
flutter
未来猫咪花17 小时前
# Flutter状态管理对比:view_model vs Riverpod
flutter·ios·android studio
阅文作家助手开发团队_山神1 天前
第四章(下) Delta 到 HTML 转换:块级与行内样式渲染深度解析
flutter
MaoJiu1 天前
Flutter造轮子系列:flutter_permission_kit
flutter·swiftui
阅文作家助手开发团队_山神2 天前
第四章(下):Delta 到 HTML 转换的核心方法解析
flutter
xiaoyan20152 天前
flutter3.32+deepseek+dio+markdown搭建windows版流式输出AI模板
flutter·openai·deepseek
阅文作家助手开发团队_山神2 天前
第四章(上):HTML 到 Delta 转换的核心方法解析
flutter
stringwu2 天前
Flutter高效开发利器:Riverpod框架简介及实践指南
flutter