downloadImage method

Future<bool> downloadImage(
  1. Image image, {
  2. int x = 0,
  3. int y = 0,
  4. double scale = 1,
  5. int printerDpi = 203,
})

Converts an image to gray scanle and sends it to the printer?

Implementation

//Future<Image> downloadImage(Image image,
Future<bool> downloadImage(Image image,
    {int x = 0, int y = 0, double scale = 1, int printerDpi = 203}) async {
  double ratio = image.height / image.width ;
  double desiredImageWidth =
      image.width * scale; //image.height * printerDpi / 72 * scale;
  double desiredImageHeight =
      image.height * scale; //image.height * printerDpi / 72 * scale;

  // Ensure the that the sizes are a multiple of 8 because we track the widths in bytes.
  if (desiredImageWidth % 8 != 0) {
    desiredImageWidth = (desiredImageWidth ~/ 8 + 1) * 8;
    desiredImageHeight = desiredImageWidth * ratio;
  }

  print(
      "Desired Width: ${desiredImageWidth} - Desired Height ${desiredImageHeight}");

  //Draw the image to gray scale
  PictureRecorder recorder = PictureRecorder();
  Canvas canvas = Canvas(recorder);
  Paint paint = new Paint()
    ..colorFilter = ColorFilter.mode(Color(0xFF000000), BlendMode.color);
  Rect scrRect =
      Rect.fromLTWH(0, 0, image.width.toDouble(), image.height.toDouble());
  Rect destRect = Rect.fromLTWH(0, 0, desiredImageWidth, desiredImageHeight);
  canvas.drawImageRect(image, scrRect, destRect, paint);

  //Save canvas to new image
  var grayPicture = await recorder
      .endRecording()
      .toImage((desiredImageWidth).toInt(), (desiredImageHeight).toInt());

  // Get new image bytes.
  ByteData? imageByteData =
      await grayPicture.toByteData(format: ImageByteFormat.rawRgba);
  print("Rgb Size ${imageByteData?.lengthInBytes}");

  imageByteData =
      await grayPicture.toByteData(format: ImageByteFormat.rawUnmodified);
  print("Raw Unmodified Size ${imageByteData?.lengthInBytes}");

  if (imageByteData == null) {
    return false;
    //return image;
  }

  int pictureWidthBytes = (grayPicture.width + 7) ~/ 8;
  int pictureHeight = grayPicture.height;
  Uint8List outImageBytes = Uint8List(pictureWidthBytes * pictureHeight);

  for (int y = 0; y < pictureHeight * pictureWidthBytes; ++y) {
    outImageBytes[y] = -1.toUnsigned(8);
  }

  Uint8List inputImageBytes = imageByteData.buffer.asUint8List();
  for (int colorByte = 0;
      colorByte < inputImageBytes.length;
      colorByte += 4) {
    int colorR = _clampColor(inputImageBytes[colorByte]);
    int colorG = _clampColor(inputImageBytes[colorByte + 1]);
    int colorB = _clampColor(inputImageBytes[colorByte + 2]);
    int colorAa = _clampColor(inputImageBytes[colorByte + 3]);
    //print ("R:${colorR} - G:${colorG} - B:${colorB} ");
    int total = (colorR + colorG + colorB) ~/ 3;
    if (total == 0) {
      outImageBytes[(colorByte / 4) ~/ 8] ^=
          (128 >> (colorByte ~/ 4) % 8).toUnsigned(8);
    }
  }

  //BrotherUtils.printBytes(outImageBytes, pictureWidthBytes);

  /*
  String binLine = "";
  for (int i = 0; i < outImageBytes.length; i++) {
    if (i % pictureWidthBytes == 0) {
      print(binLine + " -- $i");
      binLine = "";
    }

    binLine =
        binLine + " " + (outImageBytes[i].toRadixString(2).padLeft(8, '0'));
  }

  print("String version: --- ");

  */

  if (Platform.isIOS) {
    // Since iOS does not have the sentCommand(byte[])
    //  exposed we instead write make the image a BMP and use the downloadBmp instead.
    // Convert image to BMP
    Uint8List bmpImageBytes = _wrapInWindowBmp(
        width: desiredImageWidth.toInt(),
        height: desiredImageHeight.toInt(),
        imageBytes: outImageBytes,
        printerDpi: printerDpi);

    //BrotherUtils.printBytes(bmpImageBytes, pictureWidthBytes);
    BrotherUtils.printBytesHex(bmpImageBytes, 16);

    //Image bmpImage = await BrotherUtils.bytesToImage(bmpImageBytes);
    // Save image to temp storage location.
    String tempBmpFilename = "temp.bmp"; //"temp_${DateTime.now().microsecondsSinceEpoch}.bmp";
    String tempBmpFilePath = await BrotherUtils.bytesToTempFile(
        bmpImageBytes, tempBmpFilename);
    // downloadBmp
    bool downloadSuccess = await downloadBmp(tempBmpFilePath);
    // The position is set on the canvas instead of the put, so we
    // place at 0,0
    bool putSuccess = await sendTbCommand(
        TbCommandPutBmp(0, 0, tempBmpFilePath));
    // Delete bmp file
    await File(tempBmpFilePath).delete();
    // Return success value.
    return putSuccess;
  }
  else {
    TbCommandSendBitmap sendBitmapCmd = TbCommandSendBitmap(
        x,
        y,
        pictureWidthBytes,
        pictureHeight,
        Uint8List(outImageBytes.buffer.asUint8List().length));
    bool result = await sendTbCommand(sendBitmapCmd);
    result = result &&
        await sendCommandBin(
            outImageBytes); //sendCommand(String.fromCharCodes(outImageBytes));
    result = result && await sendCommand("\"\r\n");


    return result;
    //return bmpImage;
    //return grayPicture;

  }
}