flutter - 用户共享图像时如何在图像中添加(文本或图标)?
问题描述
请帮助我,当用户想要分享时,我想在图像和文本上添加水印,我尝试了很多但无法获得正确的结果。仅找到此 示例
我的分享图片代码是::
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';
void sharePhoto(String uurl) async {
final urlImage = uurl;
final url = Uri.parse(urlImage);
final response = await http.get(url);
final bytes = response.bodyBytes;
final temp = await getTemporaryDirectory();
final path = '${temp.path}/image.jpg';
File(path).writeAsBytesSync(bytes);
await Share.shareFiles([path], text: "dfgdfgdf gdf gdf g");
}
解决方案
下面是一个示例,说明如何在共享之前叠加两个图像。如果您仍在寻找如何做,您可以以此为起点。显然,必须进行更多的健全性检查,并且仅将其视为 POC。也拿一粒盐,因为我自己一个半星期进入飞镖/颤振,并且很乐意接受此代码的建议/评论/建议。
这是结果的快速 8 秒视频。
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:share_plus/share_plus.dart';
import 'package:http/http.dart' as http;
import 'package:image/image.dart' as img;
import 'package:path_provider/path_provider.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var imageBytes;
Future<Uint8List> _getImage() async {
final response = await http.get(Uri.parse(
'https://upload.wikimedia.org/wikipedia/commons/d/dd/Stack_Overflow_Home.png'));
imageBytes = response.bodyBytes;
return response.bodyBytes;
}
void _share(Uint8List originallImage) async {
final image = img.decodePng(originallImage)!;
// get second image
final responseWaterMark = await http
.get(Uri.parse('http://www.dspsl.com/images/700_confidential.png'));
final waterMark = img.decodeImage(responseWaterMark.bodyBytes)!;
// resize watermark if needed (like in my case)
final resizedWaterMark =
img.copyResize(waterMark, height: image.height, width: image.width);
// you may want to calculate the size of the resulting image
// based on other parameters
final mergedImage = img.Image(image.width, image.height);
// copy image and watermark into the resulting image
img.copyInto(mergedImage, image);
img.copyInto(mergedImage, resizedWaterMark);
// prep data in the temp folder
final mergedImageBytes = img.encodePng(mergedImage);
final directory = await getTemporaryDirectory();
final path = directory.path;
final imagePath = File('$path/image.png');
imagePath.writeAsBytesSync(mergedImageBytes);
// share image
await Share.shareFiles([imagePath.path]);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FutureBuilder(
future: _getImage(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Column(
children: [
Image.memory(snapshot.data as Uint8List),
TextButton(
onPressed: () {
_share(imageBytes);
},
child: const Text(
'Share Image',
style: TextStyle(fontSize: 40),
),
)
],
);
} else {
return const Center(child: CircularProgressIndicator());
}
}),
],
))));
}
}
推荐阅读
- flutter - Fluter,隐藏虚拟键盘时文本字段值变空
- sql - 内连接 2 个值(日期时间和 id)同一张表
- oracle - 如何以 HTML 格式保存包含数据的报告?(OBIEE 12c)
- javascript - 在反应应用程序中运行 npm run dev 命令时出错
- java - 使用 Java 8 构建的 GraalVM 本机映像失败,并出现错误 com.oracle.svm.core.hub.DynamicHub.getGenericInfo() 方法在此平台不可用
- javascript - 如何使用 npm(pact 文件)一起运行多个 .js 测试文件?
- sql - 将星期几名称转换为星期几编号 Vertica
- python - 为什么我的图像加载到数据集中都是白色的?
- google-apps-script - 通过 Google Apps 脚本获取每张幻灯片的 lh3.googleusercontent.com 链接
- c - 测试 FPE 和陷阱 FPE 有什么区别?