Flutter实现水印打卡照片

news/2024/9/23 6:27:51/

公司要求拍照以后返回加一层水印在照片上,网上找了几个组件本来都实现了结果发现只有英文不给写中文气得要死
整体来说就是这个方法,思路就是把照片当做画布然后再画一个水印出来设置透明背景把两张图拼在一起 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;}

http://www.ppmy.cn/news/1509111.html

相关文章

WPF自定义控件的应用(DynamicResource的使用方法)

1 DynamicResource的使用方法 可以在字典文件 的抬头区写入数&#xff1a; <SolidColorBrush x:Key"PrimaryBackgroundColor" Color"#FFABAdB3"/><SolidColorBrush x:Key"TextBox.MouseOver.Border" Color"#FF7EB4EA"/>&l…

MySQL笔记-基础篇(一):查询

博客主页: 南来_北往 系列专栏&#xff1a;Spring Boot实战 MySQL是一种广泛使用的关系型数据库管理系统&#xff0c;它基于结构化查询语言&#xff08;SQL&#xff09;来管理和操作数据。下面将依次探讨MySQL查询的各个方面&#xff0c;确保理解扎实&#xff0c;能够在实…

出现 AWS ECS 错误:集群中未找到容器实例处理办法?

使用部署docker容器映像&#xff0c;但未创建 EC2 实例的情况下。出现下面错误提示。 “调用 RunTask 操作时发生客户端错误 (InvalidParameterException)&#xff1a;在您的集群中未找到容器实例。” 经过以下的步骤&#xff1a; 1.将 Docker 映像从 Ubuntu 推送到我的 Amazo…

【Redis学习 | 第1篇】Redis介绍+下载+服务启动与停止

文章目录 1. Redis介绍2. Redis入门2.1 Redis简介2.2 Redis下载2.3 Redis服务启动与停止2.4 redis设置密码2.5 redis 如何支持远程连接 1. Redis介绍 Redis是一个基于内存的 key-value 结构数据库。 基于内存存储&#xff0c;读写性能高适合存储热点数据&#xff08;热点商品、…

fastapi之WebSockets

文章目录 WebSockets基本概念FastAPI 中的 WebSocket 支持WebSocket 应用示例示例 1: 简单的 WebSocket 连接解释 示例 2: 广播消息的 WebSocket 实现解释 客户端代码示例 完整示例项目结构服务器端代码 (main.py)解释 简单的前端客户端 (static/index.html)解释 测试 相关代码…

LeetCode面试150——14最长公共前缀

题目难度&#xff1a;简单 默认优化目标&#xff1a;最小化平均时间复杂度。 Python默认为Python3。 目录 1 题目描述 2 题目解析 3 算法原理及代码实现 3.1 横向扫描 3.2 纵向扫描 3.3 分治 3.4 二分查找 参考文献 1 题目描述 编写一个函数来查找字符串数组中的最长…

PCIe学习笔记(19)

TLP Prefix&#xff08;前缀&#xff09;规则 以下规则适用于任何包含TLP Prefix的TLP: •对于任何TLP, TLP第0字节的Fmt[2:0]字段值为100b表示存在TLP Prefix, Type[4]位表示TLP Prefix的类型。 ◦Type[4]位的值为0b表示存在Local TLP Prefix ◦Type[4]位的值为1b表示存在…

如何应对PCDN调度算法中的数据传输延迟问题?

针对PCDN调度算法中的数据传输延迟问题&#xff0c;可以采取以下应对策略: 1.优化网络基础设施: 提升服务器和网络基础设施的性能&#xff0c;包括增加带宽、优化路由器配置和更换高性能设备&#xff0c;以减少延迟。 2.使用CDN技术: 内容分发网络(CDN)可以将数据缓存在离用…