title: How to Print QR code in thermal printer in android?
body: <hr>
  
  <p>In android, i am trying to develop an app that needs to print QR code using thermal printer (NGX). I think , its not being decoded properly, and i am unable to fix this problem.</p>
  
  <p>*** <a href="https://i.stack.imgur.com/auI9m.jpg" rel="nofollow noreferrer">Details of Thermal printer</a></p>
  
  <p><a href="https://i.stack.imgur.com/md4xG.jpg" rel="nofollow noreferrer">And i am getting output just like this.</a></p>
  
  <blockquote>
    <p>QR printing method</p>
  </blockquote>
  
  <pre><code>private void printQRCode() {
          int QRCODE_IMAGE_HEIGHT = 255;
          int QRCODE_IMAGE_WIDTH = 255;
          String finalQR = "";
  
          QRCodeWriter qrWriter = new QRCodeWriter();
          try {
              Hashtable&lt;EncodeHintType, ErrorCorrectionLevel&gt; hints = new Hashtable&lt;EncodeHintType, ErrorCorrectionLevel&gt;();
              hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
              BitMatrix bitMatrix = qrWriter.encode(finalQR,
                      BarcodeFormat.QR_CODE,
                      QRCODE_IMAGE_WIDTH,
                      QRCODE_IMAGE_HEIGHT, hints);
  
              int bitMatrixWidth = bitMatrix.getWidth();
  
              int bitMatrixHeight = bitMatrix.getHeight();
  
              int[] pixels = new int[bitMatrixWidth * bitMatrixHeight];
  
              for (int y = 0; y &lt; bitMatrixHeight; y++) {
                  int offset = y * bitMatrixWidth;
  
                  for (int x = 0; x &lt; bitMatrixWidth; x++) {
  
                      pixels[offset + x] = bitMatrix.get(x, y) ?
                              getResources().getColor(R.color.colorPrimary) : getResources().getColor(R.color.colorPrimaryDark);
                  }
              }
              Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);
  
              bitmap.setPixels(pixels, 0, 255, 0, 0, bitMatrixWidth, bitMatrixHeight);
  
              Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.qr);
  
              Bitmap bmOverlay = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
              Canvas canvas = new Canvas(bmOverlay);
              canvas.drawBitmap(bitmap, new Matrix(), null);
              canvas.drawBitmap(bmp, 94, 94, null); //34x34
              //canvas.drawBitmap(bmp, 86, 86, null); //42x42
  
              if (bmOverlay != null) {
                  byte[] command = Utils.decodeBitmap(bmOverlay);
                  outputStream.write(ALLINEA_CT); //This command should center my QR, but it's not working
                  outputStream.write(command);
                  outputStream.write(FEED_LINE);
                  outputStream.write(FEED_LINE);
              }
          }catch (WriterException e){
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <blockquote>
    <p>bitmap decoding : this method helps to decode bitmap, </p>
  </blockquote>
  
  <pre><code>public static byte[] decodeBitmap(Bitmap bmp){
          int bmpWidth = 255;
          int bmpHeight = 255;
  
          List&lt;String&gt; list = new ArrayList&lt;&gt;(); //binaryString list
          StringBuffer sb;
  
  
          int bitLen = bmpWidth / 8;
          int zeroCount = bmpWidth % 8;
  
          String zeroStr = "";
          if (zeroCount &gt; 0) {
              bitLen = bmpWidth / 8 + 1;
              for (int i = 0; i &lt; (8 - zeroCount); i++) {
                  zeroStr = zeroStr + "0";
              }
          }
  
          for (int i = 0; i &lt; bmpHeight; i++) {
              sb = new StringBuffer();
              for (int j = 0; j &lt; bmpWidth; j++) {
                  int color = bmp.getPixel(j, i);
  
                  int r = (color &gt;&gt; 16) &amp; 0xff;
                  int g = (color &gt;&gt; 8) &amp; 0xff;
                  int b = color &amp; 0xff;
  
                  // if color close to white，bit='0', else bit='1'
                  if (r &gt; 160 &amp;&amp; g &gt; 160 &amp;&amp; b &gt; 160)
                      sb.append("0");
                  else
                      sb.append("1");
              }
              if (zeroCount &gt; 0) {
                  sb.append(zeroStr);
              }
              list.add(sb.toString());
          }
  
          List&lt;String&gt; bmpHexList = binaryListToHexStringList(list);
          String commandHexString = "1D763000";
          String widthHexString = Integer
                  .toHexString(bmpWidth % 8 == 0 ? bmpWidth / 8
                          : (bmpWidth / 8 + 1));
          if (widthHexString.length() &gt; 2) {
              Log.e("decodeBitmap error", " width is too large");
              return null;
          } else if (widthHexString.length() == 1) {
              widthHexString = "0" + widthHexString;
          }
          widthHexString = widthHexString + "00";
  
          String heightHexString = Integer.toHexString(bmpHeight);
          if (heightHexString.length() &gt; 2) {
              Log.e("decodeBitmap error", " height is too large");
              return null;
          } else if (heightHexString.length() == 1) {
              heightHexString = "0" + heightHexString;
          }
          heightHexString = heightHexString + "00";
  
          List&lt;String&gt; commandList = new ArrayList&lt;String&gt;();
          commandList.add(commandHexString+widthHexString+heightHexString);
          commandList.addAll(bmpHexList);
  
          return hexList2Byte(commandList);
      }
  </code></pre>
  
  <blockquote>
    <p>printer commands</p>
  </blockquote>
  
  <pre><code>public class PrinterCommands {
      public static final byte HT = 0x9;
      public static final byte LF = 0x0A;
      public static final byte CR = 0x0D;
      public static final byte ESC = 0x1B;
      public static final byte DLE = 0x10;
      public static final byte GS = 0x1D;
      public static final byte FS = 0x1C;
      public static final byte STX = 0x02;
      public static final byte US = 0x1F;
      public static final byte CAN = 0x18;
      public static final byte CLR = 0x0C;
      public static final byte EOT = 0x04;
  
  
      public static final byte[] INIT = {27, 64};
      public static byte[] FEED_LINE = {10};
  
      public static byte[] SELECT_FONT_A = {27, 33, 0};
  
      public static byte[] FONT_B = {0x1B, 0x4D, 0x01};
      public static byte[] ALLINEA_SX = {0x1B, 0x61, 0x00};
      public static byte[] ALLINEA_CT = {0x1B, 0x61, 0x01};
      public static byte[] ALLINEA_DX = {0x1B, 0x61, 0x02};
      public static byte[] GRASSETTO_ON = {0x1B, 0x47, 0x11};
      public static byte[] GRASSETTO_OFF = {0x1B, 0x47, 0x00};
      public static byte[] SET_6 = {0x1B, 0x52, 0x06};
      public static byte[] CODICI = {0x1B, 0x74, 0x13};
      public static byte[] EURO = {(byte) 0xD5};
      public static byte[] PALLINO = {(byte) 0x5B};
      public static byte[] FONT_3X = {0x1D, 0x21, 0x21};
      public static byte[] FONT_2X = {0x1D, 0x21, 0x11};
      public static byte[] FONT_1X = {0x1D, 0x21, 0x00};
  
      public static byte[] SET_BAR_CODE_HEIGHT = {29, 104, 100};
      public static byte[] PRINT_BAR_CODE_1 = {29, 107, 2};
      public static byte[] SEND_NULL_BYTE = {0x00};
  
      public static byte[] SELECT_PRINT_SHEET = {0x1B, 0x63, 0x30, 0x02};
      public static byte[] FEED_PAPER_AND_CUT = {0x1D, 0x56, 66, 0x00};
  
      public static byte[] SELECT_CYRILLIC_CHARACTER_CODE_TABLE = {0x1B, 0x74, 0x11};
  
      public static byte[] SELECT_BIT_IMAGE_MODE = {0x1B, 0x2F, 33, (byte) 255, 3};
      public static byte[] SET_LINE_SPACING_24 = {0x1B, 0x33, 24};
      public static byte[] SET_LINE_SPACING_30 = {0x1B, 0x33, 30};
  
      public static byte[] TRANSMIT_DLE_PRINTER_STATUS = {0x10, 0x04, 0x01};
      public static byte[] TRANSMIT_DLE_OFFLINE_PRINTER_STATUS = {0x10, 0x04, 0x02};
      public static byte[] TRANSMIT_DLE_ERROR_STATUS = {0x10, 0x04, 0x03};
      public static byte[] TRANSMIT_DLE_ROLL_PAPER_SENSOR_STATUS = {0x10, 0x04, 0x04};
  
  
  
      public static final byte[] ESC_FONT_COLOR_DEFAULT = new byte[] { 0x1B, 'r',0x00 };
      public static final byte[] FS_FONT_ALIGN = new byte[] { 0x1C, 0x21, 1, 0x1B, 0x21, 1 };
      public static final byte[] ESC_ALIGN_LEFT = new byte[] { 0x1b, 'a', 0x00 };
      public static final byte[] ESC_ALIGN_RIGHT = new byte[] { 0x1b, 'a', 0x02 };
      public static final byte[] ESC_ALIGN_CENTER = new byte[] { 0x1b, 'a', 0x01 };
      public static final byte[] ESC_CANCEL_BOLD = new byte[] { 0x1B, 0x45, 0 };
  
  
      /*********************************************/
      public static final byte[] ESC_HORIZONTAL_CENTERS = new byte[] { 0x1B, 0x44, 20, 28, 00};
      public static final byte[] ESC_CANCLE_HORIZONTAL_CENTERS = new byte[] { 0x1B, 0x44, 00 };
      /*********************************************/
  
      public static final byte[] ESC_ENTER = new byte[] { 0x1B, 0x4A, 0x40 };
      public static final byte[] PRINTE_TEST = new byte[] { 0x1D, 0x28, 0x41 };
  
  }
  </code></pre>