Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
854 views
in Technique[技术] by (71.8m points)

flutter - How to print to a thermal printer from Canvas to image?

I'm using the blue_thermal_printer with Flutter Android to try and create an image from a Canvas recording but the image prints as a solid block instead of an image:

enter image description here

This class is responsible for creating the bytedata of the image:

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, 10), _paint);
    _paint.color = Colors.blue;
    c.drawRect(
        Rect.fromCenter(center: Offset(50, 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;
  }
}

This is part of the status widget that gets the ByteData then saves the image to the file directory:

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));
  }
}

This is the method I call when I want to print the image:

  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, 50, 0);
        // bluetooth.paperCut();
      }
    });
question from:https://stackoverflow.com/questions/65626572/how-to-print-to-a-thermal-printer-from-canvas-to-image

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...