package com.common.util.portprinter;
import java.io.IOException;
import java.io.OutputStream;
import com.common.util.PrinterParameterConf;
/**
* @author ChenMing
*
*/
public class PortPrinterBase {
    private OutputStream out;
    protected int lineCount = 40;
    private String printType="0";
    public PortPrinterBase(OutputStream out, String printType){
        this.out = out;
        this.printType = printType;
        initPrinter();
        String lineCountStr = PrinterParameterConf.printerParameterConf.getProperty(PrinterParameterConf.LINEPRINTERCHARCOUNT_NAME);
        try{
            int temp = Integer.parseInt(lineCountStr);
            this.lineCount = temp;
        }catch(Exception e){   
        }
    }
    protected final String LEFT = "LEFT";
    protected final String CENTER = "CENTER";
    protected final String RIGHT = "RIGHT";
    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[] ESC_INIT = new byte[] {ESC, '@'};
    /* 设置标准模式 */
    public static final byte[] ESC_STANDARD = new byte[] {ESC, 'S'};
    /* 设置汉字打印模式 */
    public static final byte[] ESC_CN_FONT = new byte[] {FS, '&'};
    /* 选择字符集 */
    public static final byte[] ESC_SELECT_CHARACTER = new byte[] {ESC, 'R', 9};
    /* 设置用户自定义汉字字体 焗7118 */
    public static final byte[] ESC_FS_2 = new byte[] {FS, 0x32, 0x71, 0x18};
    /* 取消用户自定义字体 */
    public static final byte[] ESC_CANCEL_DEFINE_FONT = new byte[]{ESC, '%', 0};
    /* 打开钱箱指令 */
    public static final byte[] ESC_OPEN_DRAWER = new byte[]{ESC, 'p', 0x00, 0x10, (byte) 0xff};
    /* 切纸指令GS V m  
    * m  0,48 Executes a full cut(cuts the paper completely)
    *    1,49 Excutes a partilal cut(one point left uncut)
    */
   public static final byte[] POS_CUT_MODE_FULL = new byte[]{GS, 'V', 0x00};
   public static final byte[] POS_CUT_MODE_PARTIAL = new byte[]{GS, 'V', 0x01};
    /* 西文字符 (半宽)字体A (6 ×12),汉字字符 (全宽)字体A (12×12) */
    public static final byte[] ESC_FONT_A = new byte[]{ESC, '!', 0};
    /* 西文字符 (半宽)字体B (8×16),汉字字符 (全宽)字体B (16×16) */
    public static final byte[] ESC_FONT_B = new byte[]{ESC, '!', 1};
    /* 12*24   0/48*/
    public static final byte[] ESC_FONTA= new byte[]{ESC, 'M', 48};
    /* 9*17    1/49*/
    public static final byte[] ESC_FONTB= new byte[]{ESC, 'M', 1};
    /* 默认颜色字体指令 */
    public static final byte[] ESC_FONT_COLOR_DEFAULT = new byte[] {ESC, 'r', 0x00};
    /* 红色字体指令 */
    public static final byte[] ESC_FONT_COLOR_RED = new byte[] {ESC, 'r', 0x01 };
    /* 标准大小 */
    public static final byte[] FS_FONT_ALIGN = new byte[]{FS, 0x21, 1, ESC, 0x21, 1};
    /* 横向放大一倍 */
    public static final byte[] FS_FONT_ALIGN_DOUBLE = new byte[]{FS, 0x21, 4, ESC, 0x21, 4};
    /* 纵向放大一倍 */
    public static final byte[] FS_FONT_VERTICAL_DOUBLE = new byte[]{FS, 0x21, 8, ESC, 0x21, 8, GS, '!', 0x01};
    /* 横向纵向都放大一倍 */
    public static final byte[] FS_FONT_DOUBLE = new byte[]{FS, 0x21, 12, ESC, 0x21, 48};
    /* 靠左打印命令 */
    public static final byte[] ESC_ALIGN_LEFT = new byte[]{0x1b,'a', 0x00};
    /* 居中打印命令 */
    public static final byte[] ESC_ALIGN_CENTER = new byte[]{0x1b,'a', 0x01};
    /* 靠右打印命令 */
    public static final byte[] ESC_ALIGN_RIGHT = new byte[]{0x1b,'a', 0x02};
    /* 字体加粗 */
    public static final byte[] ESC_SETTING_BOLD = new byte[]{ESC, 0x45, 1};
    /* 取消字体加粗 */
    public static final byte[] ESC_CANCEL_BOLD = new byte[]{ESC, 0x45, 0};
    //DLE EOT n 实时状态传送
    //如果返回结果为22
    /**
     * 、DLE EOT n 实时状态传送
        [格式] ASCII码 DLE EOT n
        十六进制码 10 04 n
        十进制码 16 4 n
        [范围] 1 ≤ n ≤ 4
        [描述] 根据下列参数,实时传送打印机状态,参数 n 用来指定所要传送的打印机状态:
        n = 1:传送打印机状态
        n = 2:传送脱机状态
        n = 3:传送错误状态
        n = 4:传送纸传感器状态
        [注释] 打印机收到该命令后立即返回相关状态
        该命令尽量不要插在2个或更多字节的命令序列中。
        即使打印机被ESC =(选择外设)命令设置为禁止,该命令依然有效。
        打印机传送当前状态,每一状态用1个字节数据表示。
        打印机传送状态时并不确认主机是否收到。
        打印机收到该命令立即执行。
        该命令只对串口打印机有效。打印机在任何状态下收到该命令都立即执行。
     */
    public static final byte[] PRINT_STATE_DLE_EOT = new byte[] {DLE, EOT,0x01};
    public void initPrinter(){
        try {
            //modify by gongqiyi 20090917
            //ESC_INIT 将在清空缓存区的数据
            //out.write(ESC_INIT);
            //自定义字体
            //out.write(ESC_FS_2);
            out.write(ESC_STANDARD);
            out.write(ESC_CANCEL_DEFINE_FONT);
            out.write(ESC_FONTA);
            out.write(ESC_SELECT_CHARACTER);
            //进入汉字模式打印
            //out.write(ESC_CN_FONT);
            //out.write(ESC_FONT_B);
            //out.write(ESC_FONTA);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 走纸到切纸位置并切纸
     */
    public void executeLineFeedAndPaperCut(){
        try {
            out.write(PrinterParameterConf.printerParameterConf.getProperty
          (PrinterParameterConf.PRINTCUTLINE_NAME).getBytes());
            out.write(POS_CUT_MODE_PARTIAL);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 单据头打印
     * @param str
     */
    public void billHeaderPrinter(String str){
        try {
            out.write(ESC_ALIGN_CENTER);
            out.write(FS_FONT_DOUBLE);
            out.write((str+"\n").getBytes());
            out.write(LF);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 叫单号打印
     * @param str
     */
    public void callNumPrinter(String str){
        try {
            out.write(ESC_ALIGN_LEFT);
            out.write(FS_FONT_DOUBLE);
            out.write((str+"\n").getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 双倍大小字体
     * @param str
     */
    public void doubleSizePrinter(String str, String align){
        try {
            if(CENTER.equals(align)){
                out.write(ESC_ALIGN_LEFT);
            }else if(RIGHT.equals(align)){
                out.write(ESC_ALIGN_RIGHT);
            }else{
                out.write(ESC_ALIGN_LEFT);
            }
            out.write(FS_FONT_DOUBLE);
            out.write((str+"\n").getBytes());
            //out.write(LF);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 标准字体打印一行
     * @param str 需打印的字符串
     * @param align 打印的位置 LEFT/CENTER/RIGHT 其他为默认居左打印
     */
    public void standardPrinterLine(String str, String align){
        try{
            if(CENTER.equals(align)){
                out.write(ESC_ALIGN_CENTER);
                out.write(FS_FONT_ALIGN);
                out.write(ESC_CN_FONT);
                out.write(ESC_CANCEL_BOLD);
                if("1".equals(printType)){
                    out.write(ESC_FONTA);
                }else{
                    out.write(ESC_FONT_B);
                }
                out.write(str.getBytes());
            }else if(RIGHT.equals(align)){
                out.write(ESC_ALIGN_RIGHT);
                out.write(FS_FONT_ALIGN);
                out.write(ESC_CN_FONT);
                out.write(ESC_CANCEL_BOLD);
                if("1".equals(printType)){
                    out.write(ESC_FONTA);
                }else{
                    out.write(ESC_FONT_B);
                }
                out.write(str.getBytes());
            }else{
                out.write(ESC_ALIGN_LEFT);
                out.write(FS_FONT_ALIGN);
                out.write(ESC_CN_FONT);
                out.write(ESC_CANCEL_BOLD);
                if("1".equals(printType)){
                    out.write(ESC_FONTA);
                }else{
                    out.write(ESC_FONT_B);
                }
                out.write(str.getBytes());
            }
            out.write("\n".getBytes());
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 标准粗体字体打印一行
     * @param str 需打印的字符串
     * @param align 打印的位置 LEFT/CENTER/RIGHT 其他为默认居左打印
     */
    public void standardBoldPrinterLine(String str, String align){
        try{
            if(CENTER.equals(align)){
                out.write(ESC_ALIGN_CENTER);
                out.write(FS_FONT_ALIGN);
                out.write(ESC_CN_FONT);
                out.write(ESC_SETTING_BOLD);
                if("1".equals(printType)){
                    out.write(ESC_FONTA);
                }else{
                    out.write(ESC_FONT_B);
                }
                out.write(str.getBytes());
            }else if(RIGHT.equals(align)){
                out.write(ESC_ALIGN_RIGHT);
                out.write(FS_FONT_ALIGN);
                out.write(ESC_CN_FONT);
                out.write(ESC_SETTING_BOLD);
                if("1".equals(printType)){
                    out.write(ESC_FONTA);
                }else{
                    out.write(ESC_FONT_B);
                }
                out.write(str.getBytes());
            }else{
                out.write(ESC_ALIGN_LEFT);
                out.write(FS_FONT_ALIGN);
                out.write(ESC_CN_FONT);
                out.write(ESC_SETTING_BOLD);
                if("1".equals(printType)){
                    out.write(ESC_FONTA);
                }else{
                    out.write(ESC_FONT_B);
                }
                out.write(str.getBytes());
            }
            out.write("\n".getBytes());
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 双倍宽字体按行打印
     * @param str
     * @param align
     */
    public void largeSizePrinterLine(String str, String align){
        try{
            if(CENTER.equals(align)){
                out.write(ESC_ALIGN_CENTER);
                out.write(FS_FONT_ALIGN_DOUBLE);
                out.write(str.getBytes());
            }else if(RIGHT.equals(align)){
                out.write(ESC_ALIGN_RIGHT);
                out.write(FS_FONT_ALIGN_DOUBLE);
                out.write(str.getBytes());
            }else{
                out.write(ESC_ALIGN_LEFT);
                out.write(FS_FONT_ALIGN_DOUBLE);
                out.write(str.getBytes());
            }
            out.write("\n".getBytes());
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 双倍高字体按行打印
     * @param str
     * @param align
     */
    public void largeHSizePrinterLine(String str, String align){
        try{
            if(CENTER.equals(align)){
                out.write(ESC_ALIGN_CENTER);
                out.write(FS_FONT_VERTICAL_DOUBLE);
                out.write(str.getBytes());
            }else if(RIGHT.equals(align)){
                out.write(ESC_ALIGN_RIGHT);
                out.write(FS_FONT_VERTICAL_DOUBLE);
                out.write(str.getBytes());
            }else{
                out.write(ESC_ALIGN_LEFT);
                out.write(FS_FONT_VERTICAL_DOUBLE);
                out.write(str.getBytes());
            }
            out.write("\n".getBytes());
        }catch(IOException e) {
            e.printStackTrace();
        }
    }    /**
     * 大号字体红色按行打印
     * @param str
     * @param align
     */
    public void largeSizeRedPrinterLine(String str, String align){
        try{
            if(CENTER.equals(align)){
                out.write(ESC_ALIGN_CENTER);
                out.write(FS_FONT_ALIGN_DOUBLE);
                out.write(ESC_FONT_COLOR_RED);
                out.write(str.getBytes());
            }else if(RIGHT.equals(align)){
                out.write(ESC_ALIGN_RIGHT);
                out.write(FS_FONT_ALIGN_DOUBLE);
                out.write(ESC_FONT_COLOR_RED);
                out.write(str.getBytes());
            }else{
                out.write(ESC_ALIGN_LEFT);
                out.write(FS_FONT_ALIGN_DOUBLE);
                out.write(ESC_FONT_COLOR_RED);
                out.write(str.getBytes());
            }
            out.write("\n".getBytes());
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
    public void openDrawer(){
        try {
            out.write(ESC_OPEN_DRAWER);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public String makePrintString(int lineChars, String txt1, String txt2){
        if(txt1 == null){
            txt1 = "";
        }
        if(txt2 == null){
            txt2 = "";
        }
        int spaces = 0;
        String tab = "";
        try{
            spaces = lineChars - (txt1.getBytes().length + txt2.getBytes().length);
            for (int j = 0 ; j < spaces ; j++){
                tab += " ";
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return txt1 + tab + txt2;
    }
    public String makePrintString(int lineChars, String txt1, String txt2, String txt3){
        if(txt1 == null){
            txt1 = "";
        }
        if(txt2 == null){
            txt2 = "";
        }
        if(txt3 == null){
            txt3 = "";
        }
        int spaces = 0;
        int lineChars1 = lineChars*2/3;
        String tab = "";
        String returnStr = txt1;
        try{
            spaces = lineChars1 - (returnStr.getBytes().length + txt2.getBytes().length);
            for (int j = 0 ; j < spaces ; j++){
                tab += " ";
            }
            returnStr = txt1 + tab + txt2;
            spaces = lineChars - (returnStr.getBytes().length + txt3.getBytes().length);
            tab = "";
            for (int j = 0 ; j < spaces ; j++){
                tab += " ";
            }
            returnStr = returnStr + tab + txt3;
        }catch(Exception e){
            e.printStackTrace();
        }
        return returnStr;
    }
}

android java epson串口打印机的更多相关文章

  1. Java jacob调用打印机打印word文档

    前面说了Java如何生成复杂的Word文档,今年记录下Java如何调用打印机打印word文档. 起初用的是自带的PrintJob,但是系统提供的打印机制并不成熟完整.网上的代码也是千篇一律,在我的打印 ...

  2. java实现串口通讯

    一. 准备工作 1. 点击此下载java串口通讯相关工具 2. 将RXTXcomm.jar放到  %JAVA_HOME%\jre\lib\ext\  目录下,工程中引入该jar包 3. 将rxtxSe ...

  3. .NET/android/java/iOS AES通用加密解密(修正安卓)

    移动端越来越火了,我们在开发过程中,总会碰到要和移动端打交道的场景,比如.NET和android或者iOS的打交道.为了让数据交互更安全,我们需要对数据进行加密传输.今天研究了一下,把几种语言的加密都 ...

  4. Android java传递int类型数组给C

    接着前面的文章<Android java传递int类型数据给C><Android java传递string类型数据给C>,继续实践 实现public native int[] ...

  5. Android java传递string类型数据给C

    本文接着实现<Android java传递int类型数据给C>的还未实现的方法: public native String sayHelloInC(String s); 先贴一个工具方法, ...

  6. .NET/android/java/iOS AES通用加密解密

    移动端越来越火了,我们在开发过程中,总会碰到要和移动端打交道的场景,比如.NET和android或者iOS的打交道.为了让数据交互更安全,我们需要对数据进行加密传输.今天研究了一下,把几种语言的加密都 ...

  7. android Java BASE64编码和解码二:图片的编码和解码

    1.准备工作 (1)在项目中集成 Base64 代码,集成方法见第一篇博文:android Java BASE64编码和解码一:基础 (2)添加 ImgHelper 工具类 package com.a ...

  8. 用Java通过串口发送手机短信

    用Java通过串口发短信其实很简单,因为有现成的类库供我们使用.有底层的类库,也有封装好一点的类库,下面我介绍一下在 Win32 平台下发送短信的方法. 如果你想用更底层的类库开发功能更强大的应用程序 ...

  9. CC++初学者编程教程(10) 搭建Android java C/C++ NDK QTforAndroid 开发环境

    1 安装JDK 2 点下一步 3 点下一步 4 开始安装 5 定制路径,点下一步 6 开始安装 7 安装完成, 8 解压缩 9 启动eclipse 10 看到启动画面 11 设置工作文件夹 12 单击 ...

随机推荐

  1. 入门常用SQL及官方文档的使用

    SQL语句基础理论 SQL是操作和检索关系型数据库的标准语言,标准SQL语句可用于操作关系型数据库. 5大主要类型: ①DQL(Data Query Language,数据查询语言)语句,主要由于se ...

  2. 04python while循环语句

    使用while ture语法 luck_num = 33 flag = True while flag: guess_num = input('请输入您猜测的年龄:') if guess_num &l ...

  3. CM和CDH的安装-进阶完成

    安装Cloudera Manager Server 和Agent 1.在cdh1解压cloudera-manager-el6-cm5.9.0_x86_64.tar.gz(cdh1节点)tar -zcv ...

  4. HTML 引用

    关于 HTML 引用: (1) <q> 和 <blockquote> 用于实现长短不一的引用语(2) <q> 用于短的引用,<blockquote> 用 ...

  5. 解决“”父级标签和子标签边框重叠,设置子标签的margin父标签会跟着移动“”的方法

    1.可以给父标签一个padding,然后给一个很小的值,虽然不影响整体但是不建议使用 2.给父标签一个"over:hidden"的样式,推荐使用

  6. 普通for循环和增强for循环的区别

    1.普通for循环:自行维护循环次数,循环体自行维护获取元素的方法: int[] array = new int[]{1,2,3,4,5}; //int[] array ={1,2,3,4,5} ; ...

  7. 删除SQL注入的一些方法总结

    sql替换法: ); set @myStr='oa_20121026new.bak</title><style>.alx2{position:absolute;clip:rec ...

  8. <转>机器学习系列(9)_机器学习算法一览(附Python和R代码)

    转自http://blog.csdn.net/han_xiaoyang/article/details/51191386 – 谷歌的无人车和机器人得到了很多关注,但我们真正的未来却在于能够使电脑变得更 ...

  9. Centos重新启动网络配置文件,/etc/resolv.conf被覆盖或清空问题解决

    Centos在执行命令 yum update时报错如下: Could not get metalink https://mirrors.fedoraproject.org/metalink?repo= ...

  10. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验八:PS/2模块② — 键盘与组合键

    实验八:PS/2模块② — 键盘与组合键 实验七之际,我们学习如何读取PS/2键盘发送过来的通码与断码,不过实验内容也是一键按下然后释放,简单按键行为而已.然而,实验八的实验内容却是学习组合键的按键行 ...