【问题标题】:How to add (text or icon) to image when the users share the image?用户共享图像时如何在图像中添加(文本或图标)?
【发布时间】:2021-09-15 11:51:12
【问题描述】:

请帮助我,当用户想要分享时,我想在图像和文本上添加水印,我尝试了很多但无法获得正确的结果。仅找到此example

我的分享图片代码是::

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");
}

【问题讨论】:

    标签: flutter dart flutter-test


    【解决方案1】:

    以下示例说明了如何在共享两张图片之前对其进行叠加。如果您仍在寻找如何做,您可以以此为起点。显然,必须进行更多的健全性检查,并且仅将其视为 POC。也请稍加注意,因为我自己已经有一个半星期的时间来进行飞镖/颤振,并且很乐意对此代码提出建议/cmets/suggestions。

    这是结果的quick 8s video

    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());
                  }
                }),
          ],
        ))));
      }
    }
    

    【讨论】:

    • 非常感谢。我想问你分享后图像会在记忆中吗?分享后必须从手机中删除
    • 除了 responseWaterMark 。我想在照片的一角添加 Text("")
    • 我找不到 share_plus 插件的完成处理程序。不过,我确实遇到过bug
    • 由于我们共享的是图片,您必须先将您的文本转换为图片,然后按照与上述代码相同的流程进行操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    相关资源
    最近更新 更多