某人

此前素未谋面、此后遥遥无期

0%

rn蓝牙打印2

rn蓝牙打印2

本文是 rn蓝牙打印 的补充

上一篇文章里面主要说了蓝牙打印用ESC指令,这一篇补充一下TSC指令

以下大部分代码来源于佳博SDK

测试代码执行入口

  • 测试发送TSC数据
  • 测试发送ESC数据
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
package com.xx.yy.gpbt;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.masget.rcyltms.app.MainActivity;
import com.masget.rcyltms.app.R;
import com.masget.rcyltms.utils.BaseUtil;
import com.masget.rcyltms.utils.PictureUtils;

import java.util.Vector;

/**
* Created by Administrator on 2018/12/14.
*/

public class GpPrint {
/**
* 测试发送TSC数据
*/
public static byte[] sendTSCData() {
String base64Str = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQQAAAEECAIAAABBat1dAAAD+UlEQVR4nO3dwY7TMBRA0Rbx/788bK5GiAUOsmWc9Jw1omk6V948Pb+/vr5ewOv1438/AJxCDBAxQMQAEQNEDBAxQMQAEQNEDJCfw3/xfr83PMch9gynDF/pIY/xJFdeqZMBIgaIGCBigIgBIgaIGCBigIgBIgbIeBxj6Eb7Ne4ygHDIvMan/bJOBogYIGKAiAEiBogYIGKAiAEiBogYIAvGMYb2DEGcMzswP0wx/B+uvNInrdjY812cDBAxQMQAEQNEDBAxQMQAEQNEDBAxQMQA2TGb9GnmR4/4L5wMEDFAxAARA0QMEDFAxAARA0QMEDFAjGP8myU7Ws7ZasPvnAwQMUDEABEDRAwQMUDEABEDRAwQMUB2jGM8afrgkOtwznml5zzJPCcDRAwQMUDEABEDRAwQMUDEABEDRAwQMUAWzCZ91D00S1bFzI8eLXmMoY/6ZV9OBvgmBogYIGKAiAEiBogYIGKAiAEiBsh4HONJu0D2uMsUg1/2D04GiBggYoCIASIGiBggYoCIASIGiBggO27uWTKesGR24C6DEkNX3sb8l13yKTca+nAyQMQAEQNEDBAxQMQAEQNEDBAxQMQAEQPkveSSmL87Z6xow506VzxmROq16JKhDa78cE4GiBggYoCIASIGiBggYoCIASIGiBggC8YxDtkFcs5CmnmHfJdDHmMbJwNEDBAxQMQAEQNEDBAxQMQAEQNEDJDb3NyzxI1GA+bNz9HseV17/jxsx4B/IAaIGCBigIgBIgaIGCBigIgBIgbIeBxjz20Uez5lfkLh0xZGDB2yPGXJpzgZIGKAiAEiBogYIGKAiAEiBogYIGKAiAEyvrmH312ZTTrklc6PUS35Ijea5nIyQMQAEQNEDBAxQMQAEQNEDBAxQMQAGa+KOefenQ0OuavmRhtrDvnzWLKxxskAEQNEDBAxQMQAEQNEDBAxQMQAEQNkPI4xdMgyiCv2LIzYMG2xZ5Liyqcc8l2WcDJAxAARA0QMEDFAxAARA0QMEDFAxAARA2TBbNLQnm0i54y4HPIkexbSzH/ZQ5bNvJwM8E0MEDFAxAARA0QMEDFAxAARA0QMkB3jGE+yZEJhfgDhnBGGJ91C5GSAiAEiBogYIGKAiAEiBogYIGKAiAFiHONEe/ZrnDPTMbTnhTgZIGKAiAEiBogYIGKAiAEiBogYIGKAiAGyYzbpkJtslljyXQ7ZnrLnMfZMQM1vrHk5GeCbGCBigIgBIgaIGCBigIgBIgaIGCALxjFutHFkjyWjAXd5jPn/xM09cBwxQMQAEQNEDBAxQMQAEQNEDBAxQN5PWl0BM5wMEDFAxAARA0QMEDFAxAARA0QMEDFAxAD5BVn2+wfjFK/cAAAAAElFTkSuQmCC";
LabelCommand tsc = new LabelCommand();
// 设置标签尺寸,按照实际尺寸设置
tsc.addSize(60, 60);
// 设置标签间隙,按照实际尺寸设置,如果为无间隙纸则设置为0
tsc.addGap(0);
// 设置打印方向
tsc.addDirection(LabelCommand.DIRECTION.BACKWARD, LabelCommand.MIRROR.NORMAL);
// 开启带Response的打印,用于连续打印
tsc.addQueryPrinterStatus(LabelCommand.RESPONSE_MODE.ON);
// 设置原点坐标
tsc.addReference(0, 0);
// 撕纸模式开启
tsc.addTear(EscCommand.ENABLE.ON);
// 清除打印缓冲区
tsc.addCls();
// 绘制简体中文
tsc.addText(20, 20, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
"Welcome to use SMARNET printer!");
// 绘制图片
Bitmap b = PictureUtils.base64StringToBitmap(base64Str);
// Bitmap b = BitmapFactory.decodeResource(MainActivity.getMainActivity().getResources(), R.mipmap.ic_launcher);
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.smarnet.cc");
// 绘制一维条码
tsc.add1DBarcode(20, 250, LabelCommand.BARCODETYPE.CODE128, 100, LabelCommand.READABEL.EANBEL, LabelCommand.ROTATION.ROTATION_0, "SMARNET");
// 打印标签
tsc.addPrint(1, 1);
// 打印标签后 蜂鸣器响

tsc.addSound(2, 100);
tsc.addCashdrwer(LabelCommand.FOOT.F5, 255, 255);
Vector<Byte> datas = tsc.getCommand();
// 发送数据
byte[] byteData = BaseUtil.convertVectorByteTobytes(datas);
return byteData;
}

/**
* 测试发送ESC数据
*/
public static byte[] sendESCData() {
String base64Str = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQQAAAEECAIAAABBat1dAAAD+UlEQVR4nO3dwY7TMBRA0Rbx/788bK5GiAUOsmWc9Jw1omk6V948Pb+/vr5ewOv1438/AJxCDBAxQMQAEQNEDBAxQMQAEQNEDJCfw3/xfr83PMch9gynDF/pIY/xJFdeqZMBIgaIGCBigIgBIgaIGCBigIgBIgbIeBxj6Eb7Ne4ygHDIvMan/bJOBogYIGKAiAEiBogYIGKAiAEiBogYIAvGMYb2DEGcMzswP0wx/B+uvNInrdjY812cDBAxQMQAEQNEDBAxQMQAEQNEDBAxQMQA2TGb9GnmR4/4L5wMEDFAxAARA0QMEDFAxAARA0QMEDFAjGP8myU7Ws7ZasPvnAwQMUDEABEDRAwQMUDEABEDRAwQMUB2jGM8afrgkOtwznml5zzJPCcDRAwQMUDEABEDRAwQMUDEABEDRAwQMUAWzCZ91D00S1bFzI8eLXmMoY/6ZV9OBvgmBogYIGKAiAEiBogYIGKAiAEiBsh4HONJu0D2uMsUg1/2D04GiBggYoCIASIGiBggYoCIASIGiBggO27uWTKesGR24C6DEkNX3sb8l13yKTca+nAyQMQAEQNEDBAxQMQAEQNEDBAxQMQAEQPkveSSmL87Z6xow506VzxmROq16JKhDa78cE4GiBggYoCIASIGiBggYoCIASIGiBggC8YxDtkFcs5CmnmHfJdDHmMbJwNEDBAxQMQAEQNEDBAxQMQAEQNEDJDb3NyzxI1GA+bNz9HseV17/jxsx4B/IAaIGCBigIgBIgaIGCBigIgBIgbIeBxjz20Uez5lfkLh0xZGDB2yPGXJpzgZIGKAiAEiBogYIGKAiAEiBogYIGKAiAEyvrmH312ZTTrklc6PUS35Ijea5nIyQMQAEQNEDBAxQMQAEQNEDBAxQMQAGa+KOefenQ0OuavmRhtrDvnzWLKxxskAEQNEDBAxQMQAEQNEDBAxQMQAEQNkPI4xdMgyiCv2LIzYMG2xZ5Liyqcc8l2WcDJAxAARA0QMEDFAxAARA0QMEDFAxAARA2TBbNLQnm0i54y4HPIkexbSzH/ZQ5bNvJwM8E0MEDFAxAARA0QMEDFAxAARA0QMkB3jGE+yZEJhfgDhnBGGJ91C5GSAiAEiBogYIGKAiAEiBogYIGKAiAFiHONEe/ZrnDPTMbTnhTgZIGKAiAEiBogYIGKAiAEiBogYIGKAiAGyYzbpkJtslljyXQ7ZnrLnMfZMQM1vrHk5GeCbGCBigIgBIgaIGCBigIgBIgaIGCALxjFutHFkjyWjAXd5jPn/xM09cBwxQMQAEQNEDBAxQMQAEQNEDBAxQN5PWl0BM5wMEDFAxAARA0QMEDFAxAARA0QMEDFAxAD5BVn2+wfjFK/cAAAAAElFTkSuQmCC";
EscCommand esc = new EscCommand();
esc.addInitializePrinter();
esc.addPrintAndFeedLines((byte) 3);
// 设置打印居中
esc.addSelectJustification(EscCommand.JUSTIFICATION.CENTER);
// 设置为倍高倍宽
esc.addSelectPrintModes(EscCommand.FONT.FONTA, EscCommand.ENABLE.OFF, EscCommand.ENABLE.ON, EscCommand.ENABLE.ON, EscCommand.ENABLE.OFF);
// 打印文字
esc.addText("Sample\n");
esc.addPrintAndLineFeed();

/* 打印文字 */
// 取消倍高倍宽
esc.addSelectPrintModes(EscCommand.FONT.FONTA, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF);
// 设置打印左对齐
esc.addSelectJustification(EscCommand.JUSTIFICATION.LEFT);
// 打印文字
esc.addText("Print text\n");
// 打印文字
esc.addText("Welcome to use SMARNET printer!\n");

/* 打印繁体中文 需要打印机支持繁体字库 */
String message = "佳博智匯票據打印機\n";
esc.addText(message, "GB2312");
esc.addPrintAndLineFeed();

/* 绝对位置 具体详细信息请查看GP58编程手册 */
esc.addText("智汇");
esc.addSetHorAndVerMotionUnits((byte) 7, (byte) 0);
esc.addSetAbsolutePrintPosition((short) 6);
esc.addText("网络");
esc.addSetAbsolutePrintPosition((short) 10);
esc.addText("设备");
esc.addPrintAndLineFeed();

/* 打印图片 */
// 打印文字
esc.addText("Print bitmap!\n");
Bitmap b = PictureUtils.base64StringToBitmap(base64Str);
//Bitmap b = BitmapFactory.decodeResource(MainActivity.getMainActivity().getResources(), R.mipmap.ic_launcher);
// 打印图片
esc.addOriginRastBitImage(b, b.getWidth(), 0);

/* 打印一维条码 */
// 打印文字
esc.addText("Print code128\n");
esc.addSelectPrintingPositionForHRICharacters(EscCommand.HRI_POSITION.BELOW);
// 设置条码可识别字符位置在条码下方
// 设置条码高度为60点
esc.addSetBarcodeHeight((byte) 60);
// 设置条码单元宽度为1
esc.addSetBarcodeWidth((byte) 1);
// 打印Code128码
esc.addCODE128(esc.genCodeB("SMARNET"));
esc.addPrintAndLineFeed();

/*
* QRCode命令打印 此命令只在支持QRCode命令打印的机型才能使用。 在不支持二维码指令打印的机型上,则需要发送二维条码图片
*/
// 打印文字
esc.addText("Print QRcode\n");
// 设置纠错等级
esc.addSelectErrorCorrectionLevelForQRCode((byte) 0x31);
// 设置qrcode模块大小
esc.addSelectSizeOfModuleForQRCode((byte) 3);
// 设置qrcode内容
esc.addStoreQRCodeData("www.smarnet.cc");
esc.addPrintQRCode();// 打印QRCode
esc.addPrintAndLineFeed();

// 设置打印左对齐
esc.addSelectJustification(EscCommand.JUSTIFICATION.CENTER);
//打印文字
esc.addText("Completed!\r\n");

// 开钱箱
esc.addGeneratePlus(LabelCommand.FOOT.F5, (byte) 255, (byte) 255);
esc.addPrintAndFeedLines((byte) 8);
// 加入查询打印机状态,打印完成后,此时会接收到GpCom.ACTION_DEVICE_STATUS广播
esc.addQueryPrinterStatus();
Vector<Byte> datas = esc.getCommand();
// 发送数据
byte[] byteData = BaseUtil.convertVectorByteTobytes(datas);
return byteData;
}

public static byte[] printLabel() {
byte[] byteData = {};
return byteData;
}

/**
* 转换图片TSC
* @return
*/
public static byte[] convertTSCImgToBase64(String base64Str) {
LabelCommand tsc = new LabelCommand();
// 设置原点坐标
// tsc.addReference(0, 0);
// 清除打印缓冲区
// tsc.addCls();
Bitmap b = PictureUtils.base64StringToBitmap(base64Str);
tsc.addBitmap(50, 55, LabelCommand.BITMAP_MODE.OVERWRITE, b.getWidth(), b);
// 打印标签
// tsc.addPrint(1, 1);
Vector<Byte> datas = tsc.getCommand();
// 发送数据
byte[] byteData = BaseUtil.convertVectorByteTobytes(datas);
return byteData;
}

/**
* 转换图片ESC
* @return
*/
public static byte[] convertESCImgToBase64(String base64Str) {
EscCommand esc = new EscCommand();
Bitmap b = PictureUtils.base64StringToBitmap(base64Str);
// 打印图片
esc.addOriginRastBitImage(b, b.getWidth(), 0);
Vector<Byte> datas = esc.getCommand();
// 发送数据
byte[] byteData = BaseUtil.convertVectorByteTobytes(datas);
return byteData;
}
}

ESC命令方法封装类

关于ESC命令具体请参考ESC命令文档

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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.xx.yy.gpbt;

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

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.masget.rcyltms.gpbt.LabelCommand.FOOT;

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

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

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

}

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]));
}
}

}

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

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

}

private void addStrToCommandUTF8Encoding(String str, int length) {
byte[] bs = null;
if(!str.equals("")) {
try {
bs = str.getBytes("UTF-8");
} catch (UnsupportedEncodingException var5) {
var5.printStackTrace();
}

Log.d("EscCommand", "bs.length" + bs.length);
if(length > bs.length) {
length = bs.length;
}

Log.d("EscCommand", "length" + length);

for(int i = 0; i < 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 var5) {
var5.printStackTrace();
}

Log.d("EscCommand", "bs.length" + bs.length);
if(length > bs.length) {
length = bs.length;
}

Log.d("EscCommand", "length" + length);

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

}

public void addHorTab() {
byte[] command = new byte[]{9};
this.addArrayToCommand(command);
}

public void addText(String text) {
this.addStrToCommand(text);
}

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

public void addArabicText(String text) {
text = GpUtils.reverseLetterAndNumber(text);
text = GpUtils.splitArabic(text);
String[] fooInput = text.split("\\n");
String[] var3 = fooInput;
int var4 = fooInput.length;

for(int var5 = 0; var5 < var4; ++var5) {
String in = var3[var5];
byte[] output = GpUtils.string2Cp864(in);

for(int i = 0; i < output.length; ++i) {
if(output[i] == -16) {
this.addArrayToCommand(new byte[]{27, 116, 29, -124, 27, 116, 22});
} else if(output[i] == 127) {
byte a = -41;
this.Command.add(Byte.valueOf(a));
} else {
this.Command.add(Byte.valueOf(output[i]));
}
}
}

}

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

public void RealtimeStatusTransmission(EscCommand.STATUS status) {
byte[] command = new byte[]{16, 4, status.getValue()};
this.addArrayToCommand(command);
}

public void addGeneratePluseAtRealtime(FOOT foot, byte t) {
byte[] command = new byte[]{16, 20, 1, (byte)foot.getValue(), 0};
if(t > 8) {
t = 8;
}

command[4] = t;
this.addArrayToCommand(command);
}

public void addSound(byte n, byte t) {
byte[] command = new byte[]{27, 66, 0, 0};
if(n < 0) {
n = 1;
} else if(n > 9) {
n = 9;
}

if(t < 0) {
t = 1;
} else if(t > 9) {
t = 9;
}

command[2] = n;
command[3] = t;
this.addArrayToCommand(command);
}

public void addSetRightSideCharacterSpacing(byte n) {
byte[] command = new byte[]{27, 32, n};
this.addArrayToCommand(command);
}

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

public void addSelectPrintModes(EscCommand.FONT font, EscCommand.ENABLE emphasized, EscCommand.ENABLE doubleheight, EscCommand.ENABLE doublewidth, EscCommand.ENABLE underline) {
byte temp = 0;
if(font == EscCommand.FONT.FONTB) {
temp = 1;
}

if(emphasized == EscCommand.ENABLE.ON) {
temp = (byte)(temp | 8);
}

if(doubleheight == EscCommand.ENABLE.ON) {
temp = (byte)(temp | 16);
}

if(doublewidth == EscCommand.ENABLE.ON) {
temp = (byte)(temp | 32);
}

if(underline == EscCommand.ENABLE.ON) {
temp = (byte)(temp | 128);
}

byte[] command = new byte[]{27, 33, temp};
this.addArrayToCommand(command);
}

public void addSetAbsolutePrintPosition(short n) {
byte[] command = new byte[]{27, 36, 0, 0};
byte nl = (byte)(n % 256);
byte nh = (byte)(n / 256);
command[2] = nl;
command[3] = nh;
this.addArrayToCommand(command);
}

public void addSelectOrCancelUserDefineCharacter(EscCommand.ENABLE enable) {
byte[] command = new byte[]{27, 37, 0};
if(enable == EscCommand.ENABLE.ON) {
command[2] = 1;
} else {
command[2] = 0;
}

this.addArrayToCommand(command);
}

public void addTurnUnderlineModeOnOrOff(EscCommand.UNDERLINE_MODE underline) {
byte[] command = new byte[]{27, 45, underline.getValue()};
this.addArrayToCommand(command);
}

public void addSelectDefualtLineSpacing() {
byte[] command = new byte[]{27, 50};
this.addArrayToCommand(command);
}

public void addSetLineSpacing(byte n) {
byte[] command = new byte[]{27, 51, n};
this.addArrayToCommand(command);
}

public void addCancelUserDefinedCharacters(byte n) {
byte[] command = new byte[]{27, 63, 0};
if(n >= 32 && n <= 126) {
command[2] = n;
} else {
command[2] = 32;
}

this.addArrayToCommand(command);
}

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

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

public void addTurnDoubleStrikeOnOrOff(EscCommand.ENABLE enabel) {
byte[] command = new byte[]{27, 71, enabel.getValue()};
this.addArrayToCommand(command);
}

public void addPrintAndFeedPaper(byte n) {
byte[] command = new byte[]{27, 74, n};
this.addArrayToCommand(command);
}

public void addSelectCharacterFont(EscCommand.FONT font) {
byte[] command = new byte[]{27, 77, font.getValue()};
this.addArrayToCommand(command);
}

public void addSelectInternationalCharacterSet(EscCommand.CHARACTER_SET set) {
byte[] command = new byte[]{27, 82, set.getValue()};
this.addArrayToCommand(command);
}

public void addTurn90ClockWiseRotatin(EscCommand.ENABLE enabel) {
byte[] command = new byte[]{27, 86, enabel.getValue()};
this.addArrayToCommand(command);
}

public void addSetRelativePrintPositon(short n) {
byte[] command = new byte[]{27, 92, 0, 0};
byte nl = (byte)(n % 256);
byte nh = (byte)(n / 256);
command[2] = nl;
command[3] = nh;
this.addArrayToCommand(command);
}

public void addSelectJustification(EscCommand.JUSTIFICATION just) {
byte[] command = new byte[]{27, 97, just.getValue()};
this.addArrayToCommand(command);
}

public void addPrintAndFeedLines(byte n) {
byte[] command = new byte[]{27, 100, n};
this.addArrayToCommand(command);
}

public void addGeneratePlus(FOOT foot, byte t1, byte t2) {
byte[] command = new byte[]{27, 112, (byte)foot.getValue(), t1, t2};
this.addArrayToCommand(command);
}

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

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

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

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

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

public void addSetLeftMargin(short n) {
byte[] command = new byte[]{29, 76, 0, 0};
byte nl = (byte)(n % 256);
byte nh = (byte)(n / 256);
command[2] = nl;
command[3] = nh;
this.addArrayToCommand(command);
}

public void addSetHorAndVerMotionUnits(byte x, byte y) {
byte[] command = new byte[]{29, 80, x, y};
this.addArrayToCommand(command);
}

public void addCutAndFeedPaper(byte length) {
byte[] command = new byte[]{29, 86, 66, length};
this.addArrayToCommand(command);
}

public void addCutPaper() {
byte[] command = new byte[]{29, 86, 1};
this.addArrayToCommand(command);
}

public void addSetPrintingAreaWidth(short width) {
byte nl = (byte)(width % 256);
byte nh = (byte)(width / 256);
byte[] command = new byte[]{29, 87, nl, nh};
this.addArrayToCommand(command);
}

public void addSetAutoSatusBack(EscCommand.ENABLE enable) {
byte[] command = new byte[]{29, 97, 0};
if(enable == EscCommand.ENABLE.OFF) {
command[2] = 0;
} else {
command[2] = -1;
}

this.addArrayToCommand(command);
}

public void addSetFontForHRICharacter(EscCommand.FONT font) {
byte[] command = new byte[]{29, 102, font.getValue()};
this.addArrayToCommand(command);
}

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

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

if(width < 2) {
width = 1;
}

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

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

if(DoubleHeight == EscCommand.ENABLE.ON) {
temp = (byte)(temp | 8);
}

if(Underline == EscCommand.ENABLE.ON) {
temp = (byte)(temp | 128);
}

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

public void addSelectKanjiMode() {
byte[] command = new byte[]{28, 38};
this.addArrayToCommand(command);
}

public void addSetKanjiUnderLine(EscCommand.UNDERLINE_MODE underline) {
byte[] command = new byte[]{28, 45, 0};
command[3] = underline.getValue();
this.addArrayToCommand(command);
}

public void addCancelKanjiMode() {
byte[] command = new byte[]{28, 46};
this.addArrayToCommand(command);
}

public void addSetKanjiLefttandRightSpace(byte left, byte right) {
byte[] command = new byte[]{28, 83, left, right};
this.addArrayToCommand(command);
}

public void addSetQuadrupleModeForKanji(EscCommand.ENABLE enable) {
byte[] command = new byte[]{28, 87, enable.getValue()};
this.addArrayToCommand(command);
}

public void addRastBitImage(Bitmap bitmap, int nWidth, int nMode) {
if(bitmap != null) {
int width = (nWidth + 7) / 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 & 1);
command[4] = (byte)(width / 8 % 256);
command[5] = (byte)(width / 8 / 256);
command[6] = (byte)(height % 256);
command[7] = (byte)(height / 256);
this.addArrayToCommand(command);
byte[] codecontent = GpUtils.pixToEscRastBitImageCmd(src);

for(int k = 0; k < codecontent.length; ++k) {
this.Command.add(Byte.valueOf(codecontent[k]));
}
} else {
Log.d("BMP", "bmp. null ");
}

}

public void addOriginRastBitImage(Bitmap bitmap, int nWidth, int nMode) {
if(bitmap != null) {
int width = (nWidth + 7) / 8 * 8;
int height = bitmap.getHeight() * width / bitmap.getWidth();
Bitmap rszBitmap = GpUtils.resizeImage(bitmap, width, height);
byte[] data = GpUtils.printEscDraw(rszBitmap);
this.addArrayToCommand(data);
} else {
Log.d("BMP", "bmp. null ");
}

}

public void addRastBitImageWithMethod(Bitmap bitmap, int nWidth, int nMode, int method) {
if(bitmap != null) {
int width = (nWidth + 7) / 8 * 8;
int height = bitmap.getHeight() * width / bitmap.getWidth();
Bitmap resizeImage = GpUtils.resizeImage(bitmap, width, height);
Bitmap rszBitmap = GpUtils.filter(resizeImage, resizeImage.getWidth(), resizeImage.getHeight());
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 & 1);
command[4] = (byte)(width / 8 % 256);
command[5] = (byte)(width / 8 / 256);
command[6] = (byte)(height % 256);
command[7] = (byte)(height / 256);
this.addArrayToCommand(command);
byte[] codecontent = GpUtils.pixToEscRastBitImageCmd(src);

for(int k = 0; 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", "bmp. null ");
} else {
Log.d("BMP", "bitmap.length " + bitmap.length);
int n = bitmap.length;
if(n > 0) {
byte[] command = new byte[]{28, 113, (byte)n};
this.addArrayToCommand(command);

for(int i = 0; i < n; ++i) {
int height = (bitmap[i].getHeight() + 7) / 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 = 0; k < codecontent.length; ++k) {
this.Command.add(Byte.valueOf(codecontent[k]));
}
}
}

}
}

public void addPrintNvBitmap(byte n, byte mode) {
byte[] command = new byte[]{28, 112, n, mode};
this.addArrayToCommand(command);
}

public void addUPCA(String content) {
byte[] command = new byte[]{29, 107, 65, 11};
if(content.length() >= command[3]) {
this.addArrayToCommand(command);
this.addStrToCommand(content, 11);
}
}

public void addUPCE(String content) {
byte[] command = new byte[]{29, 107, 66, 11};
if(content.length() >= command[3]) {
this.addArrayToCommand(command);
this.addStrToCommand(content, command[3]);
}
}

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

public void addEAN8(String content) {
byte[] command = new byte[]{29, 107, 68, 7};
if(content.length() >= command[3]) {
this.addArrayToCommand(command);
this.addStrToCommand(content, command[3]);
}
}

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

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

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

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

public void addCODE128(String content) {
byte[] command = new byte[]{29, 107, 73, (byte)content.length()};
this.addArrayToCommand(command);
this.addStrToCommand(content, command[3]);
}

public String genCodeC(String content) {
List<Byte> bytes = new ArrayList(20);
int len = content.length();
byte a = 123;
byte b = 67;
bytes.add(Byte.valueOf(a));
bytes.add(Byte.valueOf(b));

int i;
for(i = 0; i < len; i += 2) {
i = (content.charAt(i) - 48) * 10;
int bits = content.charAt(i + 1) - 48;
int current = i + bits;
bytes.add(Byte.valueOf((byte)current));
}

byte[] bb = new byte[bytes.size()];

for(i = 0; i < bb.length; ++i) {
bb[i] = ((Byte)bytes.get(i)).byteValue();
}

return new String(bb, 0, bb.length);
}

public String genCodeB(String content) {
return String.format("{B%s", new Object[]{content});
}

public String genCode128(String content) {
String regex = "([^0-9])";
String[] str = content.split(regex);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);
String splitString = null;
int strlen = str.length;
if(strlen > 0 && matcher.find()) {
splitString = matcher.group(0);
}

StringBuilder sb = new StringBuilder();

for(int i = 0; i < strlen; ++i) {
String first = str[i];
int len = first.length();
int result = len % 2;
if(result == 0) {
String codeC = this.genCodeC(first);
sb.append(codeC);
} else {
sb.append(this.genCodeB(String.valueOf(first.charAt(0))));
sb.append(this.genCodeC(first.substring(1, first.length())));
}

if(splitString != null) {
sb.append(this.genCodeB(splitString));
splitString = null;
}
}

return sb.toString();
}

public void addSelectSizeOfModuleForQRCode(byte n) {
byte[] command = new byte[]{29, 40, 107, 3, 0, 49, 67, 3};
command[7] = n;
this.addArrayToCommand(command);
}

public void addSelectErrorCorrectionLevelForQRCode(byte n) {
byte[] command = new byte[]{29, 40, 107, 3, 0, 49, 69, n};
this.addArrayToCommand(command);
}

public void addStoreQRCodeData(String content) {
byte[] command = new byte[]{29, 40, 107, (byte)((content.getBytes().length + 3) % 256), (byte)((content.getBytes().length + 3) / 256), 49, 80, 48};
this.addArrayToCommand(command);
byte[] bs = null;
if(!content.equals("")) {
try {
bs = content.getBytes("utf-8");
} catch (UnsupportedEncodingException var5) {
var5.printStackTrace();
}

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

}

public void addPrintQRCode() {
byte[] command = new byte[]{29, 40, 107, 3, 0, 49, 81, 48};
this.addArrayToCommand(command);
}

public void addQueryPrinterStatus() {
byte[] command = new byte[]{29, 114, 1};
this.addArrayToCommand(command);
}

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

public static enum HRI_POSITION {
NO_PRINT(0),
ABOVE(1),
BELOW(2),
ABOVE_AND_BELOW(3);

private final int value;

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

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

public static enum HEIGHT_ZOOM {
MUL_1(0),
MUL_2(1),
MUL_3(2),
MUL_4(3),
MUL_5(4),
MUL_6(5),
MUL_7(6),
MUL_8(7);

private final int value;

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

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

public static enum WIDTH_ZOOM {
MUL_1(0),
MUL_2(16),
MUL_3(32),
MUL_4(48),
MUL_5(64),
MUL_6(80),
MUL_7(96),
MUL_8(112);

private final int value;

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

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

public static enum CODEPAGE {
PC437(0),
KATAKANA(1),
PC850(2),
PC860(3),
PC863(4),
PC865(5),
WEST_EUROPE(6),
GREEK(7),
HEBREW(8),
EAST_EUROPE(9),
IRAN(10),
WPC1252(16),
PC866(17),
PC852(18),
PC858(19),
IRANII(20),
LATVIAN(21),
ARABIC(22),
PT151(23),
PC747(24),
WPC1257(25),
VIETNAM(27),
PC864(28),
PC1001(29),
UYGUR(30),
THAI(255);

private final int value;

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

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

public static enum JUSTIFICATION {
LEFT(0),
CENTER(1),
RIGHT(2);

private final int value;

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

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

public static enum CHARACTER_SET {
USA(0),
FRANCE(1),
GERMANY(2),
UK(3),
DENMARK_I(4),
SWEDEN(5),
ITALY(6),
SPAIN_I(7),
JAPAN(8),
NORWAY(9),
DENMARK_II(10),
SPAIN_II(11),
LATIN_AMERCIA(12),
KOREAN(13),
SLOVENIA(14),
CHINA(15);

private final int value;

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

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

public static enum FONT {
FONTA(0),
FONTB(1);

private final int value;

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

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

public static enum UNDERLINE_MODE {
OFF(0),
UNDERLINE_1DOT(1),
UNDERLINE_2DOT(2);

private final int value;

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

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

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;
}
}

public static enum STATUS {
PRINTER_STATUS(1),
PRINTER_OFFLINE(2),
PRINTER_ERROR(3),
PRINTER_PAPER(4);

private final int value;

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

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

TSC命令封装类

命令具体参考TSC命令文档

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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.xx.yy.gpbt;

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

import com.masget.rcyltms.gpbt.EscCommand.ENABLE;
import com.masget.rcyltms.gpbt.LabelCommand;
import com.masget.rcyltms.gpbt.GpUtils;
import java.io.UnsupportedEncodingException;
import java.util.Vector;

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) {
String str = "GAP " + gap + " mm," + 0 + " mm" + "\r\n";
this.addStrToCommand(str);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

public void addBar(int x, int y, int width, int height) {
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) {
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) {
int narrow = 2;
int width = 2;
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) {
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 = GpUtils.toGrayscale(b);
Bitmap rszBitmap = GpUtils.resizeImage(grayBitmap, width, height);
byte[] src = GpUtils.bitmapToBWPix(rszBitmap);
height = src.length / width;
width /= 8;
String str = "BITMAP " + x + "," + y + "," + width + "," + height + "," + mode.getValue() + ",";
this.addStrToCommand(str);
byte[] codecontent = GpUtils.pixToLabelCmd(src);

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

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

}

public void addBitmapByMethod(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 rszBitmap = GpUtils.resizeImage(b, width, height);
Bitmap grayBitmap = GpUtils.filter(rszBitmap, width, height);
byte[] src = GpUtils.bitmapToBWPix(grayBitmap);
height = src.length / width;
width /= 8;
String str = "BITMAP " + x + "," + y + "," + width + "," + height + "," + mode.getValue() + ",";
this.addStrToCommand(str);
byte[] codecontent = GpUtils.pixToLabelCmd(src);

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

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

}

public void addBitmap(int x, int y, int nWidth, Bitmap bmp) {
if(bmp != null) {
int width = (nWidth + 7) / 8 * 8;
int height = bmp.getHeight() * width / bmp.getWidth();
Log.d("BMP", "bmp.getWidth() " + bmp.getWidth());
Bitmap rszBitmap = GpUtils.resizeImage(bmp, width, height);
byte[] bytes = GpUtils.printTscDraw(x, y, LabelCommand.BITMAP_MODE.OVERWRITE, rszBitmap);
for(int i = 0; i < bytes.length; ++i) {
this.Command.add(Byte.valueOf(bytes[i]));
}

this.addStrToCommand("\r\n");
}

}

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

public void addReverse(int x, int y, int xwidth, int yheight) {
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) {
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() {
byte a = 27;
byte b = 33;
byte c = 63;
this.Command.add(Byte.valueOf(a));
this.Command.add(Byte.valueOf(b));
this.Command.add(Byte.valueOf(c));
}

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

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

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

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

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

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

}

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

public void addCutter(ENABLE enable) {
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) {
String str = "SET REPRINT " + enable.getValue() + "\r\n";
this.addStrToCommand(str);
}

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

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

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

public void addQueryPrinterStatus(LabelCommand.RESPONSE_MODE mode) {
String str = "SET RESPONSE " + mode.getValue() + "\r\n";
this.addStrToCommand(str);
}

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

public static enum RESPONSE_MODE {
ON("ON"),
OFF("OFF"),
BATCH("BATCH");

private final String value;

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

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

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 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 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 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"),
FONT_9("9"),
FONT_10("10"),
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 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 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 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 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 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 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 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 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;
}
}

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;
}
}
}

命令封装工具类

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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.xx.yy.gpbt;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Environment;
import com.masget.rcyltms.gpbt.LabelCommand.BITMAP_MODE;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GpUtils {
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[][]{{0, 0, 0}, {255, 255, 255}};
private static int method = 1;
public static final int FLOYD_STEINBERG_DITHER = 1;
public static final int ATKINSON_DITHER = 2;

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, 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;
}

public 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;
}

public 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);

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

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

for(int j = 0; k < arrayOfByte.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]);
arrayOfByte[k] = (byte)(~temp);
j += 8;
}

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

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];
// int index = false;

for(int row = 0; row < height; ++row) {
for(int col = 0; col < width; ++col) {
int index = row * width + col;
int r1 = inPixels[index] >> 16 & 255;
int g1 = inPixels[index] >> 8 & 255;
int b1 = inPixels[index] & 255;
int cIndex = getCloseColor(r1, g1, b1);
outPixels[index] = -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 e2 = 0.3125F;
float e3 = 0.1875F;
float e4 = 0.0625F;
rgb4 = getPixel(inPixels, width, height, col + 1, row, e1, ergb);
rgb5 = getPixel(inPixels, width, height, col, row + 1, e2, ergb);
rgb6 = getPixel(inPixels, width, height, col - 1, row + 1, e3, ergb);
//int[] rgb4 = getPixel(inPixels, width, height, col + 1, row + 1, e4, ergb);
rgb4 = getPixel(inPixels, width, height, col + 1, row + 1, e4, ergb);
setPixel(inPixels, width, height, col + 1, row, rgb4);
setPixel(inPixels, width, height, col, row + 1, rgb5);
setPixel(inPixels, width, height, col - 1, row + 1, rgb6);
setPixel(inPixels, width, height, col + 1, row + 1, rgb4);
} else {
if(method != 2) {
throw new IllegalArgumentException("Not Supported Dither Mothed!!");
}

e1 = 0.125F;
int[] rgb1 = getPixel(inPixels, width, height, col + 1, row, e1, ergb);
int[] rgb2 = getPixel(inPixels, width, height, col + 2, row, e1, ergb);
int[] rgb3 = getPixel(inPixels, width, height, col - 1, row + 1, e1, ergb);
rgb4 = getPixel(inPixels, width, height, col, row + 1, e1, ergb);
rgb5 = getPixel(inPixels, width, height, col + 1, row + 1, e1, ergb);
rgb6 = getPixel(inPixels, width, height, col, row + 2, e1, ergb);
setPixel(inPixels, width, height, col + 1, row, rgb1);
setPixel(inPixels, width, height, col + 2, row, rgb2);
setPixel(inPixels, width, height, col - 1, row + 1, rgb3);
setPixel(inPixels, width, height, col, row + 1, rgb4);
setPixel(inPixels, width, height, col + 1, row + 1, rgb5);
setPixel(inPixels, width, height, col, row + 2, rgb6);
}
}
}

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

public static byte[] printEscDraw(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
byte[] bitbuf = new byte[width / 8];
byte[] imgbuf = new byte[width / 8 * height + 8];
imgbuf[0] = 29;
imgbuf[1] = 118;
imgbuf[2] = 48;
imgbuf[3] = 0;
imgbuf[4] = (byte)(width / 8);
imgbuf[5] = 0;
imgbuf[6] = (byte)(height % 256);
imgbuf[7] = (byte)(height / 256);
int s = 7;

for(int i = 0; i < height; ++i) {
int k;
for(k = 0; k < width / 8; ++k) {
int c0 = bitmap.getPixel(k * 8, i);
byte p0;
if(c0 != -1 && c0 != 0) {
p0 = 1;
} else {
p0 = 0;
}

int c1 = bitmap.getPixel(k * 8 + 1, i);
byte p1;
if(c1 == -1) {
p1 = 0;
} else {
p1 = 1;
}

int c2 = bitmap.getPixel(k * 8 + 2, i);
byte p2;
if(c2 == -1) {
p2 = 0;
} else {
p2 = 1;
}

int c3 = bitmap.getPixel(k * 8 + 3, i);
byte p3;
if(c3 == -1) {
p3 = 0;
} else {
p3 = 1;
}

int c4 = bitmap.getPixel(k * 8 + 4, i);
byte p4;
if(c4 == -1) {
p4 = 0;
} else {
p4 = 1;
}

int c5 = bitmap.getPixel(k * 8 + 5, i);
byte p5;
if(c5 == -1) {
p5 = 0;
} else {
p5 = 1;
}

int c6 = bitmap.getPixel(k * 8 + 6, i);
byte p6;
if(c6 == -1) {
p6 = 0;
} else {
p6 = 1;
}

int c7 = bitmap.getPixel(k * 8 + 7, i);
byte p7;
if(c7 == -1) {
p7 = 0;
} else {
p7 = 1;
}

int value = p0 * 128 + p1 * 64 + p2 * 32 + p3 * 16 + p4 * 8 + p5 * 4 + p6 * 2 + p7;
bitbuf[k] = (byte)value;
}

for(k = 0; k < width / 8; ++k) {
++s;
imgbuf[s] = bitbuf[k];
}
}

return imgbuf;
}

public static byte[] printTscDraw(int x, int y, BITMAP_MODE mode, Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
byte[] bitbuf = new byte[width / 8];
String str = "BITMAP " + x + "," + y + "," + width / 8 + "," + height + "," + mode.getValue() + ",";
byte[] strPrint = null;

try {
strPrint = str.getBytes("GB2312");
} catch (UnsupportedEncodingException var30) {
var30.printStackTrace();
}

byte[] imgbuf = new byte[width / 8 * height + strPrint.length + 8];

int s;
for(s = 0; s < strPrint.length; ++s) {
imgbuf[s] = strPrint[s];
}

s = strPrint.length - 1;

for(int i = 0; i < height; ++i) {
int k;
for(k = 0; k < width / 8; ++k) {
int c0 = bitmap.getPixel(k * 8, i);
byte p0;
if(c0 == -1) {
p0 = 1;
} else {
p0 = 0;
}

int c1 = bitmap.getPixel(k * 8 + 1, i);
byte p1;
if(c1 == -1) {
p1 = 1;
} else {
p1 = 0;
}

int c2 = bitmap.getPixel(k * 8 + 2, i);
byte p2;
if(c2 == -1) {
p2 = 1;
} else {
p2 = 0;
}

int c3 = bitmap.getPixel(k * 8 + 3, i);
byte p3;
if(c3 == -1) {
p3 = 1;
} else {
p3 = 0;
}

int c4 = bitmap.getPixel(k * 8 + 4, i);
byte p4;
if(c4 == -1) {
p4 = 1;
} else {
p4 = 0;
}

int c5 = bitmap.getPixel(k * 8 + 5, i);
byte p5;
if(c5 == -1) {
p5 = 1;
} else {
p5 = 0;
}

int c6 = bitmap.getPixel(k * 8 + 6, i);
byte p6;
if(c6 == -1) {
p6 = 1;
} else {
p6 = 0;
}

int c7 = bitmap.getPixel(k * 8 + 7, i);
byte p7;
if(c7 == -1) {
p7 = 1;
} else {
p7 = 0;
}

int value = p0 * 128 + p1 * 64 + p2 * 32 + p3 * 16 + p4 * 8 + p5 * 4 + p6 * 2 + p7;
bitbuf[k] = (byte)value;
}

for(k = 0; k < width / 8; ++k) {
++s;
imgbuf[s] = bitbuf[k];
}
}

return imgbuf;
}

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

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

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

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

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();
}

public 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();
}

public 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());
List<Integer> list = new ArrayList(Arrays.asList(originUnicode));
List<Integer> list1 = Hyphen(list);
list = 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;
}

public 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;
}

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

}

public 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) {
// int flag = false;
List<Integer> outputlist = new ArrayList();
Map<Integer, Integer[]> 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 flag;
if(i == 0) {
inSet1 = false;
inSet2 = compare((Integer)inputlist.get(i + 1), 2);
flag = Flag(inSet1, inSet2);
} else if(i == inputlist.size() - 1) {
inSet1 = compare((Integer)inputlist.get(i - 1), 1);
inSet2 = false;
flag = Flag(inSet1, inSet2);
} else {
inSet1 = compare((Integer)inputlist.get(i - 1), 1);
inSet2 = compare((Integer)inputlist.get(i + 1), 2);
flag = Flag(inSet1, inSet2);
}

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

return outputlist;
}

static boolean compare(Integer input, int i) {
List<Integer[]> 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 static void setMethod(int method) {
method = method;
}
}

其他类的一些方法

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
package com.xx.yy.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;

import java.io.ByteArrayOutputStream;

/**
* Created by Administrator on 2018/11/7.
*/
public class PictureUtils {
/**
* base64字符串转位图
* @param base64String
* @return
*/
public static Bitmap base64StringToBitmap(String base64String) {
Bitmap bitmap = null;
String base64Value = "";
try {
String[] strArr =base64String.split(",");
if(strArr.length==1){
base64Value = strArr[0];
} else if(strArr.length==2){
base64Value = strArr[1];
}
byte[] bitmapArray = Base64.decode(base64Value, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}

/**
* 返回base64字符串
* @param bitmap
* @return
*/
public static String bitmaptoBase64String(Bitmap bitmap) {
//将Bitmap转换成字符串
String string = null;
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
byte[] bytes = bStream.toByteArray();
string = Base64.encodeToString(bytes, Base64.DEFAULT);
return string;
}
}

结语

以上就是使用ESCTSC命令做打印的Android源码

有些打印机只支持ESC,有些只支持TSC,也有两种命令都支持的,两种都支持的可能要在打印机上做模式切换,不然有可能打印不执行,要么就是乱码