如何解决如何从 Canvas 打印到热敏打印机到图像?
我正在使用带有 Flutter Android 的 blue_thermal_printer 尝试从 Canvas 记录创建图像,但图像打印为实心块而不是图像:
这个类负责创建图像的字节数据:
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:Flutter/material.dart';
class LabelPainter {
Future<ByteData> getimageByteData() async {
int _width = 60;
int _height = 60;
ui.PictureRecorder recorder = new ui.PictureRecorder();
Paint _paint = Paint()
..style = PaintingStyle.stroke
..strokeWidth = 4.0;
Canvas c = new Canvas(recorder);
c.drawRRect(RRect.fromLTRBAndCorners(20,30,40,50),_paint);
_paint.color = Colors.red;
c.drawRect(Rect.fromLTWH(10,10,10),_paint);
_paint.color = Colors.blue;
c.drawRect(
Rect.fromCenter(center: Offset(50,height: 50,width: 50),_paint);
_paint.color = Colors.black;
c.drawRect(
Rect.fromPoints(
Offset(0,0),Offset(_width.todouble(),_height.todouble())),_paint);
// c.drawPaint(Paint()); // etc
ui.Picture p = recorder.endRecording();
ui.Image _uiImg = await p.toImage(
_width,_height); //.toByteData(format: ImageByteFormat.png);
ByteData _byteData =
await _uiImg.toByteData(format: ui.ImageByteFormat.png);
return _byteData;
}
}
这是获取 ByteData 然后将图像保存到文件目录的状态小部件的一部分:
class _PrinterState extends State<Printer> {
String pathImage;
LabelPainter _labelPainter = new LabelPainter();
@override
void initState() {
super.initState();
initSavetoPath();
}
initSavetoPath() async {
//read and write
//image max 300px X 300px
final filename = 'yourlogo.png';
// var bytes = await rootBundle.load("images/logo.png");
ByteData bytes = await _labelPainter.getimageByteData();
String dir = (await getApplicationDocumentsDirectory()).path;
writetoFile(bytes,'$dir/$filename');
setState(() {
pathImage = '$dir/$filename';
});
}
@override
Widget build(BuildContext context) {
return Container();
}
//write to app path
Future<void> writetoFile(ByteData data,String path) {
final buffer = data.buffer;
return new File(path).writeAsBytes(
buffer.asUint8List(data.offsetInBytes,data.lengthInBytes));
}
}
void _tesPrint() async {
//SIZE
// 0- normal size text
// 1- only bold text
// 2- bold with medium text
// 3- bold with large text
//ALIGN
// 0- ESC_ALIGN_LEFT
// 1- ESC_ALIGN_CENTER
// 2- ESC_ALIGN_RIGHT
bluetooth.isConnected.then((isConnected) async {
if (isConnected) {;
// bluetooth.printimageBytes(await _labelPainter.getimageBytesUint());
bluetooth.printimage(pathImage);
// bluetooth.printNewLine();
// bluetooth.printCustom("Terimakasih",2,1);
// bluetooth.printNewLine();
// bluetooth.printQRcode("Insert Your Own Text to Generate",50,0);
// bluetooth.paperCut();
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。