qrCode static method

List<int> qrCode(
  1. String data,
  2. int size,
  3. int errorCorrection
)

Implementation

static List<int> qrCode(String data, int size, int errorCorrection) {
  List<int> commands = [];

  // Modelo de QR
  commands.addAll([0x1D, 0x28, 0x6B, 0x04, 0x00, 0x31, 0x41, 0x32, 0x00]);

  // Tamaño del módulo
  commands
      .addAll([0x1D, 0x28, 0x6B, 0x03, 0x00, 0x31, 0x43, size.clamp(1, 16)]);

  // Nivel de corrección de errores
  commands.addAll([
    0x1D,
    0x28,
    0x6B,
    0x03,
    0x00,
    0x31,
    0x45,
    errorCorrection.clamp(0, 3)
  ]);

  // Datos
  List<int> dataBytes = utf8.encode(data);
  int dataLength = dataBytes.length + 3;
  commands.addAll([
    0x1D,
    0x28,
    0x6B,
    dataLength & 0xFF,
    (dataLength >> 8) & 0xFF,
    0x31,
    0x50,
    0x30
  ]);
  commands.addAll(dataBytes);

  // Imprimir QR
  commands.addAll([0x1D, 0x28, 0x6B, 0x03, 0x00, 0x31, 0x51, 0x30]);

  return commands;
}