英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论