美文网首页Flutter
Flutter 生成二维码、图片保存、图片分享(空安全版本nul

Flutter 生成二维码、图片保存、图片分享(空安全版本nul

作者: Yelena_CYL | 来源:发表于2021-07-20 10:05 被阅读0次

    使用到的库(当前版本):

    //生成二维码的库,可增加logo
    
    qr_flutter: ^4.0.0 
    
    //保存图片用到的库
    
    permission_handler: ^8.1.3
    
    image_gallery_saver: ^1.6.9
    
    path_provider: ^1.6.24
    
    //分享库
    
    share: ^2.0.4
    

    引用到文件:

    import 'package:flutter/material.dart';
    
    import 'package:flutter/rendering.dart';
    
    import 'package:flutter/services.dart';
    
    import 'package:image_gallery_saver/image_gallery_saver.dart';
    
    import 'package:path_provider/path_provider.dart';
    
    import 'package:permission_handler/permission_handler.dart';
    
    import 'package:qr_flutter/qr_flutter.dart';
    
    import 'package:share/share.dart';
    
    import 'dart:ui' as ui;
    
    

    生成图片文件方法

    Future<File> _shareUiImage(ui.Image uiImage) async {
        ByteData? finalByteData = await uiImage.toByteData(format: ui.ImageByteFormat.png);
        Uint8List finalPngBytes = finalByteData!.buffer.asUint8List();
        final document = await getApplicationDocumentsDirectory();
        final dir = Directory(document.path +'/Yelena_QR.png');
        final imageFile = File(dir.path);
        await imageFile.writeAsBytes(finalPngBytes);
        return imageFile;
      }
    

    生成图片API

    GlobalKey _globalKey = GlobalKey();
    
    RepaintBoundary(
    
    key: globalKey,
    
            child: Container(需要截图的部分)
    
    )
    

    生成图片

    GlobalKey _globalKey = GlobalKey();
    //注意⚠️这里有变更 增加强转,旧版API无法使用
    RenderRepaintBoundary? boundary = _globalKey.currentContext?.findRenderObject()! as RenderRepaintBoundary;
    
     ui.Image image = await boundary.toImage();
    

    //*******************生成二维码图片*********************

    qr_flutter: ^4.0.0 //此版本调用的API优化的比较简单了,

    不带logo

    QrImage(
      data: 'This is a simple QR code',
      version: QrVersions.auto,
      size: 320,
      gapless: false,
    )
    

    带logo

    QrImage(
      data: 'This QR code has an embedded image as well',
      version: QrVersions.auto,
      size: 320,
      gapless: false,
      embeddedImage: AssetImage('assets/images/my_embedded_image.png'),
      embeddedImageStyle: QrEmbeddedImageStyle(
        size: Size(80, 80),
      ),
    )
    

    保存图片方法

     //检查是否有存储权限
                              var status = await Permission.storage.status;
                              if (!status.isGranted) {
                                status = await Permission.storage.request();
                                print(status);
                                return;
                              }
                                
                                RenderRepaintBoundary? boundary = _globalKey.currentContext?.findRenderObject()! as RenderRepaintBoundary;
                                ui.Image image = await boundary.toImage();
                                ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
                                final result = await ImageGallerySaver.saveImage(byteData!.buffer.asUint8List(),quality:100,name: 'boss_Image'+DateTime.now().toString() );
                                if (result['isSuccess'].toString()=='true') {
                                  print('保存成功');
                                }else{
                                  print('保存失败');
                                }
    

    //*******************分享图片方法*********************

    RenderRepaintBoundary? boundary = _globalKey.currentContext?.findRenderObject()! as RenderRepaintBoundary;
                           ui.Image image = await boundary.toImage();
                           File imageFile = await _shareUiImage(image);
                           Share.shareFiles([imageFile.path]);
    

    //注意一、RenderRepaintBoundary的变化
    //注意二、import 'dart:ui' show lerpDouble;
    Error: Not found: 'dart:ui'
    检查文件名是否重复

    ****日常备注小本本***不知是否有帮助到您******不管有无均希望能点个赞*********谢谢**************

    相关文章

      网友评论

        本文标题:Flutter 生成二维码、图片保存、图片分享(空安全版本nul

        本文链接:https://www.haomeiwen.com/subject/ybadmltx.html