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

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

How to render Uint8List as a PdfImage in flutter?

问题

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

  1. doc.addPage(pw.Page(
  2. build: (pw.Context context) {
  3. return pw.Center(
  4. child: pw.Image(_imageFile),
  5. ); // Center
  6. }));
英文:

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.

  1. doc.addPage(pw.Page(
  2. build: (pw.Context context) {
  3. return pw.Center(
  4. child: pw.Image(_imageFile),
  5. ); // Center
  6. }));

答案1

得分: 1

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

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

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

英文:

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

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

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:

确定