首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将文本和图像文件合并为一个图像文件?

如何将文本和图像文件合并为一个图像文件?
EN

Stack Overflow用户
提问于 2022-03-16 22:09:10
回答 1查看 535关注 0票数 1

下面是函数中的代码片段,该函数将生成的QR代码(使用qr_flutter包)上载到firebase存储区;然后获取防火墙存储url以保存在上载到firebase防火墙的自定义模型中(未显示)。

这很好,但是我想上传一个由上面的标题文本和下面的地址文本所限制的QR代码组成的文件。(本质上是带有子标题、qrFile、address的列)。

我的问题是:如何将文本和qrFile合并成一个图像文件,我可以将其上传到防火墙存储中?

代码语言:javascript
运行
复制
String qrString = 'qr_data_here';
        final qrValidationResult = QrValidator.validate(
          data: qrString,
          version: QrVersions.auto,
          errorCorrectionLevel: QrErrorCorrectLevel.L,
        );
        if (qrValidationResult.status == QrValidationStatus.valid) {
          final qrCode = qrValidationResult.qrCode;
          const String title = 'title_name_here';
          final String address = 'address_here';
          final painter = QrPainter.withQr(
            qr: qrCode!,
            color: const Color(0xFF000000),
            gapless: true,
            embeddedImageStyle: null,
            embeddedImage: null,
          );
          Directory tempDir = await getTemporaryDirectory();
          String tempPath = tempDir.path;
          final ts = DateTime.now().millisecondsSinceEpoch.toString();
          String path = '$tempPath/$ts.png';
                                                // ui is from import 'dart:ui' as ui;
          final picData =
              await painter.toImageData(2048, format: ui.ImageByteFormat.png);
          // writeToFile is seen in code snippet below
          await writeToFile(
            picData!,
            path,
          );
        } else {
          genericErrorDialog(context);
        }
        // qrStorage is a reference to a folder in firebase storage
        await qrStorage.child('name_here').putFile(qrFile);
        var url =
            await qrStorage.child('name_here').getDownloadURL();
代码语言:javascript
运行
复制
  late File qrFile;

  Future<void> writeToFile(ByteData data, String path) async {
    final buffer = data.buffer;
    qrFile = await File(path).writeAsBytes(
        buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-17 20:01:51

一种解决方案是使用屏幕截图包(https://pub.dev/packages/screenshot)。这个包有一个函数,可以将小部件保存为图像(而不显示在屏幕上),如下所示。

代码语言:javascript
运行
复制
ScreenshotController screenshotController = ScreenshotController();


await screenshotController
              .captureFromWidget(CustomWidget())
              .then((capturedImage) async {
            await do_something_with_capturedImage_here();
          });

由于它与我的问题具体相关;下面是生成qr代码的代码,将它与文本放在一个小部件(需要更多的格式)中,然后将小部件保存为图像文件并上传到firebase。

代码语言:javascript
运行
复制
String qrString = 'qr_data_here';
        final qrValidationResult = QrValidator.validate(
          data: qrString,
          version: QrVersions.auto,
          errorCorrectionLevel: QrErrorCorrectLevel.L,
        );
        if (qrValidationResult.status == QrValidationStatus.valid) {
          final qrCode = qrValidationResult.qrCode;
          const String title = 'title_name_here';
          final String address = 'address_here';
          final painter = QrPainter.withQr(
            qr: qrCode!,
            color: const Color(0xFF000000),
            gapless: true,
            embeddedImageStyle: null,
            embeddedImage: null,
          );
          Directory tempDir = await getTemporaryDirectory();
          String tempPath = tempDir.path;
          final ts = DateTime.now().millisecondsSinceEpoch.toString();
          String path = '$tempPath/$ts.png';
                                                // ui is from import 'dart:ui' as ui;
          final picData =
              await painter.toImageData(2048, format: ui.ImageByteFormat.png);
          // writeToFile is seen in code snippet below
          await writeToFile(
            picData!,
            path,
          );
          await screenshotController
              .captureFromWidget(Column(
            children: [
              Text(title),
              Image.file(qrFile),
              Text(address),
            ],
          ))
              .then((capturedImage) async {
            await widgetToImageFile(capturedImage);
          });
        } else {
          genericErrorDialog(context);
        }
        // qrStorage is a reference to a folder in firebase storage
        await qrStorage.child('name_here').putFile(fullQrFile);
        var url =
            await qrStorage.child('name_here').getDownloadURL();
代码语言:javascript
运行
复制
ScreenshotController screenshotController = ScreenshotController();
  late File qrFile;
  late File fullQrFile;


  Future<void> writeToFile(ByteData data, String path) async {
    final buffer = data.buffer;
    qrFile = await File(path).writeAsBytes(
        buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
  }

  Future<void> widgetToImageFile(
    Uint8List capturedImage,
  ) async {
    Directory newTempDir = await getTemporaryDirectory();
    String newTempPath = newTempDir.path;
    final newTs = DateTime.now().millisecondsSinceEpoch.toString();
    String path = '$newTempPath/$newTs.png';
    fullQrFile = await File(path).writeAsBytes(capturedImage);
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71504797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档