天天看点

Android网络打印机的那些事

最近做项目有碰到使用Android手机连接网络打印机打印小票,把研究了好几天东西记录分享下。

基于环境

测试网络打印机爱普生(Epson),测试手机华为Meta9,P9,Meta8。

  • 打印一维码,我这边测试不行,有大神知道可以告诉我,可以使用zxing生成一维码二维码,在转图片,反正我用的是这种方式
  • 打印二维码OK
  • 文本打印完要记得多带几个回车在切纸

连接网络打印机

/**
     * 网络打印机 打开网络打印机
     * 端口都是
     * @param ipaddress
     * @param netport
     * @return
     */
    public boolean connction(String ipaddress, int netport)
    {
        try
        {
            Socket socket;
            if (socket!=null){
                socket.close();
            }
            SocketAddress ipe = new InetSocketAddress(ipaddress,);
            socket = new Socket();  //Socket(ipaddress, netport,true);
            socket.connect(ipe);
            socket.setSoTimeout();

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
           

打印的工具类

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Base64;
import android.util.Log;



import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Vector;

/**
 * @author: ZhuHuaHua
 * @data 2016/11/24 17:48
 */

public class PrinterTest
{

    Vector<Byte> Command = null;
    public PrinterTest() {
        this.Command = new Vector(, );
    }

    /**
     * 初始化打印机,主要清空打印机缓存数据,这步很重要,要不打印机容易内存溢出,打印出乱码
     */
    public void printInit()
    {
        byte[] command = {27, 64};
        addArrayToCommand(command);
    }

    private void addArrayToCommand(byte[] array) {
        for (int i = ; i < array.length; i++) {
            this.Command.add(Byte.valueOf(array[i]));
        }
    }

    /**
     * 换行回车
     */
    public void printEnter()
    {
        byte[] command = {10};
        addArrayToCommand(command);
    }



    /**
     * 功能:选择对齐方式
     * @param 0:左对齐 :居中对齐 :右对齐
     */
    public void printTextAlign(int align) {
        byte[] command = {,,(byte) align};
        addArrayToCommand(command);
    }

    /**
     *
     * @param bitmap  位图
     * @param nWidth  打印宽度(可以用于缩放图片)
     * @param nMode 打印模式 0: 正常 1:倍宽 2:倍高 3:倍宽 倍高
     */
    public void addRastBitImage(Bitmap bitmap, int nWidth, int nMode) {
        if (bitmap != null) {
            int width = (nWidth + ) /  * ;
            int height = bitmap.getHeight() * width / bitmap.getWidth();
            Bitmap grayBitmap = GpUtils.toGrayscale(bitmap);
            Bitmap rszBitmap = GpUtils.resizeImage(grayBitmap, width, height);
            byte[] src = GpUtils.bitmapToBWPix(rszBitmap);
            byte[] command = new byte[];
            height = src.length / width;
            command[] = ;
            command[] = ;
            command[] = ;
            command[] = ((byte) (nMode & ));
            command[] = ((byte) (width /  % ));
            command[] = ((byte) (width /  / ));
            command[] = ((byte) (height % ));
            command[] = ((byte) (height / ));
            addArrayToCommand(command);
            byte[] codecontent = GpUtils.pixToEscRastBitImageCmd(src);
            for (int k = ; k < codecontent.length; k++) {
                this.Command.add(Byte.valueOf(codecontent[k]));
            }
        } else {
            Log.d("BMP", "bmp.  null ");
        }
    }
    /**
     * 打印图片
     * @param mbitmap  对象bitmap
     */
    public Vector<Byte> CMD_Image(Bitmap mbitmap) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inScaled = false;
            byte[] decodedByte = Base64.decode(bitmapToBase64(mbitmap), Base64.DEFAULT);
            Bitmap bitmap = BitmapFactory.decodeByteArray(decodedByte, , decodedByte.length);
            final int imgWidth = bitmap.getWidth();
            final int imgHeight = bitmap.getHeight();
            final int[] argb = new int[imgWidth * imgHeight];
            bitmap.getPixels(argb, , imgWidth, , , imgWidth, imgHeight);

            EscCommand esc = new EscCommand();

            esc.addSelectJustification(EscCommand.JUSTIFICATION.CENTER);
            esc.addRastBitImage(bitmap, imgWidth, );
            esc.addSelectJustification(EscCommand.JUSTIFICATION.LEFT);
            Vector<Byte> Command  = esc.getCommand();
            Vector<Byte> data = new Vector<>(Command.size());
            for (int i = ; i < Command.size(); i++) {
                data.add(Command.get(i));
            }
            bitmap.recycle();
        return  data;
    }

    /**
     * bitmap转为base64
     * @param bitmap
     * @return
     */
    public static String bitmapToBase64(Bitmap bitmap) {

        String result = null;
        ByteArrayOutputStream baos = null;
        try {
            if (bitmap != null) {
                baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, , baos);
                baos.flush();
                baos.close();

                byte[] bitmapBytes = baos.toByteArray();
                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
    /**
     * 打印二维条形码
     * @param printData  打印的内容
     */
    public Vector<Byte> CMD_Qrcode(String printData) {
        EscCommand esc = new EscCommand();
        esc.addSelectJustification(EscCommand.JUSTIFICATION.CENTER);
        esc.addSelectErrorCorrectionLevelForQRCode((byte) ); //设置纠错等级
        esc.addSelectSizeOfModuleForQRCode((byte) );//设置qrcode模块大小
        esc.addStoreQRCodeData(printData);//设置qrcode内容
        esc.addPrintQRCode();//打印QRCode
        esc.addSelectJustification(EscCommand.JUSTIFICATION.LEFT);
        esc.addPrintAndLineFeed();
        return esc.getCommand();
    }
    /**
     * 打印的一维码
     * @param printData  需要打印的数据
     */
    public Vector<Byte> CMD_Barcode(String printData, Context context) {

         EscCommand esc = new EscCommand();
//        Bitmap b = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
//        px2Byte(,,b);
//        esc.addRastBitImage(b, , ); //打印图片
//            esc.addText("printData");
            esc.addSelectJustification(EscCommand.JUSTIFICATION.CENTER);
//             /*打印一维条码*/
            esc.addSetBarcodeWidth((byte) );
            esc.addSetBarcodeHeight((byte) ); //设置条码高度为点
                esc.addSelectPrintingPositionForHRICharacters(EscCommand.HRI_POSITION.BELOW);//设置条码可识别字符位置在条码下方
            esc.addCODE128(printData);  //打印Code128码
            esc.addPrintAndLineFeed();
//            esc.addSelectJustification(EscCommand.JUSTIFICATION.LEFT);
//            esc.addPrintAndLineFeed();
            return  esc.getCommand();
    }


    /**
     * 走纸
     * @param line  走纸的行数
     */
    public void printLine(int line)
    {
        byte[] command = {,,(byte) line};
        addArrayToCommand(command);
    }



    /**
     * 切纸
     */
    public void printCutPage()
    {
        byte[] command = {,};
        addArrayToCommand(command);
    }

    /**
     * 打开钱箱
     * @return
     */
    public void printQianXiang() {
        byte[] command = {,,,,(byte) };
        addArrayToCommand(command);
    }
}
           

发送指令

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.util.Vector;

public class EscCommand {
  private static final String DEBUG_TAG = "EscCommand";
  Vector<Byte> Command = null;

  public  enum STATUS {
    PRINTER_STATUS(),
    PRINTER_OFFLINE(),
    PRINTER_ERROR(),
    PRINTER_PAPER();

    private final int value;

     STATUS(int value) {
      this.value = value;
    }

    public byte getValue() {
      return (byte) this.value;
    }
  }

  public  enum ENABLE {
    OFF(),
    ON();

    private final int value;

     ENABLE(int value) {
      this.value = value;
    }

    public byte getValue() {
      return (byte) this.value;
    }
  }

  public  enum UNDERLINE_MODE {
    OFF(),
    UNDERLINE_1DOT(),
    UNDERLINE_2DOT();

    private final int value;

     UNDERLINE_MODE(int value) {
      this.value = value;
    }

    public byte getValue() {
      return (byte) this.value;
    }
  }

  public  enum FONT {
    FONTA(),
    FONTB();

    private final int value;

     FONT(int value) {
      this.value = value;
    }

    public byte getValue() {
      return (byte) this.value;
    }
  }

  public  enum CHARACTER_SET {
    USA(),
    FRANCE(),
    GERMANY(),
    UK(),
    DENMARK_I(),
    SWEDEN(),
    ITALY(),
    SPAIN_I(),
    JAPAN(),
    NORWAY(),
    DENMARK_II(),
    SPAIN_II(),
    LATIN_AMERCIA(),
    KOREAN(),
    SLOVENIA(),
    CHINA();

    private final int value;

     CHARACTER_SET(int value) {
      this.value = value;
    }

    public byte getValue() {
      return (byte) this.value;
    }
  }

  public  enum JUSTIFICATION {
    LEFT(),
    CENTER(),
    RIGHT();

    private final int value;

     JUSTIFICATION(int value) {
      this.value = value;
    }

    public byte getValue() {
      return (byte) this.value;
    }
  }

  public  enum CODEPAGE {
    PC437(),
    KATAKANA(),
    PC850(),
    PC860(),
    PC863(),
    PC865(),
    WEST_EUROPE(),
    GREEK(),
    HEBREW(),
    EAST_EUROPE(),
    IRAN(),
    WPC1252(),
    PC866(),
    PC852(),
    PC858(),
    IRANII(),
    LATVIAN(),
    ARABIC(),
    PT151(),
    PC747(),
    WPC1257(),
    VIETNAM(),
    PC864(),
    PC1001(),
    UYGUR(),
    THAI();

    private final int value;

     CODEPAGE(int value) {
      this.value = value;
    }

    public byte getValue() {
      return (byte) this.value;
    }
  }

  public  enum WIDTH_ZOOM {
    MUL_1(), MUL_2(), MUL_3(), MUL_4(), MUL_5(), MUL_6(), MUL_7(), MUL_8();

    private final int value;

     WIDTH_ZOOM(int value) {
      this.value = value;
    }

    public byte getValue() {
      return (byte) this.value;
    }
  }

  public  enum HEIGHT_ZOOM {
    MUL_1(), MUL_2(), MUL_3(), MUL_4(), MUL_5(), MUL_6(), MUL_7(), MUL_8();

    private final int value;

     HEIGHT_ZOOM(int value) {
      this.value = value;
    }

    public byte getValue() {
      return (byte) this.value;
    }
  }

  public  enum HRI_POSITION {
    NO_PRINT(), ABOVE(), BELOW(), ABOVE_AND_BELOW();

    private final int value;

     HRI_POSITION(int value) {
      this.value = value;
    }

    public byte getValue() {
      return (byte) this.value;
    }
  }

  public EscCommand() {
    this.Command = new Vector(, );
  }

  private void addArrayToCommand(byte[] array) {
    for (int i = ; i < array.length; i++) {
      this.Command.add(Byte.valueOf(array[i]));
    }
  }


  private void addStrToCommand(String str, String charset) {
    byte[] bs = null;
    if (!str.equals("")) {
      try {
        bs = str.getBytes(charset);
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }
      for (int i = ; i < bs.length; i++) {
        this.Command.add(Byte.valueOf(bs[i]));
      }
    }
  }

  private void addStrToCommand(String str, int length) {
    byte[] bs = null;
    if (!str.equals("")) {
      try {
        bs = str.getBytes("GB2312");
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }
      Log.d("EscCommand", "bs.length" + bs.length);
      if (length > bs.length) {
        length = bs.length;
      }
      Log.d("EscCommand", "length" + length);
      for (int i = ; i < length; i++) {
        this.Command.add(Byte.valueOf(bs[i]));
      }
    }
  }


  public void addText(String text, String charsetName) {
    addStrToCommand(text, charsetName);
  }

  public void addPrintAndLineFeed() {
    byte[] command = {10};
    addArrayToCommand(command);
  }



  public Vector<Byte> getCommand() {
    return this.Command;
  }




  public void addInitializePrinter() {
    byte[] command = {27, 64};
    addArrayToCommand(command);
  }

  public void addTurnEmphasizedModeOnOrOff(ENABLE enabel) {
    byte[] command = new byte[]{(byte)27, (byte)32, enabel.getValue()};
    addArrayToCommand(command);
  }




  /**
   * addSelectJustification(JUSTIFICATION just)
   功能:选择对齐方式
   参数: enum JUSTIFICATION{
   LEFT(), // 左对齐
   CENTER(),// 居中对齐
   RIGHT();//右对齐
   }

   * @param just
     */
  public void addSelectJustification(JUSTIFICATION just) {

    byte[] command = new byte[]{(byte)27, (byte)97, just.getValue()};
    addArrayToCommand(command);


  }

  /**
   * 添加打印行的指令
   * @param n
     */
  public void addPrintAndFeedLines(byte n) {
    byte[] command = new byte[]{(byte)27, (byte)100, n};
    addArrayToCommand(command);
  }

  public void addGeneratePlus(TscCommand.FOOT foot, byte t1, byte t2) {
    byte[] command = {27, 112, 0, 0, 0};
    command[2] = ((byte) foot.getValue());
    command[3] = t1;
    command[4] = t2;
    addArrayToCommand(command);
  }

  public void addSelectCodePage(CODEPAGE page) {
    byte[] command = {27, 116, 0};
    command[2] = page.getValue();
    addArrayToCommand(command);
  }

  public void addTurnUpsideDownModeOnOrOff(ENABLE enable) {
    byte[] command = {27, 123, 0};
    command[2] = enable.getValue();
    addArrayToCommand(command);
  }

  public void addSetCharcterSize(WIDTH_ZOOM width, HEIGHT_ZOOM height) {
    byte[] command = {29, 33, 0};
    byte temp = 0;
    temp = (byte) (temp | width.getValue());
    temp = (byte) (temp | height.getValue());
    command[2] = temp;
    addArrayToCommand(command);
  }

  public void addTurnReverseModeOnOrOff(ENABLE enable) {
    byte[] command = {29, 66, 0};
    command[2] = enable.getValue();
    addArrayToCommand(command);
  }

  public void addSelectPrintingPositionForHRICharacters(HRI_POSITION position) {
    byte[] command = {29, 72, 0};
    command[2] = position.getValue();
    addArrayToCommand(command);
  }



  public void addSetBarcodeHeight(byte height) {
    byte[] command = new byte[]{(byte) 29, (byte) 104, height};
    this.addArrayToCommand(command);
  }

  public void addSetBarcodeWidth(byte width) {
    byte[] command = new byte[]{(byte)29, (byte)119, (byte)0};
    if(width > ) {
      width = 6;
    }

    if(width < ) {
      width = 2;
    }

    command[2] = width;
    this.addArrayToCommand(command);
  }

  public void addSetKanjiFontMode(ENABLE DoubleWidth, ENABLE DoubleHeight, ENABLE Underline) {
    byte[] command = {28, 33, 0};
    byte temp = 0;
    if (DoubleWidth == ENABLE.ON) {
      temp = (byte) (temp | );
    }
    if (DoubleHeight == ENABLE.ON) {
      temp = (byte) (temp | );
    }
    if (Underline == ENABLE.ON) {
      temp = (byte) (temp | );
    }
    command[2] = temp;
    addArrayToCommand(command);
  }



  /**
   *
   * @param bitmap  位图
   * @param nWidth  打印宽度(可以用于缩放图片)
     * @param nMode 打印模式 0: 正常 1:倍宽 2:倍高 3:倍宽 倍高
     */
  public void addRastBitImage(Bitmap bitmap, int nWidth, int nMode) {
    if (bitmap != null) {
      int width = (nWidth + ) / 8 * 8;
      int height = bitmap.getHeight() * width / bitmap.getWidth();
      Bitmap grayBitmap = GpUtils.toGrayscale(bitmap);
      Bitmap rszBitmap = GpUtils.resizeImage(grayBitmap, width, height);
      byte[] src = GpUtils.bitmapToBWPix(rszBitmap);
      byte[] command = new byte[8];
      height = src.length / width;
      command[0] = 29;
      command[1] = 118;
      command[2] = 48;
      command[3] = ((byte) (nMode & ));
      command[4] = ((byte) (width /  % ));
      command[5] = ((byte) (width /  / ));
      command[6] = ((byte) (height % ));
      command[7] = ((byte) (height / ));
      addArrayToCommand(command);
      byte[] codecontent = GpUtils.pixToEscRastBitImageCmd(src);
      for (int k = ; k < codecontent.length; k++) {
        this.Command.add(Byte.valueOf(codecontent[k]));
      }
    } else {
      Log.d("BMP", "bmp.  null ");
    }
  }

  public void addDownloadNvBitImage(Bitmap[] bitmap) {
    if (bitmap != null) {
      Log.d("BMP", "bitmap.length " + bitmap.length);
      int n = bitmap.length;
      if (n > ) {
        byte[] command = new byte[3];
        command[0] = 28;
        command[1] = 113;
        command[2] = ((byte) n);
        addArrayToCommand(command);
        for (int i = ; i < n; i++) {
          int height = (bitmap[i].getHeight() + ) / 8 * 8;
          int width = bitmap[i].getWidth() * height / bitmap[i].getHeight();
          Bitmap grayBitmap = GpUtils.toGrayscale(bitmap[i]);
          Bitmap rszBitmap = GpUtils.resizeImage(grayBitmap, width, height);
          byte[] src = GpUtils.bitmapToBWPix(rszBitmap);
          height = src.length / width;
          Log.d("BMP", "bmp  Width " + width);
          Log.d("BMP", "bmp  height " + height);
          byte[] codecontent = GpUtils.pixToEscNvBitImageCmd(src, width, height);
          for (int k = ; k < codecontent.length; k++) {
            this.Command.add(Byte.valueOf(codecontent[k]));
          }
        }
      }
    } else {
      Log.d("BMP", "bmp.  null ");
      return;
    }
  }

  public void addPrintNvBitmap(byte n, byte mode) {
    byte[] command = {28, 112, 0, 0};

    command[2] = n;
    command[3] = mode;
    addArrayToCommand(command);
  }

  public void addUPCA(String content) {
    byte[] command = new byte[4];
    command[0] = 29;
    command[1] = 107;
    command[2] = 65;
    command[3] = 11;
    if (content.length() < command[]) {
      return;
    }
    addArrayToCommand(command);
    addStrToCommand(content, );
  }

  public void addUPCE(String content) {
    byte[] command = new byte[4];
    command[0] = 29;
    command[1] = 107;
    command[2] = 66;
    command[3] = 11;
    if (content.length() < command[]) {
      return;
    }
    addArrayToCommand(command);
    addStrToCommand(content, command[]);
  }

  public void addEAN13(String content) {
    byte[] command = new byte[4];
    command[0] = 29;
    command[1] = 107;
    command[2] = 67;
    command[3] = 12;
    if (content.length() < command[]) {
      return;
    }
    addArrayToCommand(command);
    Log.d("EscCommand", "content.length" + content.length());
    addStrToCommand(content, command[]);
  }

  public void addEAN8(String content) {
    byte[] command = new byte[4];
    command[0] = 29;
    command[1] = 107;
    command[2] = 68;
    command[3] = 7;
    if (content.length() < command[]) {
      return;
    }
    addArrayToCommand(command);
    addStrToCommand(content, command[]);
  }

  @SuppressLint({"DefaultLocale"})
  public void addCODE39(String content) {
    byte[] command = new byte[4];
    command[0] = 29;
    command[1] = 107;
    command[2] = 69;
    command[3] = ((byte) content.length());
    content = content.toUpperCase();
    addArrayToCommand(command);
    addStrToCommand(content, command[]);
  }

  public void addITF(String content) {
    byte[] command = new byte[4];
    command[0] = 29;
    command[1] = 107;
    command[2] = 70;
    command[3] = ((byte) content.length());
    addArrayToCommand(command);
    addStrToCommand(content, command[]);
  }

  public void addCODABAR(String content) {
    byte[] command = new byte[4];
    command[0] = 29;
    command[1] = 107;
    command[2] = 71;
    command[3] = ((byte) content.length());
    addArrayToCommand(command);
    addStrToCommand(content, command[]);
  }

  public void addCODE93(String content) {
    byte[] command = new byte[4];
    command[0] = 29;
    command[1] = 107;
    command[2] = 72;
    command[3] = ((byte) content.length());
    addArrayToCommand(command);
    addStrToCommand(content, command[]);
  }

  public void addCODE128(String content) {
    byte[] data = content.getBytes();

    if (data[] != )
    {
      byte[] command = new byte[]{(byte)29, (byte)107, (byte)73, (byte)(content.length() + ), (byte)123, (byte)66};
      this.addArrayToCommand(command);
    }
    else {
      byte[] command = new byte[]{(byte)29, (byte)107, (byte)73, (byte)content.length()};
      this.addArrayToCommand(command);
    }

    this.addStrToCommand(content, content.length());


  }

  /**
   * 功能:设置QRCode的单元模块大小
   参数:n: 单元模块为n点 默认为3点
   返回值:无
   相关指令:GP58编程手册 GS ( k <Function >

   * @param n
     */
  public void addSelectSizeOfModuleForQRCode(byte n) {
    byte[] command = {, , , , , , , };
    command[] = n;
    addArrayToCommand(command);
  }

  public void addSelectErrorCorrectionLevelForQRCode(byte n) {
    byte[] command = new byte[]{, , , , , , , n};
    addArrayToCommand(command);
  }

  public void addStoreQRCodeData(String content) {
    byte[] command = {, , , , , , , };
    command[] = ((byte) ((content.length() + ) % ));
    command[] = ((byte) ((content.length() + ) / ));
    addArrayToCommand(command);
    addStrToCommand(content, content.length());
  }

  public void addPrintQRCode() {

    byte[] command = {, , , , , , , };
    addArrayToCommand(command);
  }

  public void addUserCommand(byte[] command) {
    addArrayToCommand(command);
  }
}

           

工具类

import android.graphics.Bitmap;
  import android.graphics.Canvas;
  import android.graphics.ColorMatrix;
  import android.graphics.ColorMatrixColorFilter;
  import android.graphics.Matrix;
  import android.graphics.Paint;
  import android.graphics.Bitmap.CompressFormat;
  import android.graphics.Bitmap.Config;
  import android.os.Environment;
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.UnsupportedEncodingException;

public class GpUtils {
  private static int[] p0 = new int[]{0, 128};
  private static int[] p1 = new int[]{0, 64};
  private static int[] p2 = new int[]{0, 32};
  private static int[] p3 = new int[]{0, 16};
  private static int[] p4 = new int[]{0, 8};
  private static int[] p5 = new int[]{0, 4};
  private static int[] p6 = new int[]{0, 2};
  private static int[][] Floyd16x16 = new int[][]{{0, 128, 32, 160, 8, 136, 40, 168, 2, 130, 34, 162, 10, 138, 42, 170}, {192, 64, 224, 96, 200, 72, 232, 104, 194, 66, 226, 98, 202, 74, 234, 106}, {48, 176, 16, 144, 56, 184, 24, 152, 50, 178, 18, 146, 58, 186, 26, 154}, {240, 112, 208, 80, 248, 120, 216, 88, 242, 114, 210, 82, 250, 122, 218, 90}, {12, 140, 44, 172, 4, 132, 36, 164, 14, 142, 46, 174, 6, 134, 38, 166}, {204, 76, 236, 108, 196, 68, 228, 100, 206, 78, 238, 110, 198, 70, 230, 102}, {60, 188, 28, 156, 52, 180, 20, 148, 62, 190, 30, 158, 54, 182, 22, 150}, {252, 124, 220, 92, 244, 116, 212, 84, 254, 126, 222, 94, 246, 118, 214, 86}, {3, 131, 35, 163, 11, 139, 43, 171, 1, 129, 33, 161, 9, 137, 41, 169}, {195, 67, 227, 99, 203, 75, 235, 107, 193, 65, 225, 97, 201, 73, 233, 105}, {51, 179, 19, 147, 59, 187, 27, 155, 49, 177, 17, 145, 57, 185, 25, 153}, {243, 115, 211, 83, 251, 123, 219, 91, 241, 113, 209, 81, 249, 121, 217, 89}, {15, 143, 47, 175, 7, 135, 39, 167, 13, 141, 45, 173, 5, 133, 37, 165}, {207, 79, 239, 111, 199, 71, 231, 103, 205, 77, 237, 109, 197, 69, 229, 101}, {63, 191, 31, 159, 55, 183, 23, 151, 61, 189, 29, 157, 53, 181, 21, 149}, {254, 127, 223, 95, 247, 119, 215, 87, 253, 125, 221, 93, 245, 117, 213, 85}};
  private static int[][] Floyd8x8 = new int[][]{{0, 32, 8, 40, 2, 34, 10, 42}, {48, 16, 56, 24, 50, 18, 58, 26}, {12, 44, 4, 36, 14, 46, 6, 38}, {60, 28, 52, 20, 62, 30, 54, 22}, {3, 35, 11, 43, 1, 33, 9, 41}, {51, 19, 59, 27, 49, 17, 57, 25}, {15, 47, 7, 39, 13, 45, 5, 37}, {63, 31, 55, 23, 61, 29, 53, 21}};
  public static final int ALGORITHM_DITHER_16x16 = 16;
  public static final int ALGORITHM_DITHER_8x8 = 8;
  public static final int ALGORITHM_TEXTMODE = 2;
  public static final int ALGORITHM_GRAYTEXTMODE = 1;

  public GpUtils() {
  }

  public static Bitmap resizeImage(Bitmap bitmap, int w, int h) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float scaleWidth = (float)w / (float)width;
    float scaleHeight = (float)h / (float)height;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, , , width, height, matrix, true);
    return resizedBitmap;
  }

  public static void saveMyBitmap(Bitmap mBitmap) {
    File f = new File(Environment.getExternalStorageDirectory().getPath(), "Btatotest.jpeg");

    try {
      f.createNewFile();
    } catch (IOException var6) {
      ;
    }

    FileOutputStream fOut = null;

    try {
      fOut = new FileOutputStream(f);
      mBitmap.compress(CompressFormat.PNG, , fOut);
      fOut.flush();
      fOut.close();
    } catch (FileNotFoundException var4) {
      ;
    } catch (IOException var5) {
      ;
    }

  }

  public static Bitmap toGrayscale(Bitmap bmpOriginal) {
    int height = bmpOriginal.getHeight();
    int width = bmpOriginal.getWidth();
    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Config.RGB_565);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(F);
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, F, F, paint);
    return bmpGrayscale;
  }

  static byte[] pixToEscRastBitImageCmd(byte[] src, int nWidth, int nMode) {
    int nHeight = src.length / nWidth;
    byte[] data = new byte[8 + src.length / 8];
    data[0] = 29;
    data[1] = 118;
    data[2] = 48;
    data[3] = (byte)(nMode & );
    data[4] = (byte)(nWidth /  % );
    data[5] = (byte)(nWidth /  / );
    data[6] = (byte)(nHeight % );
    data[7] = (byte)(nHeight / );
    int i = 8;

    for(int k = ; i < data.length; ++i) {
      data[i] = (byte)(p0[src[k]] + p1[src[k + ]] + p2[src[k + ]] + p3[src[k + ]] + p4[src[k + ]] + p5[src[k + ]] + p6[src[k + ]] + src[k + ]);
      k += 8;
    }

    return data;
  }

  public static byte[] pixToEscRastBitImageCmd(byte[] src) {
    byte[] data = new byte[src.length / 8];
    int i = 0;

    for(int k = ; i < data.length; ++i) {
      data[i] = (byte)(p0[src[k]] + p1[src[k + ]] + p2[src[k + ]] + p3[src[k + ]] + p4[src[k + ]] + p5[src[k + ]] + p6[src[k + ]] + src[k + ]);
      k += 8;
    }

    return data;
  }

  static byte[] pixToEscNvBitImageCmd(byte[] src, int width, int height) {
    byte[] data = new byte[src.length / 8 + 4];
    data[0] = (byte)(width /  % );
    data[1] = (byte)(width /  / );
    data[2] = (byte)(height /  % );
    data[3] = (byte)(height /  / );
    boolean k = false;

    for(int i = ; i < width; ++i) {
      int var7 = 0;

      for(int j = ; j < height / ; ++j) {
        data[4 + j + i * height / 8] = (byte)(p0[src[i + var7]] + p1[src[i + var7 +  * width]] + p2[src[i + var7 +  * width]] + p3[src[i + var7 +  * width]] + p4[src[i + var7 +  * width]] + p5[src[i + var7 +  * width]] + p6[src[i + var7 +  * width]] + src[i + var7 +  * width]);
        var7 += 8 * width;
      }
    }

    return data;
  }

  public static byte[] pixToTscCmd(byte[] src) {
    byte[] data = new byte[src.length / 8];
    int k = 0;

    for(int j = ; k < data.length; ++k) {
      byte temp = (byte)(p0[src[j]] + p1[src[j + ]] + p2[src[j + ]] + p3[src[j + ]] + p4[src[j + ]] + p5[src[j + ]] + p6[src[j + ]] + src[j + ]);
      data[k] = (byte)(~temp);
      j += 8;
    }

    return data;
  }

  public static byte[] pixToTscCmd(int x, int y, int mode, byte[] src, int nWidth) {
    int height = src.length / nWidth;
    int width = nWidth / 8;
    String str = "BITMAP " + x + "," + y + "," + width + "," + height + "," + mode + ",";
    byte[] bitmap = null;

    try {
      bitmap = str.getBytes("GB2312");
    } catch (UnsupportedEncodingException var13) {
      var13.printStackTrace();
    }

    byte[] arrayOfByte = new byte[src.length / 8];
    int data = 0;

    for(int j = ; data < arrayOfByte.length; ++data) {
      byte temp = (byte)(p0[src[j]] + p1[src[j + ]] + p2[src[j + ]] + p3[src[j + ]] + p4[src[j + ]] + p5[src[j + ]] + p6[src[j + ]] + src[j + ]);
      arrayOfByte[data] = (byte)(~temp);
      j += 8;
    }

    byte[] var14 = new byte[bitmap.length + arrayOfByte.length];
    System.arraycopy(bitmap, , var14, , bitmap.length);
    System.arraycopy(arrayOfByte, , var14, bitmap.length, arrayOfByte.length);
    return var14;
  }

  private static void format_K_dither16x16(int[] orgpixels, int xsize, int ysize, byte[] despixels) {
    int k = 0;

    for(int y = ; y < ysize; ++y) {
      for(int x = ; x < xsize; ++x) {
        if((orgpixels[k] & ) > Floyd16x16[x & ][y & ]) {
          despixels[k] = 0;
        } else {
          despixels[k] = 1;
        }

        ++k;
      }
    }

  }

  public static byte[] bitmapToBWPix(Bitmap mBitmap) {
    int[] pixels = new int[mBitmap.getWidth() * mBitmap.getHeight()];
    byte[] data = new byte[mBitmap.getWidth() * mBitmap.getHeight()];
    Bitmap grayBitmap = toGrayscale(mBitmap);
    grayBitmap.getPixels(pixels, , mBitmap.getWidth(), , , mBitmap.getWidth(), mBitmap.getHeight());
    format_K_dither16x16(pixels, grayBitmap.getWidth(), grayBitmap.getHeight(), data);
    return data;
  }

  private static void format_K_dither16x16_int(int[] orgpixels, int xsize, int ysize, int[] despixels) {
    int k = 0;

    for(int y = ; y < ysize; ++y) {
      for(int x = ; x < xsize; ++x) {
        if((orgpixels[k] & ) > Floyd16x16[x & ][y & ]) {
          despixels[k] = -1;
        } else {
          despixels[k] = -16777216;
        }

        ++k;
      }
    }

  }

  private static void format_K_dither8x8_int(int[] orgpixels, int xsize, int ysize, int[] despixels) {
    int k = 0;

    for(int y = ; y < ysize; ++y) {
      for(int x = ; x < xsize; ++x) {
        if((orgpixels[k] & ) >>  > Floyd8x8[x & ][y & ]) {
          despixels[k] = -1;
        } else {
          despixels[k] = -16777216;
        }

        ++k;
      }
    }

  }

  public static int[] bitmapToBWPix_int(Bitmap mBitmap, int algorithm) {
    int[] pixels = new int[0];
    Bitmap grayBitmap;
    switch(algorithm) {
      case 2:
        break;
      case :
        grayBitmap = toGrayscale(mBitmap);
        pixels = new int[grayBitmap.getWidth() * grayBitmap.getHeight()];
        grayBitmap.getPixels(pixels, , grayBitmap.getWidth(), , , grayBitmap.getWidth(), grayBitmap.getHeight());
        format_K_dither8x8_int(pixels, grayBitmap.getWidth(), grayBitmap.getHeight(), pixels);
        break;
      case :
      default:
        grayBitmap = toGrayscale(mBitmap);
        pixels = new int[grayBitmap.getWidth() * grayBitmap.getHeight()];
        grayBitmap.getPixels(pixels, , grayBitmap.getWidth(), , , grayBitmap.getWidth(), grayBitmap.getHeight());
        format_K_dither16x16_int(pixels, grayBitmap.getWidth(), grayBitmap.getHeight(), pixels);
    }

    return pixels;
  }

  public static Bitmap toBinaryImage(Bitmap mBitmap, int nWidth, int algorithm) {
    int width = (nWidth + ) /  * ;
    int height = mBitmap.getHeight() * width / mBitmap.getWidth();
    Bitmap rszBitmap = resizeImage(mBitmap, width, height);
    int[] pixels = bitmapToBWPix_int(rszBitmap, algorithm);
    rszBitmap.setPixels(pixels, , width, , , width, height);
    return rszBitmap;
  }
}
           

参考以下指令集

【esc/pos打印指令 (小寿转载)-xiaoshou330-ChinaUnix博客】 http://m.blog.chinaunix.net/uid-7491192-id-2051201.html

【esc打印控制命令集 - 豆丁网】

http://m.docin.com/p2-1229264477.html

android 控制POS机图文打印(二)

http://blog.csdn.net/sdvch/article/details/45096057

Android 蓝牙连接 ESC/POS 热敏打印机打印(ESC/POS指令篇)

http://www.jianshu.com/p/c0b6d1a4823b