I am using flutter blue and flutter_esc_pos_utils
I can print text but not images or QR. I wanna print Image(which is a QR CODE) that I have already generated.
Even with provided example in their documentation I didn't able to print QR here is my code for printing QR
void printWithDevice(BluetoothDevice device) async {
final profile = await CapabilityProfile.load();
final gen = Generator(PaperSize.mm58, profile);
final printer = BluePrint();
final ByteData data = await rootBundle.load('assets/images/qr.png');
final Uint8List bytes = data.buffer.asUint8List();
final img.Image image = img.decodeImage(bytes)!;
printer.add(gen.image(image)); // does not work
printer.add(gen.qrcode('www.google.com')); // does not work
printer.add(gen.text('hello'); // works
await printer.printData(device);
}
Here is the implementation of BluePrint class which I got from here
class BluePrint {
BluePrint({this.chunkLen = 512});
final int chunkLen;
final _data = List<int>.empty(growable: true);
void add(List<int> data) {
_data.addAll(data);
}
List<List<int>> getChunks() {
final chunks = List<List<int>>.empty(growable: true);
for (var i = 0; i < _data.length; i += chunkLen) {
chunks.add(_data.sublist(i, min(i + chunkLen, _data.length)));
}
return chunks;
}
Future<void> printData(BluetoothDevice device) async {
final data = getChunks();
final characs = await _getCharacteristics(device);
for (var i = 0; i < characs.length; i++) {
if (await _tryPrint(characs[i], data)) {
break;
}
}
}
Future<bool> _tryPrint(
BluetoothCharacteristic charc,
List<List<int>> data,
) async {
for (var i = 0; i < data.length; i++) {
try {
await charc.write(data[i]);
} catch (e) {
return false;
}
}
return true;
}
Future<List<BluetoothCharacteristic>> _getCharacteristics(
BluetoothDevice device,
) async {
final services = await device.discoverServices();
final res = List<BluetoothCharacteristic>.empty(growable: true);
for (var i = 0; i < services.length; i++) {
res.addAll(services[i].characteristics);
}
return res;
}
}
It's not work also by not working I mean it doesn't print anything at all
String qrData = "google.com";
const double qrSize = 200;
try {
final uiImg = await QrPainter(
data: qrData,
version: QrVersions.auto,
gapless: false,
).toImageData(qrSize);
final dir = await getTemporaryDirectory();
final pathName = '${dir.path}/qr_tmp.png';
final qrFile = File(pathName);
final imgFile = await qrFile.writeAsBytes(uiImg.buffer.asUint8List());
final img = decodeImage(imgFile.readAsBytesSync());
generator.image(img);
} catch (e) {
print(e);
}
I have also tried this it's printed the string value of Image not image itself, is there anyway I can print image with that approach?
There is one more thing I would like to mention is while I was trying different thing I got weird print of question marks (??????) like this I got this image from here
I got that output two days ago I don't have my code now I'll have to check my local history to find out that code as I don't remember what I tried.
what Am I doing wrong?
Edit - I was trying something and I noticed when I pass simple text I have data in this line like [14,21,...]
Future<bool> _tryPrint(
BluetoothCharacteristic charc,
List<List<int>> data,
) async {
for (var i = 0; i < data.length; i++) {
try {
await charc.write(data[i]);
print('printing data in chunck ${data[i]} and size ${data.length}');
} catch (e) {
return false;
}
}
return true;
}
but when I pass the image I got 0,0,0,0.... maybe that could be a problem?
printer.add(gen.image(image)); // does not work
this line didn't work because of two reasons -
1 - My printer does not support ESC* command,
2 - I did not pay attention to the width of the image,
Solutions -
1 - It worked with the GS v 0
command
like this gen.imageRaster(image);
2 - Image width 222 worked well for me.