如何从 Canvas 打印到热敏打印机到图像?

问题描述

我正在使用带有 Flutter Android 的 blue_thermal_printer 尝试从 Canvas 记录创建图像,但图像打印为实心块而不是图像:

enter image description here

这个类负责创建图像的字节数据:

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 (将#修改为@)