健康一贴灵,专注医药行业管理信息化

Android打印机--TSC 标签打印(转)

原文:https://blog.csdn.net/johnWcheung/article/details/72123858

打印机按照连接方式分为USB打印机、蓝牙打印机、网络打印机、云打印机、内联打印机;按照打印纸张大小分为带切刀的80厨房打印机和58热敏票据打印机;按照打印结果分为小票打印机和标签打印机;关于小票打印,一般都是使用ESC/POS打印指令,可以参见Android打印机–小票打印格式及模板设置,本篇主要介绍使用TSC标签打印指令实现打印标签功能。

首先是将打印指令作一下封装

/**
 * TSC 标签打印指令
 * Created by john on 17-5-12.
 */

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

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

public LabelCommand(int width, int height, int gap) {
this.Command = new Vector(4096, 1024);
this.addSize(width, height);
this.addGap(gap);
}

public void clrCommand() {
this.Command.clear();
}

private void addStrToCommand(String str) {
byte[] bs = null;
if(!str.equals("")) {
try {
bs = str.getBytes("GB2312");
} catch (UnsupportedEncodingException var4) {
var4.printStackTrace();
}

for(int i = 0; i < bs.length; ++i) {
this.Command.add(Byte.valueOf(bs[i]));
}
}

}

public void addGap(int gap) {
new String();
String str = "GAP " + gap + " mm," + 0 + " mm" + "\r\n";
this.addStrToCommand(str);
}

public void addSize(int width, int height) {
new String();
String str = "SIZE " + width + " mm," + height + " mm" + "\r\n";
this.addStrToCommand(str);
}

public void addCashdrwer(LabelCommand.FOOT m, int t1, int t2) {
new String();
String str = "CASHDRAWER " + m.getValue() + "," + t1 + "," + t2 + "\r\n";
this.addStrToCommand(str);
}

public void addOffset(int offset) {
new String();
String str = "OFFSET " + offset + " mm" + "\r\n";
this.addStrToCommand(str);
}

public void addSpeed(LabelCommand.SPEED speed) {
new String();
String str = "SPEED " + speed.getValue() + "\r\n";
this.addStrToCommand(str);
}

public void addDensity(LabelCommand.DENSITY density) {
new String();
String str = "DENSITY " + density.getValue() + "\r\n";
this.addStrToCommand(str);
}

public void addDirection(LabelCommand.DIRECTION direction, LabelCommand.MIRROR mirror) {
new String();
String str = "DIRECTION " + direction.getValue() + ',' + mirror.getValue() + "\r\n";
this.addStrToCommand(str);
}

public void addReference(int x, int y) {
new String();
String str = "REFERENCE " + x + "," + y + "\r\n";
this.addStrToCommand(str);
}

public void addShif(int shift) {
new String();
String str = "SHIFT " + shift + "\r\n";
this.addStrToCommand(str);
}

public void addCls() {
new String();
String str = "CLS\r\n";
this.addStrToCommand(str);
}

public void addFeed(int dot) {
new String();
String str = "FEED " + dot + "\r\n";
this.addStrToCommand(str);
}

public void addBackFeed(int dot) {
new String();
String str = "BACKFEED " + dot + "\r\n";
this.addStrToCommand(str);
}

public void addFormFeed() {
new String();
String str = "FORMFEED\r\n";
this.addStrToCommand(str);
}

public void addHome() {
new String();
String str = "HOME\r\n";
this.addStrToCommand(str);
}

public void addPrint(int m, int n) {
new String();
String str = "PRINT " + m + "," + n + "\r\n";
this.addStrToCommand(str);
}

public void addPrint(int m) {
new String();
String str = "PRINT " + m + "\r\n";
this.addStrToCommand(str);
}

public void addCodePage(LabelCommand.CODEPAGE page) {
new String();
String str = "CODEPAGE " + page.getValue() + "\r\n";
this.addStrToCommand(str);
}

public void addSound(int level, int interval) {
new String();
String str = "SOUND " + level + "," + interval + "\r\n";
this.addStrToCommand(str);
}

public void addLimitFeed(int n) {
new String();
String str = "LIMITFEED " + n + "\r\n";
this.addStrToCommand(str);
}

public void addSelfTest() {
new String();
String str = "SELFTEST\r\n";
this.addStrToCommand(str);
}

public void addBar(int x, int y, int width, int height) {
new String();
String str = "BAR " + x + "," + y + "," + width + "," + height + "\r\n";
this.addStrToCommand(str);
}

public void addText(int x, int y, LabelCommand.FONTTYPE font, LabelCommand.ROTATION rotation, LabelCommand.FONTMUL Xscal, LabelCommand.FONTMUL Yscal, String text) {
new String();
String str = "TEXT " + x + "," + y + "," + """ + font.getValue() + """ + "," + rotation.getValue() + "," + Xscal.getValue() + "," + Yscal.getValue() + "," + """ + text + """ + "\r\n";
this.addStrToCommand(str);
}

public void add1DBarcode(int x, int y, LabelCommand.BARCODETYPE type, int height, LabelCommand.READABEL readable, LabelCommand.ROTATION rotation, String content) {
byte narrow = 2;
byte width = 2;
new String();
String str = "BARCODE " + x + "," + y + "," + """ + type.getValue() + """ + "," + height + "," + readable.getValue() + "," + rotation.getValue() + "," + narrow + "," + width + "," + """ + content + """ + "\r\n";
this.addStrToCommand(str);
}

public void add1DBarcode(int x, int y, LabelCommand.BARCODETYPE type, int height, LabelCommand.READABEL readable, LabelCommand.ROTATION rotation, int narrow, int width, String content) {
String str = "BARCODE " + x + "," + y + "," + """ + type.getValue() + """ + "," + height + "," + readable.getValue() + "," + rotation.getValue() + "," + narrow + "," + width + "," + """ + content + """ + "\r\n";
this.addStrToCommand(str);
}

public void addBox(int x, int y, int xend, int yend, int thickness) {
new String();
String str = "BOX " + x + "," + y + "," + xend + "," + yend + "," + thickness + "\r\n";
this.addStrToCommand(str);
}

public void addBitmap(int x, int y, LabelCommand.BITMAP_MODE mode, int nWidth, Bitmap b) {
if(b != null) {
int width = (nWidth + 7) / 8 8;
int height = b.getHeight()
width / b.getWidth();
Log.d("BMP", "bmp.getWidth() " + b.getWidth());
Bitmap grayBitmap = LabelUtils.toGrayscale(b);
Bitmap rszBitmap = LabelUtils.resizeImage(grayBitmap, width, height);
byte[] src = LabelUtils.bitmapToBWPix(rszBitmap);
height = src.length / width;
width /= 8;
String str = "BITMAP " + x + "," + y + "," + width + "," + height + "," + mode.getValue() + ",";
this.addStrToCommand(str);
byte[] codecontent = LabelUtils.pixToLabelCmd(src);

for(int k = 0; k < codecontent.length; ++k) {
this.Command.add(Byte.valueOf(codecontent[k]));
}

Log.d("LabelCommand", "codecontent" + codecontent);
}

}

public void addErase(int x, int y, int xwidth, int yheight) {
new String();
String str = "ERASE " + x + "," + y + "," + xwidth + "," + yheight + "\r\n";
this.addStrToCommand(str);
}

public void addReverse(int x, int y, int xwidth, int yheight) {
new String();
String str = "REVERSE " + x + "," + y + "," + xwidth + "," + yheight + "\r\n";
this.addStrToCommand(str);
}

public void addQRCode(int x, int y, LabelCommand.EEC level, int cellwidth, LabelCommand.ROTATION rotation, String data) {
new String();
String str = "QRCODE " + x + "," + y + "," + level.getValue() + "," + cellwidth + "," + 'A' + "," + rotation.getValue() + "," + """ + data + """ + "\r\n";
this.addStrToCommand(str);
}

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

public void addQueryPrinterType() {
new String();
String str = "~!T\r\n";
this.addStrToCommand(str);
}

public void addQueryPrinterStatus() {
this.Command.add(Byte.valueOf((byte)27));
this.Command.add(Byte.valueOf((byte)33));
this.Command.add(Byte.valueOf((byte)63));
}

public void addResetPrinter() {
this.Command.add(Byte.valueOf((byte)27));
this.Command.add(Byte.valueOf((byte)33));
this.Command.add(Byte.valueOf((byte)82));
}

public void addQueryPrinterLife() {
new String();
String str = "~!@\r\n";
this.addStrToCommand(str);
}

public void addQueryPrinterMemory() {
new String();
String str = "~!A\r\n";
this.addStrToCommand(str);
}

public void addQueryPrinterFile() {
new String();
String str = "~!F\r\n";
this.addStrToCommand(str);
}

public void addQueryPrinterCodePage() {
new String();
String str = "~!I\r\n";
this.addStrToCommand(str);
}

public void addPeel(ENABLE enable) {
String str = new String();
if(enable.getValue() == 0) {
str = "SET PEEL " + enable.getValue() + "\r\n";
}

this.addStrToCommand(str);
}

public void addTear(ENABLE enable) {
new String();
String str = "SET TEAR " + enable.getValue() + "\r\n";
this.addStrToCommand(str);
}

public void addCutter(ENABLE enable) {
new String();
String str = "SET CUTTER " + enable.getValue() + "\r\n";
this.addStrToCommand(str);
}

public void addCutterBatch() {
String str = "SET CUTTER BATCH\r\n";
this.addStrToCommand(str);
}

public void addCutterPieces(short number) {
String str = "SET CUTTER " + number + "\r\n";
this.addStrToCommand(str);
}

public void addReprint(ENABLE enable) {
new String();
String str = "SET REPRINT " + enable.getValue() + "\r\n";
this.addStrToCommand(str);
}

public void addPrintKey(ENABLE enable) {
new String();
String str = "SET PRINTKEY " + enable.getValue() + "\r\n";
this.addStrToCommand(str);
}

public void addPrintKey(int m) {
new String();
String str = "SET PRINTKEY " + m + "\r\n";
this.addStrToCommand(str);
}

public void addPartialCutter(ENABLE enable) {
new String();
String str = "SET PARTIAL_CUTTER " + enable.getValue() + "\r\n";
this.addStrToCommand(str);
}

public void addUserCommand(String command) {
this.addStrToCommand(command);
}

public static enum BARCODETYPE {
CODE128("128"),
CODE128M("128M"),
EAN128("EAN128"),
ITF25("25"),
ITF25C("25C"),
CODE39("39"),
CODE39C("39C"),
CODE39S("39S"),
CODE93("93"),
EAN13("EAN13"),
EAN13_2("EAN13+2"),
EAN13_5("EAN13+5"),
EAN8("EAN8"),
EAN8_2("EAN8+2"),
EAN8_5("EAN8+5"),
CODABAR("CODA"),
POST("POST"),
UPCA("UPCA"),
UPCA_2("UPCA+2"),
UPCA_5("UPCA+5"),
UPCE("UPCE13"),
UPCE_2("UPCE13+2"),
UPCE_5("UPCE13+5"),
CPOST("CPOST"),
MSI("MSI"),
MSIC("MSIC"),
PLESSEY("PLESSEY"),
ITF14("ITF14"),
EAN14("EAN14");

private final String value;

private BARCODETYPE(String value) {
this.value = value;
}

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

public static enum BITMAP_MODE {
OVERWRITE(0),
OR(1),
XOR(2);

private final int value;

private BITMAP_MODE(int value) {
this.value = value;
}

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

public static enum CODEPAGE {
PC437(437),
PC850(850),
PC852(852),
PC860(860),
PC863(863),
PC865(865),
WPC1250(1250),
WPC1252(1252),
WPC1253(1253),
WPC1254(1254);

private final int value;

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

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

public static enum DENSITY {
DNESITY0(0),
DNESITY1(1),
DNESITY2(2),
DNESITY3(3),
DNESITY4(4),
DNESITY5(5),
DNESITY6(6),
DNESITY7(7),
DNESITY8(8),
DNESITY9(9),
DNESITY10(10),
DNESITY11(11),
DNESITY12(12),
DNESITY13(13),
DNESITY14(14),
DNESITY15(15);

private final int value;

private DENSITY(int value) {
this.value = value;
}

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

public static enum DIRECTION {
FORWARD(0),
BACKWARD(1);

private final int value;

private DIRECTION(int value) {
this.value = value;
}

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

public static enum EEC {
LEVEL_L("L"),
LEVEL_M("M"),
LEVEL_Q("Q"),
LEVEL_H("H");

private final String value;

private EEC(String value) {
this.value = value;
}

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

public static enum FONTMUL {
MUL_1(1),
MUL_2(2),
MUL_3(3),
MUL_4(4),
MUL_5(5),
MUL_6(6),
MUL_7(7),
MUL_8(8),
MUL_9(9),
MUL_10(10);

private final int value;

private FONTMUL(int value) {
this.value = value;
}

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

public static enum FONTTYPE {
FONT_1("1"),
FONT_2("2"),
FONT_3("3"),
FONT_4("4"),
FONT_5("5"),
FONT_6("6"),
FONT_7("7"),
FONT_8("8"),
SIMPLIFIED_CHINESE("TSS24.BF2"),
TRADITIONAL_CHINESE("TST24.BF2"),
KOREAN("K");

private final String value;

private FONTTYPE(String value) {
this.value = value;
}

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

public static enum FOOT {
F2(0),
F5(1);

private final int value;

private FOOT(int value) {
this.value = value;
}

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

public static enum MIRROR {
NORMAL(0),
MIRROR(1);

private final int value;

private MIRROR(int value) {
this.value = value;
}

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

public static enum READABEL {
DISABLE(0),
EANBEL(1);

private final int value;

private READABEL(int value) {
this.value = value;
}

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

public static enum ROTATION {
ROTATION_0(0),
ROTATION_90(90),
ROTATION_180(180),
ROTATION_270(270);

private final int value;

private ROTATION(int value) {
this.value = value;
}

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

public static enum SPEED {
SPEED1DIV5(1.5F),
SPEED2(2.0F),
SPEED3(3.0F),
SPEED4(4.0F);

private final float value;

private SPEED(float value) {
this.value = value;
}

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

  • 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
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604

然后定义一个操作这些指令的工具类

/**
 * 标签打印工具类
 * Created by john on 17-5-12.
 */

public class LabelUtils {
private static Pattern pattern = Pattern.compile("([a-zA-Z0-9!@#$^&*\(\)~\{\}:",\.<>/]+)");
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 PAPER_58_WIDTH = 32;
public static final int PAPER_80_WIDTH = 48;
private static int sPaperWidth = 48;
private static Integer[] theSet0 = new Integer[]{Integer.valueOf(1569), Integer.valueOf(1570), Integer.valueOf(1571), Integer.valueOf(1572), Integer.valueOf(1573), Integer.valueOf(1574), Integer.valueOf(1575), Integer.valueOf(1576), Integer.valueOf(1577), Integer.valueOf(1578), Integer.valueOf(1579), Integer.valueOf(1580), Integer.valueOf(1581), Integer.valueOf(1582), Integer.valueOf(1583), Integer.valueOf(1584), Integer.valueOf(1585), Integer.valueOf(1586), Integer.valueOf(1587), Integer.valueOf(1588), Integer.valueOf(1589), Integer.valueOf(1590), Integer.valueOf(1591), Integer.valueOf(1592), Integer.valueOf(1593), Integer.valueOf(1594), Integer.valueOf(1601), Integer.valueOf(1602), Integer.valueOf(1603), Integer.valueOf(1604), Integer.valueOf(1605), Integer.valueOf(1606), Integer.valueOf(1607), Integer.valueOf(1608), Integer.valueOf(1609), Integer.valueOf(1610), Integer.valueOf(17442), Integer.valueOf(17443), Integer.valueOf(17445), Integer.valueOf(17447)};
private static Integer[][] FormatTable = new Integer[][]{{Integer.valueOf('ﺀ'), Integer.valueOf('ﺀ'), Integer.valueOf('ﺀ'), Integer.valueOf('ﺀ')}, {Integer.valueOf('ﺁ'), Integer.valueOf('ﺂ'), Integer.valueOf('ﺁ'), Integer.valueOf('ﺂ')}, {Integer.valueOf('ﺃ'), Integer.valueOf('ﺄ'), Integer.valueOf('ﺃ'), Integer.valueOf('ﺄ')}, {Integer.valueOf('ﺅ'), Integer.valueOf('ﺅ'), Integer.valueOf('ﺅ'), Integer.valueOf('ﺅ')}, {Integer.valueOf('ﹽ'), Integer.valueOf('ﹽ'), Integer.valueOf('ﹽ'), Integer.valueOf('ﹽ')}, {Integer.valueOf('ﺋ'), Integer.valueOf('ﺋ'), Integer.valueOf('ﺋ'), Integer.valueOf('ﺋ')}, {Integer.valueOf('ﺍ'), Integer.valueOf('ﺎ'), Integer.valueOf('ﺍ'), Integer.valueOf('ﺎ')}, {Integer.valueOf('ﺏ'), Integer.valueOf('ﺏ'), Integer.valueOf('ﺑ'), Integer.valueOf('ﺑ')}, {Integer.valueOf('ﺓ'), Integer.valueOf('ﺓ'), Integer.valueOf('ﺓ'), Integer.valueOf('ﺓ')}, {Integer.valueOf('ﺕ'), Integer.valueOf('ﺕ'), Integer.valueOf('ﺗ'), Integer.valueOf('ﺗ')}, {Integer.valueOf('ﺙ'), Integer.valueOf('ﺙ'), Integer.valueOf('ﺛ'), Integer.valueOf('ﺛ')}, {Integer.valueOf('ﺝ'), Integer.valueOf('ﺝ'), Integer.valueOf('ﺟ'), Integer.valueOf('ﺟ')}, {Integer.valueOf('ﺡ'), Integer.valueOf('ﺡ'), Integer.valueOf('ﺣ'), Integer.valueOf('ﺣ')}, {Integer.valueOf('ﺥ'), Integer.valueOf('ﺥ'), Integer.valueOf('ﺧ'), Integer.valueOf('ﺧ')}, {Integer.valueOf('ﺩ'), Integer.valueOf('ﺩ'), Integer.valueOf('ﺩ'), Integer.valueOf('ﺩ')}, {Integer.valueOf('ﺫ'), Integer.valueOf('ﺫ'), Integer.valueOf('ﺫ'), Integer.valueOf('ﺫ')}, {Integer.valueOf('ﺭ'), Integer.valueOf('ﺭ'), Integer.valueOf('ﺭ'), Integer.valueOf('ﺭ')}, {Integer.valueOf('ﺯ'), Integer.valueOf('ﺯ'), Integer.valueOf('ﺯ'), Integer.valueOf('ﺯ')}, {Integer.valueOf('ﺱ'), Integer.valueOf('ﺱ'), Integer.valueOf('ﺳ'), Integer.valueOf('ﺳ')}, {Integer.valueOf('ﺵ'), Integer.valueOf('ﺵ'), Integer.valueOf('ﺷ'), Integer.valueOf('ﺷ')}, {Integer.valueOf('ﺹ'), Integer.valueOf('ﺹ'), Integer.valueOf('ﺻ'), Integer.valueOf('ﺻ')}, {Integer.valueOf('ﺽ'), Integer.valueOf('ﺽ'), Integer.valueOf('ﺿ'), Integer.valueOf('ﺿ')}, {Integer.valueOf('ﻁ'), Integer.valueOf('ﻁ'), Integer.valueOf('ﻁ'), Integer.valueOf('ﻁ')}, {Integer.valueOf('ﻅ'), Integer.valueOf('ﻅ'), Integer.valueOf('ﻅ'), Integer.valueOf('ﻅ')}, {Integer.valueOf('ﻉ'), Integer.valueOf('ﻊ'), Integer.valueOf('ﻋ'), Integer.valueOf('ﻌ')}, {Integer.valueOf('ﻍ'), Integer.valueOf('ﻎ'), Integer.valueOf('ﻏ'), Integer.valueOf('ﻐ')}, {Integer.valueOf('ﻑ'), Integer.valueOf('ﻑ'), Integer.valueOf('ﻓ'), Integer.valueOf('ﻓ')}, {Integer.valueOf('ﻕ'), Integer.valueOf('ﻕ'), Integer.valueOf('ﻗ'), Integer.valueOf('ﻗ')}, {Integer.valueOf('ﻙ'), Integer.valueOf('ﻙ'), Integer.valueOf('ﻛ'), Integer.valueOf('ﻛ')}, {Integer.valueOf('ﻝ'), Integer.valueOf('ﻝ'), Integer.valueOf('ﻟ'), Integer.valueOf('ﻟ')}, {Integer.valueOf('ﻡ'), Integer.valueOf('ﻡ'), Integer.valueOf('ﻣ'), Integer.valueOf('ﻣ')}, {Integer.valueOf('ﻥ'), Integer.valueOf('ﻥ'), Integer.valueOf('ﻧ'), Integer.valueOf('ﻧ')}, {Integer.valueOf('ﻩ'), Integer.valueOf('ﻩ'), Integer.valueOf('ﻫ'), Integer.valueOf('ﻫ')}, {Integer.valueOf('ﻭ'), Integer.valueOf('ﻭ'), Integer.valueOf('ﻭ'), Integer.valueOf('ﻭ')}, {Integer.valueOf('ﻯ'), Integer.valueOf('ﻰ'), Integer.valueOf('ﻯ'), Integer.valueOf('ﻰ')}, {Integer.valueOf('ﻱ'), Integer.valueOf('ﻲ'), Integer.valueOf('ﻳ'), Integer.valueOf('ﻳ')}, {Integer.valueOf('ﻵ'), Integer.valueOf('ﻶ'), Integer.valueOf('ﻵ'), Integer.valueOf('ﻶ')}, {Integer.valueOf('ﻷ'), Integer.valueOf('ﻸ'), Integer.valueOf('ﻷ'), Integer.valueOf('ﻸ')}, {Integer.valueOf('ﻹ'), Integer.valueOf('ﻺ'), Integer.valueOf('ﻹ'), Integer.valueOf('ﻺ')}, {Integer.valueOf('ﻻ'), Integer.valueOf('ﻼ'), Integer.valueOf('ﻻ'), Integer.valueOf('ﻼ')}};
static Integer[] theSet1 = new Integer[]{Integer.valueOf(1574), Integer.valueOf(1576), Integer.valueOf(1578), Integer.valueOf(1579), Integer.valueOf(1580), Integer.valueOf(1581), Integer.valueOf(1582), Integer.valueOf(1587), Integer.valueOf(1588), Integer.valueOf(1589), Integer.valueOf(1590), Integer.valueOf(1591), Integer.valueOf(1592), Integer.valueOf(1593), Integer.valueOf(1594), Integer.valueOf(1600), Integer.valueOf(1601), Integer.valueOf(1602), Integer.valueOf(1603), Integer.valueOf(1604), Integer.valueOf(1605), Integer.valueOf(1606), Integer.valueOf(1607), Integer.valueOf(1610)};
static Integer[] theSet2 = new Integer[]{Integer.valueOf(1570), Integer.valueOf(1571), Integer.valueOf(1572), Integer.valueOf(1573), Integer.valueOf(1574), Integer.valueOf(1575), Integer.valueOf(1576), Integer.valueOf(1577), Integer.valueOf(1578), Integer.valueOf(1579), Integer.valueOf(1580), Integer.valueOf(1581), Integer.valueOf(1582), Integer.valueOf(1583), Integer.valueOf(1584), Integer.valueOf(1585), Integer.valueOf(1586), Integer.valueOf(1587), Integer.valueOf(1588), Integer.valueOf(1589), Integer.valueOf(1590), Integer.valueOf(1591), Integer.valueOf(1592), Integer.valueOf(1593), Integer.valueOf(1594), Integer.valueOf(1600), Integer.valueOf(1601), Integer.valueOf(1602), Integer.valueOf(1603), Integer.valueOf(1604), Integer.valueOf(1605), Integer.valueOf(1606), Integer.valueOf(1607), Integer.valueOf(1608), Integer.valueOf(1609), Integer.valueOf(1610)};
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 static final int[][] COLOR_PALETTE = new int[][]{new int[3], {255, 255, 255}};
public static final int FLOYD_STEINBERG_DITHER = 1;
private static int method = 1;
public static final int ATKINSON_DITHER = 2;

public LabelUtils() {
}

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, 0, 0, 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, 100, 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(0.0F);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0.0F, 0.0F, 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 & 1);
data[4] = (byte)(nWidth / 8 % 256);
data[5] = (byte)(nWidth / 8 / 256);
data[6] = (byte)(nHeight % 256);
data[7] = (byte)(nHeight / 256);
int i = 8;

for(int k = 0; i < data.length; ++i) {
data[i] = (byte)(p0[src[k]] + p1[src[k + 1]] + p2[src[k + 2]] + p3[src[k + 3]] + p4[src[k + 4]] + p5[src[k + 5]] + p6[src[k + 6]] + src[k + 7]);
k += 8;
}

return data;
}

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

for(int k = 0; i < data.length; ++i) {
data[i] = (byte)(p0[src[k]] + p1[src[k + 1]] + p2[src[k + 2]] + p3[src[k + 3]] + p4[src[k + 4]] + p5[src[k + 5]] + p6[src[k + 6]] + src[k + 7]);
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 / 8 % 256);
data[1] = (byte)(width / 8 / 256);
data[2] = (byte)(height / 8 % 256);
data[3] = (byte)(height / 8 / 256);
boolean k = false;

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

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

return data;
}

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

for(int j = 0; k < data.length; ++k) {
byte temp = (byte)(p0[src[j]] + p1[src[j + 1]] + p2[src[j + 2]] + p3[src[j + 3]] + p4[src[j + 4]] + p5[src[j + 5]] + p6[src[j + 6]] + src[j + 7]);
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 = 0; data < arrayOfByte.length; ++data) {
byte temp = (byte)(p0[src[j]] + p1[src[j + 1]] + p2[src[j + 2]] + p3[src[j + 3]] + p4[src[j + 4]] + p5[src[j + 5]] + p6[src[j + 6]] + src[j + 7]);
arrayOfByte[data] = (byte)(~temp);
j += 8;
}

byte[] var14 = new byte[bitmap.length + arrayOfByte.length];
System.arraycopy(bitmap, 0, var14, 0, bitmap.length);
System.arraycopy(arrayOfByte, 0, 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 = 0; y < ysize; ++y) {
for(int x = 0; x < xsize; ++x) {
if((orgpixels[k] & 255) > Floyd16x16[x & 15][y & 15]) {
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, 0, mBitmap.getWidth(), 0, 0, 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 = 0; y < ysize; ++y) {
for(int x = 0; x < xsize; ++x) {
if((orgpixels[k] & 255) > Floyd16x16[x & 15][y & 15]) {
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 = 0; y < ysize; ++y) {
for(int x = 0; x < xsize; ++x) {
if((orgpixels[k] & 255) >> 2 > Floyd8x8[x & 7][y & 7]) {
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 8:
grayBitmap = toGrayscale(mBitmap);
pixels = new int[grayBitmap.getWidth() grayBitmap.getHeight()];
grayBitmap.getPixels(pixels, 0, grayBitmap.getWidth(), 0, 0, grayBitmap.getWidth(), grayBitmap.getHeight());
format_K_dither8x8_int(pixels, grayBitmap.getWidth(), grayBitmap.getHeight(), pixels);
break;
case 16:
default:
grayBitmap = toGrayscale(mBitmap);
pixels = new int[grayBitmap.getWidth()
grayBitmap.getHeight()];
grayBitmap.getPixels(pixels, 0, grayBitmap.getWidth(), 0, 0, 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 + 7) / 8 8;
int height = mBitmap.getHeight()
width / mBitmap.getWidth();
Bitmap rszBitmap = resizeImage(mBitmap, width, height);
int[] pixels = bitmapToBWPix_int(rszBitmap, algorithm);
rszBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return rszBitmap;
}

private static int getCloseColor(int tr, int tg, int tb) {
int minDistanceSquared = 195076;
int bestIndex = 0;

for(int i = 0; i < COLOR_PALETTE.length; ++i) {
int rdiff = tr - COLOR_PALETTE[i][0];
int gdiff = tg - COLOR_PALETTE[i][1];
int bdiff = tb - COLOR_PALETTE[i][2];
int distanceSquared = rdiff rdiff + gdiff gdiff + bdiff * bdiff;
if(distanceSquared < minDistanceSquared) {
minDistanceSquared = distanceSquared;
bestIndex = i;
}
}

return bestIndex;
}

private static void setPixel(int[] input, int width, int height, int col, int row, int[] p) {
if(col < 0 || col >= width) {
col = 0;
}

if(row < 0 || row >= height) {
row = 0;
}

int index = row * width + col;
input[index] = -16777216 | clamp(p[0]) << 16 | clamp(p[1]) << 8 | clamp(p[2]);
}

private static int[] getPixel(int[] input, int width, int height, int col, int row, float error, int[] ergb) {
if(col < 0 || col >= width) {
col = 0;
}

if(row < 0 || row >= height) {
row = 0;
}

int index = row width + col;
int tr = input[index] >> 16 & 255;
int tg = input[index] >> 8 & 255;
int tb = input[index] & 255;
tr = (int)((float)tr + error
(float)ergb[0]);
tg = (int)((float)tg + error (float)ergb[1]);
tb = (int)((float)tb + error
(float)ergb[2]);
return new int[]{tr, tg, tb};
}

public static int clamp(int value) {
return value > 255?255:(value < 0?0:value);
}

public static Bitmap filter(Bitmap nbm, int width, int height) {
int[] inPixels = new int[width * height];
nbm.getPixels(inPixels, 0, width, 0, 0, width, height);
int[] outPixels = new int[inPixels.length];
boolean index = false;

for(int bitmap = 0; bitmap < height; ++bitmap) {
for(int col = 0; col < width; ++col) {
int var21 = bitmap * width + col;
int r1 = inPixels[var21] >> 16 & 255;
int g1 = inPixels[var21] >> 8 & 255;
int b1 = inPixels[var21] & 255;
int cIndex = getCloseColor(r1, g1, b1);
outPixels[var21] = -16777216 | COLOR_PALETTE[cIndex][0] << 16 | COLOR_PALETTE[cIndex][1] << 8 | COLOR_PALETTE[cIndex][2];
int[] ergb = new int[]{r1 - COLOR_PALETTE[cIndex][0], g1 - COLOR_PALETTE[cIndex][1], b1 - COLOR_PALETTE[cIndex][2]};
float e1;
int[] rgb4;
int[] rgb5;
int[] rgb6;
if(method == 1) {
e1 = 0.4375F;
float rgb1 = 0.3125F;
float rgb2 = 0.1875F;
float rgb3 = 0.0625F;
rgb4 = getPixel(inPixels, width, height, col + 1, bitmap, e1, ergb);
rgb5 = getPixel(inPixels, width, height, col, bitmap + 1, rgb1, ergb);
rgb6 = getPixel(inPixels, width, height, col - 1, bitmap + 1, rgb2, ergb);
int[] rgb41 = getPixel(inPixels, width, height, col + 1, bitmap + 1, rgb3, ergb);
setPixel(inPixels, width, height, col + 1, bitmap, rgb4);
setPixel(inPixels, width, height, col, bitmap + 1, rgb5);
setPixel(inPixels, width, height, col - 1, bitmap + 1, rgb6);
setPixel(inPixels, width, height, col + 1, bitmap + 1, rgb41);
} else {
if(method != 2) {
throw new IllegalArgumentException("Not Supported Dither Mothed!!");
}

e1 = 0.125F;
int[] var23 = getPixel(inPixels, width, height, col + 1, bitmap, e1, ergb);
int[] var24 = getPixel(inPixels, width, height, col + 2, bitmap, e1, ergb);
int[] var25 = getPixel(inPixels, width, height, col - 1, bitmap + 1, e1, ergb);
rgb4 = getPixel(inPixels, width, height, col, bitmap + 1, e1, ergb);
rgb5 = getPixel(inPixels, width, height, col + 1, bitmap + 1, e1, ergb);
rgb6 = getPixel(inPixels, width, height, col, bitmap + 2, e1, ergb);
setPixel(inPixels, width, height, col + 1, bitmap, var23);
setPixel(inPixels, width, height, col + 2, bitmap, var24);
setPixel(inPixels, width, height, col - 1, bitmap + 1, var25);
setPixel(inPixels, width, height, col, bitmap + 1, rgb4);
setPixel(inPixels, width, height, col + 1, bitmap + 1, rgb5);
setPixel(inPixels, width, height, col, bitmap + 2, rgb6);
}
}
}

Bitmap var22 = Bitmap.createBitmap(outPixels, 0, width, width, height, Config.RGB_565);
return var22;
}

static String splitArabic(String input) {
StringBuilder sb = new StringBuilder(256);
String[] arabics = input.split("\n");
int i;
int childStringLength;
if(arabics.length == 1 && arabics[0].length() > sPaperWidth) {
i = arabics[0].length() / sPaperWidth;
childStringLength = 1;

for(int j = 0; childStringLength <= i; ++childStringLength) {
sb.append(arabics[0].substring(j, sPaperWidth * childStringLength));
j += sPaperWidth;
}

if(sb.length() >= 0) {
sb.append('\n');
}

childStringLength = arabics[0].length() % sPaperWidth;
sb.append(arabics[0].substring(arabics[0].length() - childStringLength, arabics[0].length()));
return splitArabic(sb.toString());
} else {
for(i = 0; i < arabics.length; ++i) {
childStringLength = arabics[i].length();
if(childStringLength > sPaperWidth) {
sb.append(splitArabic(arabics[i]));
} else {
sb.append(addSpaceAfterArabicString(arabics[i], sPaperWidth - childStringLength));
}
}

return sb.toString();
}
}

static String addSpaceAfterArabicString(String arabic, int number) {
StringBuilder sb = new StringBuilder(65);
sb.append(arabic);

for(int i = 0; i < number; ++i) {
sb.append(' ');
}

sb.append('\n');
return sb.toString();
}

static String reverseLetterAndNumber(String input) {
StringBuilder sb = new StringBuilder(input);
Matcher matcher = pattern.matcher(input);

while(matcher.find()) {
String matcherString = matcher.group();
int matcherStart = matcher.start();
int matcherEnd = matcher.end();
sb.replace(matcherStart, matcherEnd, (new StringBuilder(matcherString)).reverse().toString());
}

return sb.toString();
}

static byte[] string2Cp864(String arabicString) {
Integer[] originUnicode = new Integer[arabicString.length()];
Integer[] outputUnicode = new Integer[arabicString.length()];
Integer[] outputChars = new Integer[originUnicode.length];
copy(arabicString.toCharArray(), originUnicode, arabicString.length());
ArrayList list = new ArrayList(Arrays.asList(originUnicode));
List list1 = Hyphen(list);
list1 = Deformation(list1);
Collections.reverse(list1);
list1.toArray(outputUnicode);
char[] chs = integer2Character(outputUnicode);
byte[] cp864bytes = new byte[0];

try {
cp864bytes = (new String(chs)).getBytes("cp864");
} catch (UnsupportedEncodingException var8) {
var8.printStackTrace();
}

return cp864bytes;
}

static char[] integer2Character(Integer[] integers) {
char[] chs = new char[integers.length];

for(int i = 0; i < integers.length; ++i) {
if(integers[i] != null) {
chs[i] = (char)integers[i].intValue();
} else {
chs[i] = 32;
}
}

return chs;
}

static void copy(char[] array, Integer[] originUnicode, int length) {
for(int i = 0; i < length; ++i) {
originUnicode[i] = Integer.valueOf(array[i]);
}

}

static List<Integer> Hyphen(List<Integer> list) {
for(int i = 0; i < list.size(); ++i) {
if(((Integer)list.get(i)).intValue() == 1604) {
switch(((Integer)list.get(i + 1)).intValue()) {
case 1570:
list.set(i, Integer.valueOf(17442));
list.remove(i + 1);
break;
case 1571:
list.set(i, Integer.valueOf(17443));
list.remove(i + 1);
case 1572:
case 1574:
default:
break;
case 1573:
list.set(i, Integer.valueOf(17445));
list.remove(i + 1);
break;
case 1575:
list.set(i, Integer.valueOf(17447));
list.remove(i + 1);
}
}
}

return list;
}

static List<Integer> Deformation(List<Integer> inputlist) {
boolean flag = false;
ArrayList outputlist = new ArrayList();
HashMap formHashTable = new HashMap(40);

int i;
for(i = 0; i < 40; ++i) {
formHashTable.put(theSet0[i], FormatTable[i]);
}

for(i = 0; i < inputlist.size(); ++i) {
if(compare((Integer)inputlist.get(i), 0)) {
boolean inSet1;
boolean inSet2;
int var8;
if(i 0) {
inSet1 = false;
inSet2 = compare((Integer)inputlist.get(i + 1), 2);
var8 = Flag(inSet1, inSet2);
} else if(i inputlist.size() - 1) {
inSet1 = compare((Integer)inputlist.get(i - 1), 1);
inSet2 = false;
var8 = Flag(inSet1, inSet2);
} else {
inSet1 = compare((Integer)inputlist.get(i - 1), 1);
inSet2 = compare((Integer)inputlist.get(i + 1), 2);
var8 = Flag(inSet1, inSet2);
}

Integer[] a = (Integer[])formHashTable.get(inputlist.get(i));
outputlist.add(a[var8]);
} else {
outputlist.add((Integer)inputlist.get(i));
}
}

return outputlist;
}

static boolean compare(Integer input, int i) {
ArrayList list = new ArrayList(3);
list.add(theSet0);
list.add(theSet1);
list.add(theSet2);
return findInArray((Integer[])list.get(i), input.intValue());
}

static boolean findInArray(Integer[] integer, int input) {
for(int j = 0; j < integer.length; ++j) {
if(integer[j].intValue() == input) {
return true;
}
}

return false;
}

static int Flag(boolean set1, boolean set2) {
return set1 && set2?3:(!set1 && set2?2:(set1 && !set2?1:0));
}

public static void setPaperWidth(int paperWidth) {
sPaperWidth = paperWidth;
}

public static byte[] ByteTo_byte(Vector<Byte> vector) {
int len = vector.size();
byte[] data = new byte[len];

for(int i = 0; i < len; ++i) {
data[i] = ((Byte)vector.get(i)).byteValue();
}

return data;
}

public int getMethod() {
return method;
}

public void setMethod(int method) {
method = method;
}

public static enum ENABLE {
OFF(0),
ON(1);

private final int value;

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

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

  • 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
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604
  • 605
  • 606
  • 607
  • 608
  • 609
  • 610
  • 611
  • 612
  • 613

接下来就是设置具体的打印模板及内容

//标签打印
LabelCommand tsc = new LabelCommand();
tsc.addSize(40, 30); // 设置标签尺寸,按照实际尺寸设置
tsc.addGap(2); // 设置标签间隙,按照实际尺寸设置,如果为无间隙纸则设置为0
tsc.addDirection(LabelCommand.DIRECTION.BACKWARD, LabelCommand.MIRROR.NORMAL);// 设置打印方向
tsc.addReference(0, 0);// 设置原点坐标
tsc.addTear(LabelUtils.ENABLE.ON); // 撕纸模式开启

tsc.addCls();// 清除打印缓冲区
// 绘制简体中文
tsc.addText(20, 20, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
" 天猫超市");
tsc.addText(20, 50, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
"商品:农夫山泉");
tsc.addText(20, 80, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
"规格:箱");
tsc.addText(20, 110, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
"金额:¥16.00");
tsc.addText(20, 140, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
"时间:2017/05/19 15:00");
tsc.addText(20, 170, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
"电话:18818181818");

// 绘制图片
// Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.product_000001);
// tsc.addBitmap(20, 50, LabelCommand.BITMAP_MODE.OVERWRITE, b.getWidth(), b);

// 绘制二维码
// tsc.addQRCode(250, 80, LabelCommand.EEC.LEVEL_L, 5, LabelCommand.ROTATION.ROTATION_0, " www.gg.com.cn");

// 绘制条形码
// tsc.add1DBarcode(20, 250, LabelCommand.BARCODETYPE.CODE128, 100, LabelCommand.READABEL.EANBEL, LabelCommand.ROTATION.ROTATION_0, "printer");

tsc.addPrint(1, 1); // 打印标签
tsc.addSound(2, 100); // 打印标签后 蜂鸣器响

// tsc.addCashdrwer(LabelCommand.FOOT.F5, 255, 255);

Vector<Byte> datas = tsc.getCommand(); // 发送数据
byte[] bytes = LabelUtils.ByteTo_byte(datas);

String str = Base64.encodeToString(bytes, Base64.DEFAULT);

  • 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

最后就是将这些指令数据传输给打印设备

传输方式有USB、BlueTooth、NetWork等,这里给出蓝牙传输的例子

byte[] decode_datas = Base64.decode(str, Base64.DEFAULT);

BluetoothManager btManager = BluetoothManager.getInstance();
if (btManager.hasConnectedDevice()) {
btManager.printText(decode_datas, new BluetoothManager.OnPrintListener() {
@Override
public void onPrintFinished() {
showPrintFinished();
}

@Override
public void onPrintFail(final BluetoothDevice device) {
showPrintFailed(device.getName());
}
});
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

打印效果

这里写图片描述







资深Android开发工程师


微信名片




posted @ 2024-04-12 14:42  一贴灵  阅读(228)  评论(0)    收藏  举报
学以致用,效率第一