【发布时间】:2022-11-05 18:28:25
【问题描述】:
我正在使用flutter blue 和flutter_esc_pos_utils
我可以打印文字但不是图像或二维码。我想打印我已经生成的图像(这是一个二维码)。
即使在他们的文档中提供了示例,我也无法打印 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 获得的 BluePrint 类的实现
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;
}
}
这也不行不工作是指它根本不打印任何东西
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);
}
我也试过this它打印的是图像的字符串值而不是图像本身,无论如何我可以用这种方法打印图像吗?
还有一件事我想提到的是,当我尝试不同的事情时,我得到了奇怪的问号 (??????),例如 this 我从 here 得到这张图片
两天前我得到了那个输出我现在没有我的代码我必须检查我的本地历史以找出那个代码,因为我不记得我尝试了什么。
我究竟做错了什么?
编辑 - 我正在尝试一些事情,我注意到当我传递简单的文本时,我在这一行中有数据,例如 [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;
}
但是当我通过图像时,我得到了 0,0,0,0.... 也许这可能是个问题?
【问题讨论】:
标签: flutter image qr-code thermal-printer escpos