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;
  }
相关推荐
克喵的水银蛇6 小时前
Flutter 动画实战:基础动画 + Hero 动画 + 自定义动画
flutter
松☆8 小时前
Flutter + OpenHarmony 实战:构建离线优先的跨设备笔记应用
笔记·flutter
_大学牲10 小时前
Flutter 勇闯2D像素游戏之路(一):一个 Hero 的诞生
flutter·游戏·游戏开发
kirk_wang10 小时前
Flutter插件在鸿蒙端的开发与部署:跨生态桥梁的架构与实现
flutter·移动开发·跨平台·arkts·鸿蒙
勇气要爆发13 小时前
【第五阶段—高级特性和框架】复杂动画案例分析初体验
flutter
勤劳打代码14 小时前
追本溯源 —— SetState 刷新做了什么
flutter·面试·性能优化
松☆16 小时前
OpenHarmony 后台任务与 Flutter 生命周期协调:构建稳定可靠的混合应用
flutter
松☆16 小时前
Flutter 与 OpenHarmony 深度集成:自定义 MethodChannel 插件开发全指南
flutter·wpf
克喵的水银蛇16 小时前
Flutter 布局实战:掌握 Row/Column/Flex 弹性布局
前端·javascript·flutter