如何在Flutter中将Uint8List渲染为PdfImage?

huangapple go评论54阅读模式
英文:

How to render Uint8List as a PdfImage in flutter?

问题

我已使用这个截图包:https://pub.dev/packages/screenshot
它返回一个Uint8list,但我需要将其打印为pdfImage,使用:https://pub.dev/packages/printing。我该如何做?
以下是我的打印代码。

doc.addPage(pw.Page(
 build: (pw.Context context) {
  return pw.Center(
    child: pw.Image(_imageFile),
  ); // Center
}));
英文:

I have used this screenshot package:
https://pub.dev/packages/screenshot
It gives back a Uint8list but I need to print it as a pdfImage using: https://pub.dev/packages/printing. How would I do that?

The below is my printing code.

doc.addPage(pw.Page(
 build: (pw.Context context) {
  return pw.Center(
    child: pw.Image(_imageFile),
  ); // Center
}));

答案1

得分: 1

首先,您需要将Unit8List转换为File,以进行转换。

// 将path_provider包添加到pubspec.yaml文件
final _snapController = ScreenshotController();
_snapController.capture().then((Uint8List? imageFile) async {
    // 保存到本地临时目录
    final tempDir = await getTemporaryDirectory();
    File _file = await File('${tempDir.path}/$_userId.png').create();
    _file.writeAsBytesSync(imageFile!);
    _snapImage = _file;
    // 现在您可以打印此图像......
});

请注意,如果没有使用path_provider包,将Unit8List转换为File会变得非常复杂。

英文:

First u need to convert the Unit8List to File, to convert

// add path_provider package to pubspec.yaml
  final _snapController = ScreenshotController();
  _snapController.capture().then((Uint8List? imageFile) async {
      // saving to local temp-directory
      final tempDir = await getTemporaryDirectory();
      File _file = await File('${tempDir.path}/$_userId.png').create();
      _file.writeAsBytesSync(imageFile!);
      _snapImage = _file;
      // Now u can print this img......
   });

Note that, its very complicated to convert the Unit8List to File without path_provider package.

huangapple
  • 本文由 发表于 2023年4月7日 04:11:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定