Flutter Print Qr Code Image in Flutter to Thermal Printer - flutter

I try to print out qr code to thermal printer by using flutter_bluetooth_serial.
I generate the qr code by using following guide
https://medium.com/flutter-community/building-flutter-qr-code-generator-scanner-and-sharing-app-703e73b228d3
I manage to convert the image into Uint8List and send to the printer.
Future<Uint8List> _getQrByte() async {
RenderRepaintBoundary boundary =
globalKey.currentContext.findRenderObject();
var image = await boundary.toImage();
var byteData = await image.toByteData();
return byteData.buffer.asUint8List();
}
and I call function in flutter_bluetooth_serial
await _bluetooth.writeBytes(bytes);
I expect to print a perfect qr code, but the printout is random char and very long.
In android, I manage to print out by sending byte array from bitmap class to the printer

Have you tried esc_pos_bluetooth for a Bluetooth printer (or esc_pos_printer for a WiFi/network printer)? Both packages can print QR codes

You can use this:
final profile = await CapabilityProfile.load(name: 'XP-N160I');
final generator = Generator(PaperSize.mm80, profile);
bytes += generator.qrcode(
'https://github.com/BrunoSantosCosta',
size: const QRSize(30),
);

Related

Convert memory image to base64 in flutter

trying to create pdf file using pdf package then convert it to an image and then convert the image to base64 to save it in a QR code. this should be a simple task for URL images and gallery images. But in my case, the image is generated in the flutter from pdf doc as a memory image. my code is
await for (var page in Printing.raster(await doc.save(), pages: [0, 1], dpi: 72)) {final reciptimage = page.toImage();}
tried the below code but it doesn't work
final bytes = await Io.File(reciptimage).readAsBytes();
I couldn't find any way to generate base64 from the memory image reciptimage.
Any help?
may this eample can help you.
RenderRepaintBoundary boundary = _globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
ByteData byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
String bs64 = base64Encode(pngBytes);

I cannot Print Images with blue_thermal_printer (Print Random Characters) Flutter

what's a problem with my code?
var response = await http.get(Uri.parse(
"https://thumbs.dreamstime.com/b/jpg-file-icon-logo-element-illustration-design-155443757.jpg"));
Uint8List bytesNetwork = response.bodyBytes;
Uint8List imageBytesFromNetwork = bytesNetwork.buffer.asUint8List();
await bluetooth.printImageBytes(imageBytesFromNetwork);
Try lowering the pixels of your image, I had a similar problem.

How do i open srt file on flutter?

I am trying to load subtitle to a video using the flutter video player package it works good for short files but it stopped as the file get bigger
I trayed subtitle_wrapper package but it has many bugs
Future<ClosedCaptionFile> getSubtitle(String url) async {
final data = NetworkAssetBundle(Uri(path: url));
final newdata = await data.load(url);
String fileContents = getStringFromBytes(newdata);
return captionFile = SubRipCaptionFile(fileContents);
}
this is getStringFromBytes function
getStringFromBytes(ByteData data) { final buffer = data.buffer;
var list = buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
return utf8.decode(list); }
it wasn't the size after all I tested on some srt files that have a blank space for some duration and the flutter converter do a check on every element if the length is<3 it break on get out of the loop

How to create customized design of print with print preview and finally print it using Bluetooth Thermal Printer using Flutter Dart?

I am creating one Android app using Flutter Dart and I am little bit confused about how to create our own customized design (i.e. need vertical print design) with Print Preview screen and finally print that design using Bluetooth Thermal Printer.
In other words simply I am wanting the results like bellow attached image.
So I am looking for little bit help. Thanks in Advance.
I used my own custom code after looking through other plugins githubs, because i couldn't get most of the commands to work from those plugins.
I use a LAN thermal printer but im sure this will work for Bluetooth too.
https://www.starmicronics.com/support/Mannualfolder/dot_star_cm_en.pdf
I used this website to see the commands you can send to the printer and it was just trial and error from there.
Hopefully you can get the command you need to work from there.
Use this plugin to connect the printer and try printing something with their method, to see if it is connected.
https://pub.dev/packages/esc_pos_bluetooth
The bottom method "printTicketViaLan()" in my code is sending via LAN. So to use Bluetooth i have made another method "printUsingBluetooth()" but i don't have a Bluetooth printer, so I can't test it.
Send the data using ticket.rawBytes(bytes);
Bellow are the commands i have tried and works for my printer.
PS. With the LAN method you don't need any plugins.
import 'dart:convert' show utf8;
import 'dart:io';
import 'package:flutter/material.dart';
class _MyHomePageState extends State<MyHomePage> {
void _printOrder() async {
const esc = '\x1B';
const gs = '\x1D';
const fs = '\x1C';
const reset = '$esc#'; // Initialize printer
const underline = '$esc-1';
const noUnderline = '$esc-0';
const whiteOnBlack = '${esc}4';
const noWhiteOnBlack = '${esc}5';
const doubleWide = '${esc}W1';
const doubleHeight = '${esc}h1';
const tripleWide = '${esc}W2';
const tripleHeight = '${esc}h2';
const cutPaper = '${esc}d3';
const cutPaper = '${esc}d3';
List<int> bytes = List<int>();
bytes += reset.codeUnits;
bytes += tripleWide.codeUnits;
bytes += tripleHeight.codeUnits;
bytes += utf8.encode('Some Headline \n');
bytes += doubleWide.codeUnits;
bytes += doubleHeight.codeUnits;
bytes += utf8.encode('Subtitle');
bytes += reset.codeUnits;
bytes += utf8.encode('Normal Text \n');
bytes += whiteOnBlack.codeUnits;
bytes += utf8.encode('White Text with black background\n');
bytes += reset.codeUnits;
bytes += underline.codeUnits;
bytes += noUnderline.codeUnits;
bytes += utf8.encode('Underlined Text \n');
bytes += cutPaper.codeUnits;
printUsingBluetooth(bytes);
printTicketViaLan(bytes);
}
void printUsingBluetooth(List<int> bytes) {
PrinterBluetoothManager printerManager = PrinterBluetoothManager();
printerManager.scanResults.listen((printers) async {
// store found printers and choose the right one from a list
});
printerManager.startScan(Duration(seconds: 4));
//Creating new ticket and using bytes to send.
Ticket ticket = Ticket();
ticket.rawBytes(bytes);
//Not sure if isKanji must be set to true... Try both.
ticket.rawBytes(bytes, isKanji: true);
printerManager.selectPrinter(printer);
final PosPrintResult res = await printerManager.printTicket(ticket);
print('Print result: ${res.msg}');
}
void printTicketViaLan(List<int> bytes) async {
Duration timeout = const Duration(seconds: 5);
var result = await Socket.connect('192.168.0.34', 9100, timeout: timeout)
.then((Socket socket) {
socket.add(bytes);
socket.destroy();
return 'Data sent Success';
}).catchError((dynamic e) {
return 'Error sending Data';
});
print(result);
}

How to convert a PDF file to an image with Flutter?

How can I convert a PDF file into an image with Flutter?
I want to print the image to an ESC/POS printer using esc_pos_printer. This package won't accept PDFImage, it needs to be a Flutter Image.
I see plenty of PHP plugins that do this but nothing for Flutter.
edit: There is an answer to another question here which shows some code to decode an image from "pdf64" but I can't figure out exactly what "pdf64" is.
I created a PDF from html using flutter_html_to_pdflike this:
Directory appDocDir = await getApplicationDocumentsDirectory();
var targetPath = appDocDir.path;
var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlContent(
htmlContent, targetPath, targetFileName);
generatedPdfFilePath = generatedPdfFile.path;
Now I need to know how to create a Flutter Image from that PDF or the bytecode to send raw to the printer.
You can use https://pub.dev/packages/printing:
await for (var page in Printing.raster(document)) {
final image = page.asImage();
...
}
This plugin can also convert your Html to Pdf with this:
final pdf = await Printing.convertHtml(
format: PdfPageFormat.a4,
html: '<html><body><p>Hello!</p></body></html>',
));