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;
  }
相关推荐
m0_7482478014 小时前
Flutter Intl包使用指南:实现国际化和本地化
前端·javascript·flutter
迷雾漫步者15 小时前
Flutter组件————PageView
flutter·跨平台·dart
迷雾漫步者1 天前
Flutter组件————FloatingActionButton
前端·flutter·dart
coder_pig1 天前
📝小记:Ubuntu 部署 Jenkins 打包 Flutter APK
flutter·ubuntu·jenkins
捡芝麻丢西瓜1 天前
flutter自学笔记5- dart 编码规范
flutter·dart
恋猫de小郭1 天前
什么?Flutter 可能会被 SwiftUI/ArkUI 化?全新的 Flutter Roadmap
flutter·ios·swiftui
sunly_2 天前
Flutter:导航,tab切换,顶部固定,列表分页滚动
开发语言·javascript·flutter
敲代码的小强2 天前
Flutter项目兼容鸿蒙Next系统
flutter·华为·harmonyos
Zh-jie3 天前
flutter 快速实现侧边栏
前端·javascript·flutter
truemi.733 天前
flutter --no-color pub get 超时解决方法
android·flutter