当前位置:   article > 正文

flutter截屏的方式生成图片水印

flutter截屏的方式生成图片水印
import 'dart:io';
import 'dart:ui' as ui;

import 'package:jade/utils/ImageWaterMarkUtil.dart';
import 'package:util/screen_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
/*
* 截屏的方式生成图片水印
* */
///图片生产水印
class ImageWaterMarkPage extends StatefulWidget {
  //任务ID
  //final Asset imagePath;
  final String imagePath;

  ImageWaterMarkPage(this.imagePath, {Key key}) : super(key: key);

  
  State<ImageWaterMarkPage> createState() => _WaterMarkPageState();
}

class _WaterMarkPageState extends State<ImageWaterMarkPage> {

  GlobalKey _globalKey = GlobalKey();

  get image_watermark => null;



  Future<void> saveSignImg() async {
    //通过globalkey将Widget保存为ui.Image
    ui.Image _image = await ImageWaterMarkUtil.imageLoader.getImageFromWidget(_globalKey);

    ///异步将这张图片保存在手机内部存储目录下
    String localImagePath =  await ImageWaterMarkUtil.imageLoader.saveImageByUIImage(_image, isEncode: false);
    ///保存完毕后关闭当前页面并将保存的图片路径返回到上一个页面
    Get.back(result: localImagePath);
  }
  
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(title: Text('水印功能'),),
      body: Column(
        children: [
          InkWell(
            child: Text('保存'),
            onTap: (){
              saveSignImg();
            },
          ),
          Container(
              alignment: Alignment.center,
              child: RepaintBoundary(
                key: _globalKey,
                child: Stack(
                  children: [
                    Image.file(
                      File(widget.imagePath),
                      width: setWidth(600),
                      fit: BoxFit.fitWidth,
                    ),
                    Positioned(
                        top: 2,
                        right: 2,
                        child: Text(
                          "这里是水印内容",
                          style: TextStyle(fontSize: 30.sp, color: Colors.white),
                        ))
                  ],
                ),
              )
          )
        ],
      ),
    );
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'dart:ui';
import 'package:crypto/crypto.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/rendering.dart';
import 'package:path_provider/path_provider.dart';

/// 图片加载工具类
class ImageWaterMarkUtil {

  //私有化构造
  ImageWaterMarkUtil._();

  //单例模式创建
  static final ImageWaterMarkUtil imageLoader = ImageWaterMarkUtil._();

  // 将一个Widget转为image.Image对象
  Future<ui.Image> getImageFromWidget(GlobalKey globalKey) async {
    // globalKey为需要图像化的widget的key
    RenderRepaintBoundary boundary =
    globalKey.currentContext?.findRenderObject() as RenderRepaintBoundary;
    // 转换为图像
    ui.Image img = await boundary.toImage();
    return img;
  }

  ///将指定的文件保存到目录空间中。
  ///[image] 这里是使用的ui包下的Image
  ///[picName] 保存到本地的文件(图片)文件名,如test_image
  ///[endFormat]保存到本地的文件(图片)文件格式,如png,
  ///[isReplace]当本地存在同名的文件(图片)时,true就是替换
  ///[isEncode]对保存的文件(图片)进行编码
  ///  最终保存到本地的文件 (图片)的名称为 picName.endFormat
  Future<String> saveImageByUIImage(ui.Image image,
      {String picName,
        String endFormat = "png",
        bool isReplace = true,
        bool isEncode = true}) async {
    ///获取本地磁盘路径
    /*
     * 在Android平台中获取的是/data/user/0/com.studyyoun.flutterbookcode/app_flutter
     * 此方法在在iOS平台获取的是Documents路径
     */
    Directory appDocDir = await getApplicationDocumentsDirectory();
    String appDocPath = appDocDir.path;

    ///拼接目录
    if (picName == null || picName.trim().length == 0) {
      ///当用户没有指定picName时,取当前的时间命名
      picName = "${DateTime.now().millisecond.toString()}.$endFormat";
    } else {
      picName = "$picName.$endFormat";
    }

    if (isEncode) {
      ///对保存的图片名字加密
      picName = md5.convert(utf8.encode(picName)).toString();
    }

    appDocPath = "$appDocPath/$picName";

    ///校验图片是否存在
    var file = File(appDocPath);
    bool exist = await file.exists();
    if (exist) {
      if (isReplace) {
        ///如果图片存在就进行删除替换
        ///如果新的图片加载失败,那么旧的图片也被删除了
        await file.delete();
      } else {
        ///如果图片存在就不进行下载
        return "";
      }
    }
    ByteData byteData = await image.toByteData(format: ImageByteFormat.png);
    Uint8List pngBytes = byteData.buffer.asUint8List();
    print("保存的图片路径 $appDocPath");

    ///将Uint8List的数据格式保存
    await File(appDocPath).writeAsBytes(pngBytes);

    return appDocPath;
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

调用

//拍照图传至WaterMarkPage返回水印图
Get.to(WaterMarkPage(file!.path))?.then((newSignImg){ });
  • 1
  • 2
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/227191
推荐阅读
相关标签
  

闽ICP备14008679号