PrinterEscCmd.java 21.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
package com.eztlib;

import java.util.Hashtable;
import com.google.zxing.BarcodeFormat; //zxingcore-2.3.0.jar
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;

public class PrinterEscCmd
{
    // 字体大小
    public enum LOAD_ID
    {
        FONT_12((byte)0x00),
        FONT_16((byte)0x01),
        FONT_24((byte)0x02),
        FONT5x7((byte)0x03),
        FONT7x8((byte)0x04);

        private final byte value;
        private LOAD_ID(byte value)
        {
            this.value = value;
        }

        public byte getValue() {
            return value;
        }
    }

    // 字符旋转角度
    public enum ALIGN_MODE
    {
        ALIGN_Lift((byte)0x00),   //居左
        ALIGN_Mid((byte)0x01),    //居中
        ALIGN_Right((byte)0x02);  //居右

        private final byte value;
        private ALIGN_MODE(byte value)
        {
            this.value = value;
        }
        public byte getValue() {
            return value;
        }
    }


    // 字符旋转角度
    public enum ROTATE_ANGLE
    {
        ROTATE_0((byte)0x00),
        ROTATE_90((byte)0x01),
        ROTATE_180((byte)0x02),
        ROTATE_270((byte)0x03);

        private final byte value;
        private ROTATE_ANGLE(byte value)
        {
            this.value = value;
        }
        public byte getValue() {
            return value;
        }
    }


    // 字符放大倍数
    public enum ENLARGE_MODE
    {
        ENLARGE_N((byte)0x00),           //不放大
        ENLARGE_1((byte)0x01),           //放大1倍
        ENLARGE_2((byte)0x02),           //放大2倍
        ENLARGE_3((byte)0x03);           //放大3倍

        private final byte value;
        private ENLARGE_MODE(byte value)
        {
            this.value = value;
        }
        public byte getValue() {
            return value;
        }
    }

    // 条码字符的打印位置
    public enum Bar1DChar_ADDR
    {
        ADDR_N((byte)0x00),           //不打印字符
        ADDR_UP((byte)0x01),          //条码的上方
        ADDR_DOWN((byte)0x02);        //条码的下方

        private final byte value;
        private Bar1DChar_ADDR(byte value)
        {
            this.value = value;
        }
        public byte getValue() {
            return value;
        }
    }

    //一维条码类型
    public enum Bar1DCode_TYPE
    {
        UPCA((byte)0x00),
        UPCE((byte)0x01),
        EAN8((byte)0x02),
        EAN13((byte)0x03),
        ITF25((byte)0x04),
        CODEBAR((byte)0x05),
        CODE93((byte)0x06),
        CODE39((byte)0x07);

        private final byte value;
        private Bar1DCode_TYPE(byte value)
        {
            this.value = value;
        }
        public byte getValue() {
            return value;
        }
    }

    //二维条码类型
    public enum Bar2DCode_TYPE
    {
        QRcode((byte)0x00);

        private final byte value;
        private Bar2DCode_TYPE(byte value)
        {
            this.value = value;
        }
        public byte getValue() {
            return value;
        }
    }


    //切刀方式
    public enum Cuter_TYPE
    {
        FullCut((byte)0x41), //全切
        HalfCut((byte)0x42); //半切
        private final byte value;
        private Cuter_TYPE(byte value)
        {
            this.value = value;
        }
        public byte getValue() {
            return value;
        }
    }


    static
    {
        try {
            System.loadLibrary("EZTLIB");
        } catch (Exception e) {
            e.printStackTrace();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    /// 函数功能:清空ESC指令缓存
    public native void escBufClear();


    /// 函数功能:“打印字符串”指令
    /// <param name="Text">需要打印的文本字符串</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escPrintText(String Text);


    /// <summary>
    /// 函数功能:“初始化打印机”指令
    /// 打印机初始化(清除打印缓存、各参数恢复默认值)
    /// ESC @	,1B 40	,打印机初始化
    /// </summary>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escInit();

    /// 函数功能:“打印并走纸到黑标/标签位置”指令
    /// ESC FF	,1B 0C	,打印并走纸到黑标/标签位置
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escLabelStart();


    /// 函数功能:“打印回车”指令
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escEnter();

    /// 函数功能:“字符字体大小设置”指令
    /// ESC M n	,1B 4D n	,设置字体大小
    /// <param name="mID">字体大小ID号</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escCharFont(byte mID);

    /// 函数功能:打印缓存内容并走纸
    /// ESC d n	,1B 64 n	,打印并进纸 n 行
    /// ESC J n	,1B 4A n	,打印并进纸 n 点
    /// <param name="LineFlag">走纸距离计量单位标志,LineFlag = true时,以行为单位;LineFlag = flase时,以点为单位,1点= 0.125mm</param>
    /// <param name="Param">走纸Param各单位长度</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escFeedPaper(boolean LineFlag, byte Param);

    /// 函数功能:“字符横向放大”指令
    /// ESC U n	,1B 55 n	,横向放大n倍
    /// <param name="mode">放大倍数</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escWEnlarge(byte mode);

    /// 函数功能:“字符纵向放大”指令
    /// //ESC V n	,1B 56 n	,纵向放大n倍
    /// <param name="mode">放大倍数</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escHEnlarge(byte mode);

    /// 函数功能:“字符横向纵向放大”指令
    /// //ESC W n	,1B 57 n	,横向纵向放大n倍
    /// <param name="mode">放大倍数</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escWHEnlarge(byte mode);

    /// 函数功能:“设置行间距”指令
    /// ESC 3 n	,1B 33 n	,设置行间距
    /// <param name="Param">行间距参数值,以点为单位,1点=0.125mm</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escLineSpace(byte Param);

    /// 函数功能:“设置字间距”指令
    /// ESC SP n	,1B 20 n	,设置字间距
    /// <param name="Param">字符间距参数值,以点为单位,1点=0.125mm</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escCharSpace(byte Param);

    /// 函数功能:“设置左边距”指令
    /// ESC l n	,1B 6C n	,设置左边距
    /// <param name="Param">左边距参数值</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escLeftMargin(byte Param);

    /// 函数功能:“设置右边距”指令
    /// ESC Q n	,1B 51 n	,设置右边距
    /// <param name="Param">右边距参数值</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escRightMargin(byte Param);

    /// 函数功能:打印空格或空行
    /// ESC f m n  ,1B 66 m n  ,打印空格或空行,M=1打印 n行空行,M=0打印n个空格
    /// <param name="LineFlag">LineFlag = true时,以行为单位;LineFlag = flase时,以一个ASCII字符宽度为单位</param>
    /// <param name="Param">打印Param各单位长度</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escBlank(boolean LineFlag,byte Param);

    /// 函数功能:“设置打印绝对位置”指令
    /// ESC $ nL nH ,1B 24 nL nH ,设置打印绝对位置
    /// <param name="Position">绝对位置</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escposition(int Position);


    /// 函数功能:“执行水平制表”指令
    /// HT ,09 ,执行水平制表
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escmaketab();


    /// 函数功能:“设置水平制表位置”指令
    /// ESC D n1…nk NUL ,1B 44 n1…nk NUL ,设置水平制表位置
    /// <param name="TabList">水平制表位置</param>
    /// <param name="ListCt">水平制表个数</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escpositiontab(byte[] TabList,int ListCt);


    /// 函数功能:“字符上划线设置”指令
    /// ESC + n	,1B 2B n	,设置/取消上划线打印
    /// <param name="verline">overline = true, 字符加上划线,overline = false字符无上划线</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escOverline(boolean overline);


    /// 函数功能:“字符下划线设置”指令
    /// ESC - n	,1B 2D n	,设置/取消下划线打印
    /// <param name="underline">underline = true, 字符加下划线,underline = false字符无下划线</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escUnderline(boolean underline);


    /// 函数功能:“字符反白打印设置”指令
    /// ESC i n	,1B 69 n	,设置/取消反白打印
    /// <param name="white">white = true,反白打印,white = false反白打印</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escWhite(boolean white);

    /// 函数功能:“字符斜体打印设置”指令
    /// ESC x n	,1B 78 n	,设置/取消斜体打印
    /// <param name="italic">italic = true,斜体打印,italic = false斜体打印</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escItalic(boolean italic);

    /// 函数功能:“字符粗体设置”指令
    /// ESC E n	,1B 45 n	,设置/取消粗体打印
    /// <param name="bold">bold = true,粗体打印,bold = false正常打印</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escBold(boolean bold);

    /// 函数功能:“设置打印对齐方式”指令
    /// ESC a n	,1B 61 n	,设置打印对齐方式(居左、居中、居右)
    /// <param name="mAlign">对齐方式</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escAlign(byte mAlign);


    /// 函数功能:“设置打印灰度”指令
    /// ESC g n	,1B 67 n	,设置打印灰度
    /// <param name="Param">灰度等级 0-9</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escGray(byte mgray);



    /// 函数功能:“设置打印速度”指令
    /// ESC m n	,1B 6D n	,设置打印速度
    /// <param name="Param">n =0 变速 ,n =1 匀速</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escSpeed(byte mspeed);


    /// 函数功能:“设置字符旋转角度方式”指令
    /// FC I n	,1C 49 n	,设置/取消字符旋转
    /// <param name="Angle">字符旋转角度, Angle=0 旋转0度,Angle=1 旋转90度,Angle=2 旋转180度,Angle=3 旋转270度</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escRotate(byte Angle);

    /// 函数功能:“设置条形码宽度”指令
    /// GS w n	,1D 77 n      ,设置条码单元宽度为 n 点,1<=n<=4
    /// <param name="Param">条码单元宽度,以点为单位,1点=0.125mm</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean esc1DBarWidth(byte Param);


    /// 函数功能:“设置条形码高度”指令
    /// GS h n,   1D 68 n     ,设置条码高度为 n 点,1<=n<=255
    /// <param name="Param">条码条码高度,以点为单位,1点=0.125mm</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean esc1DBarHigh(byte Param);


    /// 函数功能:“设置条码字符的打印位置”指令
    /// GS H n,    1D 48 n    ,设置条码字符的打印位置 ,不打印 条码的上方 条码的下方
    /// <param name="mAddr">打印位置</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean esc1DBarCharAddr(byte mAddr);

    /// 设置公司名称
    /// <param name="Text"></param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escSetCompanyName(String Text);

    /// 设置产品型号
    /// <param name="Text"></param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escSetProductModel(String Text);


    /// 读取设备序列号
    /// <param name="mUID">返回设备序列号</param>
    /// <returns>返回设备序列号字节长度</returns>
    public native int escGetProductID(byte[] mUID);

    /// 设置休眠时间
    /// <param name="time">设置time分钟后机器休眠,0<=time<=255 (休眠时间=0且关机时间=0 则机器为不休眠不关机 )</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escSetPowerIdleTime(byte time);

    /// 设置关机时间
    /// <param name="time">设置time分钟后机器关机,0<=time<=255 (休眠时间=0且关机时间=0 则机器为不休眠不关机 )</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escSetPowerOffTime(byte time);

    /// 读取检测纸ADC值
    /// <returns>返回检测纸ADC值</returns>
    public native int escGetPaperCheckADC();

    /// 设置黑标检测ADC值
    /// <param name="value">黑标检测ADC值</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escSetHeiBiaoADC(int value);

    /// 设置蓝牙名称
    /// <param name="Text"></param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escSetBluName(String Text);

    /// 设置蓝牙密码
    /// <param name="Text"></param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escSetBluKey(String Text);

    /// 设置串口波特率
    /// <param name="baud"></param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escSetUartBaud(byte baud);

    /// 设置蓝牙波特率
    /// <param name="baud"></param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escSetBluBaud(byte baud);


    /// 读取打印机状态
    /// <returns>返回打印机状态</returns>
    public native byte escGetState();


    /// 函数功能:走纸并切纸
    /// GS V m n ,1D 56 m n	,走纸 n 点并切纸
    /// <param name="mType">切刀类型,mType=0x41全切,mType=0x42半切</param>
    /// <param name="Param">走纸Param各单位长度,以点为单位,1点= 0.125mm</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escCuter(byte mType,byte Param);


    /// 打印图片
    /// <param name="mType">单元宽度</param>
    /// <param name="pValue">图片数据</param>
    /// <param name="Width">图片宽度</param>
    /// <param name="Height">图片高度</param>
    /// <returns>返回true表示成功,返回false表示失败</returns>
    public native boolean escPicturePrint(byte mType,byte[][] pValue,int Width,int Height);


    /// 函数功能:“打印一维条码”指令
    /// GS k m n d1…dn, 1D 6B m n d1…dn	打印一维条码  ,m为条码类型 n为条码数据长度 d1…dn为条码数据
    /// <param name="Type">一维条码类型</param>
    /// <param name="Text">一维条码数据</param>
    /// <returns>返回空表示成功,返回其他数据表示失败</returns>
    public native String esc1DBarPrint(byte Type, String Text);

    /// <summary>
    /// 函数功能:打印二维码
    /// </summary>
    /// <param name="mType">二维条码类型</param>
    /// <param name="mText">二维条码数据</param>
    /// <param name="msize">二维条码大小</param>
    /// <returns>返回空表示成功,返回其他数据表示失败</returns>
    public String esc2DBarPrint(Bar2DCode_TYPE mType,String mText,int msize)
    {
        byte[][] graydata=null;
        switch (mType)
        {
            case QRcode:
                Bitmap bitmap =createQRCode(mText,msize);

                if(bitmap==null){return "QRcode生成失败";}
                graydata=ConvertImageTo2byte(bitmap,(byte)127);//提取图片二值化数据
                break;
            default: break;
        }

        if(graydata==null){return "没有生成二维码数据";}

        int Width=graydata.length;
        int Height = graydata[0].length;
        if(escPicturePrint((byte)0,graydata,Width,Height)==false)
        {
            return "数据写入失败";
        }

        return "";
    }



    public Bitmap createQRCode(String str,int msize)
    {
        try
        {
            //判断URL合法性
            if (str == null || "".equals(str) || str.length() < 1 || msize<10)
            {
                return null;
            }
            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            //图像数据转换,使用了矩阵转换
            BitMatrix bitMatrix = new QRCodeWriter().encode(str, BarcodeFormat.QR_CODE, msize, msize, hints);
            int[] pixels = new int[msize * msize];
            //下面这里按照二维码的算法,逐个生成二维码的图片,
            //两个for循环是图片横列扫描的结果
            for (int y = 0; y < msize; y++)
            {
                for (int x = 0; x < msize; x++)
                {
                    if (bitMatrix.get(x, y))
                    {
                        pixels[y * msize + x] = 0xff000000;
                    }
                    else
                    {
                        pixels[y * msize + x] = 0xffffffff;
                    }
                }
            }
            //生成二维码图片的格式,使用ARGB_8888
            Bitmap bitmap = Bitmap.createBitmap(msize, msize, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, msize, 0, 0, msize, msize);

            return bitmap;
        }
        catch (WriterException e)
        {
            e.printStackTrace();
        }
        return null;
    }


    //获取取第index位
    public int GetBit(byte b, int index) { return ((b & (1 << index)) > 0) ? 1 : 0; }
    //将第index位设为1
    public byte SetBit(byte b, int index) { return (byte)(b | (1 << index)); }
    //将第index位设为0
    public byte ClearBit(byte b, int index) { return (byte)(b & (0xff - (1 << index))); }
    //将第index位取反
    public byte ReverseBit(byte b, int index) { return (byte)(b ^ (byte)(1 << index)); }

    /// <summary>
    /// 图像二值化
    /// </summary>
    /// <param name="bmp"></param>
    /// <param name="cmpvalue"></param>
    /// <returns></returns>
    public byte[][] ConvertImageTo2byte(Bitmap bmp,byte cmpvalue ) {
        int width = bmp.getWidth();      //获取位图的宽
        int height = bmp.getHeight();    //获取位图的高

        int wbyte = width / 8;
        if ((width % 8) != 0) { wbyte++; }
        byte[][] svvalue = new byte[wbyte][height];

        int []pixels = new int[width * height];  //通过位图的大小创建像素点数组

        bmp.getPixels(pixels, 0, width, 0, 0, width, height);

        for(int y = 0; y < height; y++)  {
            for(int x = 0; x < width; x++) {
                int grey = pixels[width * y + x];

                int red = ((grey    & 0x00FF0000 ) >> 16);
                int green = ((grey & 0x0000FF00) >> 8);
                int blue = (grey & 0x000000FF);

                grey = (int)((float) red * 0.3 + (float)green * 0.59 + (float)blue * 0.11);
                if(grey<cmpvalue)
                {svvalue[x / 8][y] = SetBit(svvalue[x / 8][y], 7 - x % 8);}

            }
        }
        return svvalue;
    }


    public Bitmap convertImageTo2Bmp(Bitmap bmp,byte cmpvalue ) {
        int width = bmp.getWidth();      //获取位图的宽
        int height = bmp.getHeight();    //获取位图的高

        int []pixels = new int[width * height];  //通过位图的大小创建像素点数组

        bmp.getPixels(pixels, 0, width, 0, 0, width, height);

        for(int y = 0; y < height; y++)  {
            for(int x = 0; x < width; x++) {
                int grey = pixels[width * y + x];

                int red = ((grey    & 0x00FF0000 ) >> 16);
                int green = ((grey & 0x0000FF00) >> 8);
                int blue = (grey & 0x000000FF);

                grey = (int)((float) red * 0.3 + (float)green * 0.59 + (float)blue * 0.11);
                if(grey<cmpvalue){grey=0x00000000;}
                else{grey=0x00FFFFFF;}
                pixels[width * y + x] = grey;
            }
        }
        Bitmap result = Bitmap.createBitmap(width, height, Config.RGB_565);
        result.setPixels(pixels, 0, width, 0, 0, width, height);
        return result;
    }
}