請幫助我,我想在用戶想分享圖片和文字時,在圖片上加水印,我試了很多,但無法得到正確的結果。我發現這個例子只是
。我的共享圖片代碼是::
import 'dart:io'/span>;
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'/span>。
File(path).writeAsBytesSync(bytes)。
await Share.shareFiles([path], text: "dfgdfgdf gdf g")。
uj5u.com熱心網友回復:
下面是一個例子,說明你如何在分享前疊加兩張圖片。如果你還在尋找如何做,你可以把這個作為一個起點。顯然,必須有更多的理智檢查和所有,把這僅僅作為一個POC。另外,我自己對飛鏢/flutter也有一個半星期的了解,我很樂意接受關于這個代碼的建議/意見/提議。
這里有一個快速的8s視頻的結果。
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 image = img.decodePng(originallImage)!
final responseWaterMark = await http
.get(Uri.parse('http://www.dspsl.com/images/700_confidential.png') )。
final waterMark = img.decodeImage(responseWaterMark.bodyBytes)!
//調整水印的大小,如果需要的話(像我的情況)。
final resizedWaterMark =
img.copyResize(waterMark, height: image.height, width: image.width)。
//你可能希望計算出結果影像的大小。
//基于其他引數。
final mergedImage = img.Image(image.width, image.height)。
//將影像和水印復制到生成的影像中。
img.copyInto(mergedImage, image)。
img.copyInto(mergedImage, resizedWaterMark)。
//預處理臨時檔案夾中的資料。
final mergedImageBytes = img.encodePng(mergedImage);
final directory = await getTemporaryDirectory()。
final path = directory.path;
final imagePath = File('$path/image.png') 。
imagePath.writeAsBytesSync(mergedImageBytes)。
//分享圖片; imagePath.writeAsBytesSync(mergedImageBytes); //分享圖片await Share.shareFiles([imagePath.path]) 。
}
@override
Widget build(BuildContext context) {
return MaterialApp(
首頁。Scaffold(
body: SafeArea(
child: 列(
mainAxisAlignment: MainAxisAlignment.center,
兒童。[
FutureBuilder(
未來。_getImage(),
構建者。(context, snapshot) {
if (snapshot.hasData) {
return Column(
兒童。[
Image.memory(snapshot.data as Uint8List)。)
TextButton(
onPressed: () {
_share(imageBytes);
},
孩子。const Text(
'Share Image'。
風格。TextStyle(fontSize: 40)。
),
)
],
);
} else {
return const Center(child: CircularProgressIndicator())。)
}
}),
],
))));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/313103.html
標籤:
