image method

List<int> image(
  1. Image imgSrc, {
  2. PosAlign align = PosAlign.center,
})

Print an image using (ESC *) command

image is an instanse of class from Image library

Implementation

List<int> image(Image imgSrc, {PosAlign align = PosAlign.center}) {
  List<int> bytes = [];
  // Image alignment
  bytes += setStyles(PosStyles().copyWith(align: align));

  final Image image = Image.from(imgSrc); // make a copy
  //const bool highDensityHorizontal = true;
  //const bool highDensityVertical = true;

  invert(image);
  flip(image, Flip.horizontal);
  final Image imageRotated = copyRotate(image, 270);

  //const int lineHeight = highDensityVertical ? 3 : 1;
  const int lineHeight = 3;
  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;
  //const int densityByte = (highDensityHorizontal ? 1 : 0) + (highDensityVertical ? 32 : 0);
  const int densityByte = 1 + 32;

  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, 16];
  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;
}