Commit e34e6d51 authored by Andrey's avatar Andrey

printing images (beta)

parent 8a5e3cce
## [1.2.0]
* printing images (beta)
## [1.1.3] ## [1.1.3]
* printCodeTable bug fixed * printCodeTable bug fixed
* updated test print example * updated test print example
## [1.1.2] ## [1.1.2]
* Better alignment * Better alignment
## [1.1.1] ## [1.1.1]
* Fixed columns alignment bug * Fixed columns alignment bug
## [1.1.0] ## [1.1.0]
* Added code page support * Added code page support
## [1.0.2] ## [1.0.2]
* Send raw command(s) * Send raw command(s)
* Turn 90° clockwise rotation mode on/off * Turn 90° clockwise rotation mode on/off
......
...@@ -76,9 +76,20 @@ printer.printRow([ ...@@ -76,9 +76,20 @@ printer.printRow([
]); ]);
``` ```
Print image:
```dart
import 'dart:io';
import 'package:image/image.dart';
const String filename = './logo.png';
final Image image = decodeImage(File(filename).readAsBytesSync());
printer.printImage(image);
```
## TODO ## TODO
* ~~Add raw print function~~ * ~~Add raw print function~~
* Print images * ~~Print images~~
* Print barcodes * Print barcodes
* Print QR codes * Print QR codes
* ~~Turn 90° clockwise rotation mode on/off~~ * ~~Turn 90° clockwise rotation mode on/off~~
......
import 'dart:io';
import 'package:esc_pos_printer/esc_pos_printer.dart'; import 'package:esc_pos_printer/esc_pos_printer.dart';
import 'package:image/image.dart';
void main() { void main() {
// To discover existing printers in your subnet, consider using // To discover existing printers in your subnet, consider using
// ping_discover_network package (https://pub.dev/packages/ping_discover_network). // ping_discover_network package (https://pub.dev/packages/ping_discover_network).
// Note that most of ESC/POS printers by default listen on port 9100. // Note that most of ESC/POS printers by default listen on port 9100.
Printer.connect('192.168.0.123', port: 9100).then((printer) { Printer.connect('192.168.0.123', port: 9100).then((printer) {
printer.println('Regular: aA bB cC dD eE fF gG hH iI jJ kK lL mM nN oO pP qQ rR sS tT uU vV wW xX yY zZ'); printer.println(
'Regular: aA bB cC dD eE fF gG hH iI jJ kK lL mM nN oO pP qQ rR sS tT uU vV wW xX yY zZ');
printer.println('Special 1: àÀ èÈ éÉ ûÛ üÜ çÇ ôÔ', printer.println('Special 1: àÀ èÈ éÉ ûÛ üÜ çÇ ôÔ',
styles: PosStyles(codeTable: PosCodeTable.westEur)); styles: PosStyles(codeTable: PosCodeTable.westEur));
printer.println('Special 2: blåbærgrød', printer.println('Special 2: blåbærgrød',
...@@ -43,6 +46,10 @@ void main() { ...@@ -43,6 +46,10 @@ void main() {
width: PosTextSize.size2, width: PosTextSize.size2,
)); ));
const String filename = './logo.png';
final Image image = decodeImage(File(filename).readAsBytesSync());
printer.printImage(image);
printer.cut(); printer.cut();
printer.disconnect(); printer.disconnect();
}); });
......
...@@ -44,3 +44,6 @@ const cPos = '$esc\$'; // Set absolute print position [nL] [nH] ...@@ -44,3 +44,6 @@ const cPos = '$esc\$'; // Set absolute print position [nL] [nH]
// Print // Print
const cFeedN = '${esc}d'; // Print and feed n lines [N] const cFeedN = '${esc}d'; // Print and feed n lines [N]
const cReverseFeedN = '${esc}e'; // Print and reverse feed n lines [N] const cReverseFeedN = '${esc}e'; // Print and reverse feed n lines [N]
// Bit Image
const cImgPrint = '${gs}v0'; // Print raster bit image [obsolete command]
...@@ -10,6 +10,7 @@ import 'dart:convert'; ...@@ -10,6 +10,7 @@ import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:hex/hex.dart'; import 'package:hex/hex.dart';
import 'package:image/image.dart';
import 'commands.dart'; import 'commands.dart';
import 'enums.dart'; import 'enums.dart';
import 'pos_column.dart'; import 'pos_column.dart';
...@@ -281,4 +282,79 @@ class Printer { ...@@ -281,4 +282,79 @@ class Printer {
_socket.write(cCutFull); _socket.write(cCutFull);
} }
} }
/// Generate multiple bytes for a number: In lower and higher parts, or more parts as needed.
///
/// [value] Input number
/// [bytesNb] The number of bytes to output (1 - 4)
List<int> _intLowHigh(int value, int bytesNb) {
final dynamic maxInput = 256 << (bytesNb * 8) - 1;
if (bytesNb < 1 || bytesNb > 4) {
throw Exception('Can only output 1-4 bytes');
}
if (value < 0 || value > maxInput) {
throw Exception(
'Number too large. Can only output up to $maxInput in $bytesNb bytes');
}
final List<int> res = <int>[];
int buf = value;
for (int i = 0; i < bytesNb; ++i) {
res.add(buf % 256);
buf = buf ~/ 256;
}
return res;
}
List<int> _convert1bit(List<int> bytes) {
final List<int> res = [];
for (int i = 0; i < bytes.length; i += 8) {
res.add(bytes[i]);
}
return res;
}
/// Print image
///
/// [image] is an instanse of class from [Image library](https://pub.dev/packages/image)
void printImage(Image image) {
const bool highDensityHorizontal = true;
const bool highDensityVertical = true;
final int widthPx = image.width;
final int heightPx = image.height;
final int widthBytes = (widthPx + 7) ~/ 8;
const int densityByte =
(highDensityVertical ? 0 : 1) + (highDensityHorizontal ? 0 : 2);
final List<int> header = List.from(cImgPrint.codeUnits);
header.add(densityByte);
header.addAll(_intLowHigh(widthBytes, 2));
header.addAll(_intLowHigh(heightPx, 2));
final xL = header[4];
final xH = header[5];
final newWidth = (xL + xH * 256) * 8;
final Image imgResized =
copyResize(image, width: newWidth, height: image.height);
final Image imgGreyscale = grayscale(imgResized);
final Image imgInvert = invert(imgGreyscale);
final bytes = imgInvert.getBytes(format: Format.luminance);
final res = _convert1bit(bytes);
// print('img w * h (src): $widthPx * $heightPx');
// print('img w * h (new): ${imgResized.width} * ${imgResized.height}');
// print('source bytes: ${bytes.length}');
// print('= target bytes: ${(xL + xH * 256) * (header[6] + header[7] * 256)}');
// print('= to print bytes: ${res.length}');
// print(header);
sendRaw(List.from(header)..addAll(res));
}
} }
name: esc_pos_printer name: esc_pos_printer
description: The library allows to print receipts using a ESC/POS (usually thermal) network printer. It can be used in Flutter or Dart projects. In Flutter, both Android and iOS are supported. description: The library allows to print receipts using a ESC/POS (usually thermal) network printer. It can be used in Flutter or Dart projects. In Flutter, both Android and iOS are supported.
version: 1.1.3 version: 1.2.0
author: Andrey Ushakov <flutter@tablemi.com> author: Andrey Ushakov <flutter@tablemi.com>
homepage: https://github.com/andrey-ushakov/esc_pos_printer homepage: https://github.com/andrey-ushakov/esc_pos_printer
...@@ -11,6 +11,7 @@ dependencies: ...@@ -11,6 +11,7 @@ dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
hex: ^0.1.2 hex: ^0.1.2
image: ^2.1.4
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment