Commit ee7610bc authored by Andrey's avatar Andrey

added new printImage method using ESC * command

parent 7b9f912d
...@@ -315,10 +315,10 @@ class Printer { ...@@ -315,10 +315,10 @@ class Printer {
return res; return res;
} }
/// Print image /// Print image using GS v 0 (obsolete command)
/// ///
/// [image] is an instanse of class from [Image library](https://pub.dev/packages/image) /// [image] is an instanse of class from [Image library](https://pub.dev/packages/image)
void printImage(Image image) { void printImageRaster(Image image) {
const bool highDensityHorizontal = true; const bool highDensityHorizontal = true;
const bool highDensityVertical = true; const bool highDensityVertical = true;
...@@ -355,4 +355,63 @@ class Printer { ...@@ -355,4 +355,63 @@ class Printer {
sendRaw(List.from(header)..addAll(res)); sendRaw(List.from(header)..addAll(res));
} }
/// Print image using ESC *
///
/// [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;
invert(image);
final Image imageRotated = copyRotate(image, 270);
// TODO(Andrey): Mirror image (FLIP_LEFT_RIGHT)
const int lineHeight = highDensityVertical ? 3 : 1;
final List<List<int>> blobs = _toColumnFormat(imageRotated, lineHeight * 8);
print(blobs.length);
print(blobs[0].length);
final int widthPx = imageRotated.width;
const int densityByte =
(highDensityHorizontal ? 1 : 0) + (highDensityVertical ? 32 : 0);
// header = ESC + b"*" + six.int2byte(density_byte) + _int_low_high(width_pixels, 2)
final List<int> header = List.from(cBitImg.codeUnits);
header.add(densityByte);
header.addAll(_intLowHigh(widthPx, 2));
// Adjust line spacing (for 16-unit line feeds): ESC 3 0x10 (HEX: 0x1b 0x33 0x10)
sendRaw([27, 51, 16]);
for (int i = 0; i < blobs.length; ++i) {
sendRaw(List.from(header)..addAll(blobs[i])..addAll('\n'.codeUnits));
}
// Reset line spacing: ESC 2 (HEX: 0x1b 0x32)
sendRaw([27, 50]);
}
/// Extract slices of an image as equal-sized blobs of column-format data.
///
/// [image] Image to extract from
/// [lineHeight] Printed line height in dots
List<List<int>> _toColumnFormat(Image image, int lineHeight) {
final int widthPx = image.width;
final int heightPx = image.height;
int left = 0;
List<List<int>> blobs = [];
int i = 0;
// TODO(Andrey): We lose a part of image here -> use while(left < widthPx)
while ((left - widthPx).abs() >= lineHeight) {
final Image slice = copyCrop(image, left, 0, lineHeight, heightPx);
// File('slice_$i.png')..writeAsBytesSync(encodePng(slice));
final Uint8List bytes = slice.getBytes(format: Format.luminance);
blobs.add(_convert1bit(bytes));
i++;
left += lineHeight;
}
return blobs;
}
} }
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