image method
Print an image using (ESC *) command
image is an instance of class from Image library
Implementation
List<int> image(Image imgSrc,
{PosAlign align = PosAlign.center, bool isDoubleDensity = true}) {
List<int> bytes = [];
// Image alignment
bytes += setStyles(const PosStyles().copyWith(align: align));
Image image;
if (!isDoubleDensity) {
int size = 558 ~/ 2;
if (_paperSize == PaperSize.mm58) {
size = 375 ~/ 2;
} else if (_paperSize == PaperSize.mm72) {
size = 503 ~/ 2;
}
image =
copyResize(imgSrc, width: size, interpolation: Interpolation.linear);
} else {
image = Image.from(imgSrc); // make a copy
}
bool highDensityHorizontal = isDoubleDensity;
bool highDensityVertical = isDoubleDensity;
invert(image);
flipHorizontal(image);
final Image imageRotated = copyRotate(image, angle: 270);
int lineHeight = highDensityVertical ? 3 : 1;
final List<List<int>> blobs = _toColumnFormat(imageRotated, lineHeight * 8);
// Compress according to line density
// Line height contains 8 or 24 pixels of src image
// Each blobs[i] contains greyscale bytes [0-255]
// const int pxPerLine = 24 ~/ lineHeight;
for (int blobInd = 0; blobInd < blobs.length; blobInd++) {
blobs[blobInd] = _packBitsIntoBytes(blobs[blobInd]);
}
final int heightPx = imageRotated.height;
int densityByte =
(highDensityHorizontal ? 1 : 0) + (highDensityVertical ? 32 : 0);
final List<int> header = List.from(cBitImg.codeUnits);
header.add(densityByte);
header.addAll(_intLowHigh(heightPx, 2));
// Adjust line spacing (for 16-unit line feeds): ESC 3 0x10 (HEX: 0x1b 0x33 0x10)
bytes += [27, 51, 0];
for (int i = 0; i < blobs.length; ++i) {
bytes += List.from(header)
..addAll(blobs[i])
..addAll('\n'.codeUnits);
}
// Reset line spacing: ESC 2 (HEX: 0x1b 0x32)
bytes += [27, 50];
return bytes;
}