react-native-printer-imin 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1658 +1,2052 @@
1
- package com.printerimin;
2
-
3
-
4
- import com.bumptech.glide.Glide;
5
- import com.bumptech.glide.load.engine.DiskCacheStrategy;
6
-
7
- import android.graphics.Bitmap;
8
- import android.graphics.Typeface;
9
- import android.os.RemoteException;
10
- import android.graphics.BitmapFactory;
11
- import android.os.Build;
12
- import android.util.Log;
13
-
14
- import androidx.annotation.NonNull;
15
- import androidx.annotation.Nullable;
16
-
17
- import com.facebook.react.bridge.Arguments;
18
- import com.facebook.react.bridge.Promise;
19
- import com.facebook.react.bridge.ReactApplicationContext;
20
- import com.facebook.react.bridge.ReactContext;
21
- import com.facebook.react.bridge.ReactContextBaseJavaModule;
22
- import com.facebook.react.bridge.ReactMethod;
23
- import com.facebook.react.bridge.ReadableArray;
24
- import com.facebook.react.bridge.ReadableMap;
25
- import com.facebook.react.bridge.WritableMap;
26
-
27
- import com.facebook.react.module.annotations.ReactModule;
28
- import com.facebook.react.modules.core.DeviceEventManagerModule;
29
-
30
-
31
- import com.imin.printer.INeoPrinterCallback;
32
- import com.imin.printer.PrinterHelper;
33
- import com.imin.printerlib.Callback;
34
- import com.imin.printerlib.IminPrintUtils;
35
-
36
- import java.util.ArrayList;
37
- import java.util.Arrays;
38
- import java.util.HashMap;
39
- import java.util.List;
40
- import java.util.Map;
41
-
42
- import org.json.JSONArray;
43
- import org.json.JSONObject;
44
-
45
- import org.json.JSONException;
46
-
47
-
48
- import java.lang.RuntimeException;
49
-
50
- import android.content.Context;
51
- import android.content.Intent;
52
- import android.content.IntentFilter;
53
- import android.content.BroadcastReceiver;
54
-
55
- @ReactModule(name = PrinterIminModule.NAME)
56
- public class PrinterIminModule extends ReactContextBaseJavaModule {
57
- private final ReactApplicationContext reactContext;
58
- public static final String NAME = "PrinterImin";
59
- private IminPrintUtils iminPrintUtils;
60
- private String[] modelArry = {"W27_Pro", "I23M01", "I23M02", "I23D01", "D4-503 Pro", "D4-504 Pro", "D4-505 Pro", "MS2-11", "MS2-12", "MS1-15"};
61
- private static final String ACTION_PRITER_STATUS_CHANGE = "com.imin.printerservice.PRITER_STATUS_CHANGE";
62
- private static final String ACTION_POGOPIN_STATUS_CHANGE = "com.imin.printerservice.PRITER_CONNECT_STATUS_CHANGE";
63
- private String sdkVersion = "1.0.0";
64
- private static final String ACTION_PRITER_STATUS = "status";
65
- private IminPrintUtils.PrintConnectType connectType = IminPrintUtils.PrintConnectType.USB;
66
- private static final String TAG = "IminPrinterReactNativePlugin";
67
- private BroadcastReceiver mBroadcastReceiver;
68
-
69
- public PrinterIminModule(ReactApplicationContext reactContext) {
70
- super(reactContext);
71
- this.reactContext = reactContext;
72
- List<String> modelList = Arrays.asList(modelArry);
73
- if (modelList.contains(Build.MODEL)) {
74
- //初始化 2.0 SDK。
75
- PrinterHelper.getInstance().initPrinterService(reactContext);
76
- sdkVersion = "2.0.0";
77
- } else {
78
- //初始化 1.0 SDK
79
-
80
- iminPrintUtils = IminPrintUtils.getInstance(reactContext);
81
- String deviceModel = Utils.getInstance().getModel();
82
- if (deviceModel.contains("M2-203") || deviceModel.contains("M2-202") || deviceModel.contains("M2-Pro")) {
83
- connectType = IminPrintUtils.PrintConnectType.SPI;
84
- } else {
85
- connectType = IminPrintUtils.PrintConnectType.USB;
86
- }
87
- iminPrintUtils.setIsOpenLog(IminPrintUtils.isOpenLog == 1 ? 0 : 1);
88
- iminPrintUtils.resetDevice();
89
- sdkVersion = "1.0.0";
90
- }
91
- initializeBroadcastReceiver();
92
- }
93
-
94
- @Override
95
- @NonNull
96
- public String getName() {
97
- return NAME;
98
- }
99
-
100
- private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
101
-
102
- try {
103
- reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
104
- } catch (RuntimeException e) {
105
- Log.e(TAG, "java.lang.RuntimeException: Trying to invoke JS before CatalystInstance has been set!", e);
106
- }
107
- }
108
-
109
- @ReactMethod
110
- public void initPrinter(final Promise promise) {
111
- try {
112
- if (iminPrintUtils != null) {
113
- iminPrintUtils.initPrinter(connectType);
114
- } else {
115
- PrinterHelper.getInstance().initPrinter(reactContext.getPackageName(), null);
116
- }
117
- promise.resolve(true);
118
- } catch (Exception e) {
119
- promise.reject("ININTPRINTER_FAILED", e.getMessage());
120
- }
121
- }
122
-
123
- @ReactMethod
124
- public void getPrinterStatus(final Promise promise) {
125
- try {
126
- WritableMap payload = Arguments.createMap();
127
- if (iminPrintUtils != null) {
128
- if (connectType.equals(IminPrintUtils.PrintConnectType.SPI)) {
129
- iminPrintUtils.getPrinterStatus(connectType, new Callback() {
130
- @Override
131
- public void callback(int status) {
132
- payload.putString("message", Utils.getInstance().getPrinterStatusText(status));
133
- payload.putString("code", String.format("%d", status));
134
- promise.resolve(payload);
135
- }
136
- });
137
- } else {
138
- int status = iminPrintUtils.getPrinterStatus(connectType);
139
- payload.putString("message", Utils.getInstance().getPrinterStatusText(status));
140
- payload.putString("code", String.format("%d", status));
141
- promise.resolve(payload);
142
- }
143
- } else {
144
- int status = PrinterHelper.getInstance().getPrinterStatus();
145
- payload.putString("message", Utils.getInstance().getPrinterStatusText(status));
146
- payload.putString("code", String.format("%d", status));
147
- promise.resolve(payload);
148
- }
149
- payload.putString("message", "sdsd");
150
- payload.putString("code", "1");
151
- promise.resolve(payload);
152
- } catch (Exception e) {
153
- promise.reject("getPrinterStatus_failed", e.getMessage());
154
- }
155
-
156
- }
157
-
158
- @ReactMethod
159
- public void setTextSize(int size, final Promise promise) {
160
- try {
161
- if (iminPrintUtils != null) {
162
- iminPrintUtils.setTextSize(size);
163
- }
164
- promise.resolve(null);
165
- } catch (Exception e) {
166
- promise.reject("setTextSize_failed", e.getMessage());
167
- }
168
- }
169
-
170
- @ReactMethod
171
- public void setTextTypeface(int font, final Promise promise) {
172
- try {
173
- if (iminPrintUtils != null) {
174
- switch (font) {
175
- case 1:
176
- iminPrintUtils.setTextTypeface(Typeface.MONOSPACE);
177
- break;
178
- case 2:
179
- iminPrintUtils.setTextTypeface(Typeface.DEFAULT_BOLD);
180
- break;
181
- case 3:
182
- iminPrintUtils.setTextTypeface(Typeface.SANS_SERIF);
183
- break;
184
- case 4:
185
- iminPrintUtils.setTextTypeface(Typeface.SERIF);
186
- break;
187
- default:
188
- iminPrintUtils.setTextTypeface(Typeface.DEFAULT);
189
- break;
190
- }
191
- }
192
- promise.resolve(null);
193
- } catch (Exception e) {
194
- promise.reject("setTextTypeface_failed", e.getMessage());
195
- }
196
- }
197
-
198
- @ReactMethod
199
- public void setTextStyle(int style, final Promise promise) {
200
- try {
201
- if (iminPrintUtils != null) {
202
- iminPrintUtils.setTextStyle(style);
203
- }
204
- promise.resolve(null);
205
- } catch (Exception e) {
206
- promise.reject("setTextStyle_failed", e.getMessage());
207
- }
208
- }
209
-
210
- @ReactMethod
211
- public void setAlignment(int alignment, final Promise promise) {
212
- try {
213
- if (iminPrintUtils != null) {
214
- iminPrintUtils.setAlignment(alignment);
215
- }
216
- promise.resolve(null);
217
- } catch (Exception e) {
218
- promise.reject("setAlignment_failed", e.getMessage());
219
- }
220
- }
221
-
222
- @ReactMethod
223
- public void setTextLineSpacing(float space, final Promise promise) {
224
- try {
225
- if (iminPrintUtils != null) {
226
- iminPrintUtils.setTextLineSpacing(space);
227
- }
228
- promise.resolve(null);
229
- } catch (Exception e) {
230
- promise.reject("setTextLineSpacing_failed", e.getMessage());
231
- }
232
- }
233
-
234
- @ReactMethod
235
- public void printText(String text, final Promise promise) {
236
- try {
237
- if (iminPrintUtils != null) {
238
- iminPrintUtils.printText(text + "\n");
239
- } else {
240
- PrinterHelper.getInstance().printText(text + "\n", null);
241
- }
242
- promise.resolve(null);
243
- } catch (Exception e) {
244
- promise.reject("printText_failed", e.getMessage());
245
- }
246
- }
247
-
248
- @ReactMethod
249
- public void printAntiWhiteText(String text, final Promise promise) {
250
- try {
251
- if (iminPrintUtils != null) {
252
- iminPrintUtils.printAntiWhiteText(text);
253
- }
254
- promise.resolve(null);
255
- } catch (Exception e) {
256
- promise.reject("printAntiWhiteText_failed", e.getMessage());
257
- }
258
- }
259
-
260
- @ReactMethod
261
- public void setTextWidth(int width, final Promise promise) {
262
- try {
263
- if (iminPrintUtils != null) {
264
- iminPrintUtils.setTextWidth(width);
265
- }
266
- promise.resolve(null);
267
- } catch (Exception e) {
268
- promise.reject("setTextWidth_failed", e.getMessage());
269
- }
270
- }
271
-
272
- @ReactMethod
273
- public void printAndLineFeed(final Promise promise) {
274
- try {
275
- if (iminPrintUtils != null) {
276
- iminPrintUtils.printAndLineFeed();
277
- } else {
278
- PrinterHelper.getInstance().printAndLineFeed();
279
- }
280
- promise.resolve(null);
281
- } catch (Exception e) {
282
- promise.reject("printAndLineFeed_failed", e.getMessage());
283
- }
284
- }
285
-
286
- @ReactMethod
287
- public void printAndFeedPaper(int height, final Promise promise) {
288
- try {
289
- if (iminPrintUtils != null) {
290
- iminPrintUtils.printAndFeedPaper(height);
291
- } else {
292
- PrinterHelper.getInstance().printAndFeedPaper(height);
293
- }
294
- promise.resolve(null);
295
- } catch (Exception e) {
296
- promise.reject("printAndLineFeed_failed", e.getMessage());
297
- }
298
- }
299
-
300
- @ReactMethod
301
- public void printColumnsText(ReadableArray cols, final Promise promise) {
302
- try {
303
- String[] colsText = new String[cols.size()];
304
- int[] colsWidth = new int[cols.size()];
305
- int[] colsAlign = new int[cols.size()];
306
- int[] colsFontSize = new int[cols.size()];
307
- for (int i = 0; i < cols.size(); i++) {
308
- ReadableMap col = cols.getMap(i);
309
- String textColumn = col.getString("text");
310
- int widthColumn = col.getInt("width");
311
- int alignColumn = col.getInt("align");
312
- Log.d(TAG, "printColumnsText: colsText" + textColumn);
313
- int fontSizeColumn = col.getInt("fontSize");
314
- colsText[i] = textColumn;
315
- colsWidth[i] = widthColumn;
316
- colsAlign[i] = alignColumn;
317
- colsFontSize[i] = fontSizeColumn;
318
- }
319
- Log.d(TAG, "printColumnsText: colsText" + colsText);
320
- Log.d(TAG, "printColumnsText: colsWidth" + colsWidth);
321
- Log.d(TAG, "printColumnsText: colsAlign" + colsAlign);
322
- Log.d(TAG, "printColumnsText: colsFontSize" + colsFontSize);
323
- if (iminPrintUtils != null) {
324
- iminPrintUtils.printColumnsText(colsText, colsWidth, colsAlign, colsFontSize);
325
- } else {
326
- PrinterHelper.getInstance().printColumnsText(colsText, colsWidth, colsAlign, colsFontSize, null);
327
- }
328
- promise.resolve(null);
329
- } catch (Exception e) {
330
- promise.reject("printColumnsText_failed", e.getMessage());
331
- }
332
- }
333
-
334
- @ReactMethod
335
- public void setPageFormat(int style, final Promise promise) {
336
- try {
337
- if (iminPrintUtils != null) {
338
- iminPrintUtils.setPageFormat(style);
339
- } else {
340
- PrinterHelper.getInstance().setPageFormat(style);
341
- }
342
- promise.resolve(null);
343
- } catch (Exception e) {
344
- promise.reject("setPageFormat_failed", e.getMessage());
345
- }
346
- }
347
-
348
- @ReactMethod
349
- public void partialCut(final Promise promise) {
350
- try {
351
- if (iminPrintUtils != null) {
352
- iminPrintUtils.partialCut();
353
- } else {
354
- PrinterHelper.getInstance().partialCut();
355
- }
356
- promise.resolve(null);
357
- } catch (Exception e) {
358
- promise.reject("partialCut_failed", e.getMessage());
359
- }
360
- }
361
-
362
- @ReactMethod
363
- public void fullCut(final Promise promise) {
364
- try {
365
- if (iminPrintUtils == null) {
366
- PrinterHelper.getInstance().fullCut();
367
- }
368
- promise.resolve(null);
369
- } catch (Exception e) {
370
- promise.reject("fullCut_failed", e.getMessage());
371
- }
372
- }
373
-
374
- @ReactMethod
375
- public void printSingleBitmap(ReadableMap config, final Promise promise) {
376
- try {
377
- String url = config.getString("url");
378
- if (config.hasKey("width") && config.hasKey("height")) {
379
- int width = config.getInt("width");
380
- int height = config.getInt("height");
381
- Bitmap image = Glide.with(reactContext).asBitmap().load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit(width, height).get();
382
- if (image.isRecycled()) {
383
- return;
384
- }
385
- if (iminPrintUtils != null) {
386
- if (config.hasKey("align")) {
387
- int align = config.getInt("align");
388
- iminPrintUtils.printSingleBitmap(image, align);
389
- } else {
390
- iminPrintUtils.printSingleBitmap(image);
391
- }
392
- } else {
393
- if (config.hasKey("align")) {
394
- int align = config.getInt("align");
395
- PrinterHelper.getInstance().printBitmapWithAlign(image, align, null);
396
- } else {
397
- PrinterHelper.getInstance().printBitmap(image, null);
398
- }
399
- }
400
- } else {
401
- Bitmap image = Glide.with(reactContext).asBitmap().load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit().get();
402
- if (image.isRecycled()) {
403
- return;
404
- }
405
- if (iminPrintUtils != null) {
406
- if (config.hasKey("align")) {
407
- int align = config.getInt("align");
408
- iminPrintUtils.printSingleBitmap(image, align);
409
- } else {
410
- iminPrintUtils.printSingleBitmap(image);
411
- }
412
- } else {
413
- if (config.hasKey("align")) {
414
- int align = config.getInt("align");
415
- PrinterHelper.getInstance().printBitmapWithAlign(image, align, null);
416
- } else {
417
- PrinterHelper.getInstance().printBitmap(image, null);
418
- }
419
- }
420
- }
421
- promise.resolve(null);
422
- } catch (Exception e) {
423
- promise.reject("printSingleBitmap_failed", e.getMessage());
424
- }
425
- }
426
-
427
- @ReactMethod
428
- public void printMultiBitmap(ReadableMap config, final Promise promise) {
429
- try {
430
- ReadableArray urls = config.getArray("urls");
431
- ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
432
- if (config.hasKey("width") && config.hasKey("height")) {
433
- int width = config.getInt("width");
434
- int height = config.getInt("height");
435
- for (int i = 0; i < urls.size(); i++) {
436
- Bitmap image = Glide.with(reactContext).asBitmap().load(urls.getString(i)).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit(width, height).get();
437
- if (!image.isRecycled()) {
438
- bitmaps.add(image);
439
- }
440
- }
441
- if (iminPrintUtils != null) {
442
- if (config.hasKey("align")) {
443
- int align = config.getInt("align");
444
- iminPrintUtils.printMultiBitmap(bitmaps, align);
445
- } else {
446
- iminPrintUtils.printMultiBitmap(bitmaps, 0);
447
- }
448
- } else {
449
- if (config.hasKey("align")) {
450
- int align = config.getInt("align");
451
- PrinterHelper.getInstance().printMultiBitmapWithAlign(bitmaps, align, null);
452
- } else {
453
- PrinterHelper.getInstance().printMultiBitmap(bitmaps, null);
454
- }
455
- }
456
- } else {
457
- for (int i = 0; i < urls.size(); i++) {
458
- Bitmap image = Glide.with(reactContext).asBitmap().load(urls.getString(i)).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit().get();
459
- if (!image.isRecycled()) {
460
- bitmaps.add(image);
461
- }
462
- }
463
- if (iminPrintUtils != null) {
464
- if (config.hasKey("align")) {
465
- int align = config.getInt("align");
466
- iminPrintUtils.printMultiBitmap(bitmaps, align);
467
- } else {
468
- iminPrintUtils.printMultiBitmap(bitmaps, 0);
469
- }
470
- } else {
471
- if (config.hasKey("align")) {
472
- int align = config.getInt("align");
473
- PrinterHelper.getInstance().printMultiBitmapWithAlign(bitmaps, align, null);
474
- } else {
475
- PrinterHelper.getInstance().printMultiBitmap(bitmaps, null);
476
- }
477
- }
478
- }
479
- promise.resolve(null);
480
- } catch (Exception e) {
481
- promise.reject("printMultiBitmap_failed", e.getMessage());
482
- }
483
- }
484
-
485
- @ReactMethod
486
- public void printSingleBitmapBlackWhite(ReadableMap config, final Promise promise) {
487
- try {
488
- String url = config.getString("url");
489
- if (config.hasKey("width") && config.hasKey("height")) {
490
- int width = config.getInt("width");
491
- int height = config.getInt("height");
492
- Bitmap image = Glide.with(reactContext).asBitmap().load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit(width, height).get();
493
- if (image.isRecycled()) {
494
- return;
495
- }
496
- if (iminPrintUtils != null) {
497
- iminPrintUtils.printSingleBitmapBlackWhite(image);
498
- }
499
- } else {
500
- Bitmap image = Glide.with(reactContext).asBitmap().load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit().get();
501
- if (image.isRecycled()) {
502
- return;
503
- }
504
- if (iminPrintUtils != null) {
505
- iminPrintUtils.printSingleBitmapBlackWhite(image);
506
- }
507
- }
508
- promise.resolve(null);
509
- } catch (Exception e) {
510
- promise.reject("printSingleBitmapBlackWhite_failed", e.getMessage());
511
- }
512
- }
513
-
514
- @ReactMethod
515
- public void setQrCodeSize(int qrSize, final Promise promise) {
516
- try {
517
- if (iminPrintUtils != null) {
518
- iminPrintUtils.setQrCodeSize(qrSize);
519
- } else {
520
- PrinterHelper.getInstance().setQrCodeSize(qrSize);
521
- }
522
- promise.resolve(null);
523
- } catch (Exception e) {
524
- promise.reject("setQrCodeSize_failed", e.getMessage());
525
- }
526
- }
527
-
528
- @ReactMethod
529
- public void setLeftMargin(int margin, final Promise promise) {
530
- try {
531
- if (iminPrintUtils != null) {
532
- iminPrintUtils.setLeftMargin(margin);
533
- } else {
534
- PrinterHelper.getInstance().setLeftMargin(margin);
535
- }
536
- promise.resolve(null);
537
- } catch (Exception e) {
538
- promise.reject("setLeftMargin_failed", e.getMessage());
539
- }
540
- }
541
-
542
- @ReactMethod
543
- public void setBarCodeWidth(int width, final Promise promise) {
544
- try {
545
- if (iminPrintUtils != null) {
546
- iminPrintUtils.setBarCodeWidth(width);
547
- } else {
548
- PrinterHelper.getInstance().setBarCodeWidth(width);
549
- }
550
- promise.resolve(null);
551
- } catch (Exception e) {
552
- promise.reject("setBarCodeWidth_failed", e.getMessage());
553
- }
554
- }
555
-
556
- @ReactMethod
557
- public void setBarCodeHeight(int height, final Promise promise) {
558
- try {
559
- if (iminPrintUtils != null) {
560
- iminPrintUtils.setBarCodeHeight(height);
561
- } else {
562
- PrinterHelper.getInstance().setBarCodeHeight(height);
563
- }
564
- promise.resolve(null);
565
- } catch (Exception e) {
566
- promise.reject("setBarCodeHeight_failed", e.getMessage());
567
- }
568
- }
569
-
570
- @ReactMethod
571
- public void setBarCodeContentPrintPos(int position, final Promise promise) {
572
- try {
573
- if (iminPrintUtils != null) {
574
- iminPrintUtils.setBarCodeContentPrintPos(position);
575
- } else {
576
- PrinterHelper.getInstance().setBarCodeContentPrintPos(position);
577
- }
578
- promise.resolve(null);
579
- } catch (Exception e) {
580
- promise.reject("setBarCodeContentPrintPos_failed", e.getMessage());
581
- }
582
- }
583
-
584
- @ReactMethod
585
- public void printBarCode(ReadableMap config, final Promise promise) {
586
- try {
587
- String barCodeContent = config.getString("data");
588
- int barCodeType = config.getInt("type");
589
- if (iminPrintUtils != null) {
590
- if (config.hasKey("align")) {
591
- int barCodeAlign = config.getInt("align");
592
- iminPrintUtils.printBarCode(barCodeType, barCodeContent, barCodeAlign);
593
- } else {
594
- iminPrintUtils.printBarCode(barCodeType, barCodeContent);
595
- }
596
- } else {
597
- if (config.hasKey("align")) {
598
- int barCodeAlign = config.getInt("align");
599
- if (config.hasKey("position") && config.hasKey("height") && config.hasKey("width")) {
600
- int position = config.getInt("position");
601
- int height = config.getInt("height");
602
- int width = config.getInt("width");
603
- PrinterHelper.getInstance().printBarCodeWithFull(barCodeContent, barCodeType, width, height, barCodeAlign, position, null);
604
- } else {
605
- PrinterHelper.getInstance().printBarCodeWithAlign(barCodeContent, barCodeType, barCodeAlign, null);
606
- }
607
- } else {
608
- PrinterHelper.getInstance().printBarCode(barCodeContent, barCodeType, null);
609
- }
610
- }
611
- promise.resolve(null);
612
- } catch (Exception e) {
613
- promise.reject("printBarCode_failed", e.getMessage());
614
- }
615
-
616
- }
617
-
618
- @ReactMethod
619
- public void setDoubleQRSize(int size, final Promise promise) {
620
- try {
621
- if (iminPrintUtils != null) {
622
- iminPrintUtils.setDoubleQRSize(size);
623
- } else {
624
- PrinterHelper.getInstance().setDoubleQRSize(size);
625
- }
626
- promise.resolve(null);
627
- } catch (Exception e) {
628
- promise.reject("setDoubleQRSize_failed", e.getMessage());
629
- }
630
- }
631
-
632
- @ReactMethod
633
- public void setDoubleQR1Level(int level, final Promise promise) {
634
- try {
635
- if (iminPrintUtils != null) {
636
- iminPrintUtils.setDoubleQR1Level(level);
637
- } else {
638
- PrinterHelper.getInstance().setDoubleQR1Level(level);
639
- }
640
- promise.resolve(null);
641
- } catch (Exception e) {
642
- promise.reject("setDoubleQR1Level_failed", e.getMessage());
643
- }
644
- }
645
-
646
- @ReactMethod
647
- public void setDoubleQR2Level(int level, final Promise promise) {
648
- try {
649
- if (iminPrintUtils != null) {
650
- iminPrintUtils.setDoubleQR2Level(level);
651
- } else {
652
- PrinterHelper.getInstance().setDoubleQR2Level(level);
653
- }
654
- promise.resolve(null);
655
- } catch (Exception e) {
656
- promise.reject("setDoubleQR2Level_failed", e.getMessage());
657
- }
658
- }
659
-
660
- @ReactMethod
661
- public void setDoubleQR1MarginLeft(int leftMargin, final Promise promise) {
662
- try {
663
- if (iminPrintUtils != null) {
664
- iminPrintUtils.setDoubleQR1MarginLeft(leftMargin);
665
- } else {
666
- PrinterHelper.getInstance().setDoubleQR1MarginLeft(leftMargin);
667
- }
668
- promise.resolve(null);
669
- } catch (Exception e) {
670
- promise.reject("setDoubleQR1MarginLeft_failed", e.getMessage());
671
- }
672
- }
673
-
674
- @ReactMethod
675
- public void setDoubleQR2MarginLeft(int leftMargin, final Promise promise) {
676
- try {
677
- if (iminPrintUtils != null) {
678
- iminPrintUtils.setDoubleQR2MarginLeft(leftMargin);
679
- } else {
680
- PrinterHelper.getInstance().setDoubleQR2MarginLeft(leftMargin);
681
- }
682
- promise.resolve(null);
683
- } catch (Exception e) {
684
- promise.reject("setDoubleQR2MarginLeft_failed", e.getMessage());
685
- }
686
- }
687
-
688
- @ReactMethod
689
- public void setQrCodeErrorCorrectionLev(int level, final Promise promise) {
690
- try {
691
- if (iminPrintUtils != null) {
692
- iminPrintUtils.setQrCodeErrorCorrectionLev(level);
693
- } else {
694
- PrinterHelper.getInstance().setQrCodeErrorCorrectionLev(level);
695
- }
696
- promise.resolve(null);
697
- } catch (Exception e) {
698
- promise.reject("setQrCodeErrorCorrectionLev_failed", e.getMessage());
699
- }
700
- }
701
-
702
- @ReactMethod
703
- public void setDoubleQR1Version(int version, final Promise promise) {
704
- try {
705
- if (iminPrintUtils != null) {
706
- iminPrintUtils.setDoubleQR1Version(version);
707
- } else {
708
- PrinterHelper.getInstance().setDoubleQR1Version(version);
709
- }
710
- promise.resolve(null);
711
- } catch (Exception e) {
712
- promise.reject("setDoubleQR1Version_failed", e.getMessage());
713
- }
714
- }
715
-
716
- @ReactMethod
717
- public void setDoubleQR2Version(int version, final Promise promise) {
718
- try {
719
- if (iminPrintUtils != null) {
720
- iminPrintUtils.setDoubleQR2Version(version);
721
- } else {
722
- PrinterHelper.getInstance().setDoubleQR2Version(version);
723
- }
724
- promise.resolve(null);
725
- } catch (Exception e) {
726
- promise.reject("setDoubleQR2Version_failed", e.getMessage());
727
- }
728
- }
729
-
730
- @ReactMethod
731
- public void printQrCode(ReadableMap config, final Promise promise) {
732
- try {
733
- String qrStr = config.getString("data");
734
- if (config.hasKey("align")) {
735
- int align = config.getInt("align");
736
- if (iminPrintUtils != null) {
737
- iminPrintUtils.printQrCode(qrStr, align);
738
- } else {
739
- if (config.hasKey("qrSize") && config.hasKey("level")) {
740
- int qrSize = config.getInt("qrSize");
741
- int qrLevel = config.getInt("level");
742
- PrinterHelper.getInstance().printQRCodeWithFull(qrStr, qrSize, qrLevel, align, null);
743
- } else {
744
- PrinterHelper.getInstance().printQrCodeWithAlign(qrStr, align, null);
745
- }
746
- }
747
- } else {
748
- if (iminPrintUtils != null) {
749
- iminPrintUtils.printQrCode(qrStr);
750
- } else {
751
- PrinterHelper.getInstance().printQrCode(qrStr, null);
752
- }
753
- }
754
- promise.resolve(null);
755
- } catch (Exception e) {
756
- promise.reject("printQrCode_failed", e.getMessage());
757
- }
758
- }
759
-
760
- @ReactMethod
761
- public void printDoubleQR(ReadableMap config, final Promise promise) {
762
- try {
763
- String qrCode1Text = config.getString("qrCode1Text");
764
- String qrCode2Text = config.getString("qrCode2Text");
765
- if (iminPrintUtils != null) {
766
- iminPrintUtils.printDoubleQR(qrCode1Text, qrCode2Text);
767
- } else {
768
- PrinterHelper.getInstance().printDoubleQR(qrCode1Text, qrCode2Text, null);
769
- }
770
- promise.resolve(null);
771
- } catch (Exception e) {
772
- promise.reject("printDoubleQR_failed", e.getMessage());
773
- }
774
- }
775
-
776
- @ReactMethod
777
- public void openCashBox(final Promise promise) {
778
- try {
779
- if (iminPrintUtils != null) {
780
- Utils.getInstance().opencashBox();
781
- } else {
782
- PrinterHelper.getInstance().openDrawer();
783
- }
784
- promise.resolve(null);
785
- } catch (Exception e) {
786
- promise.reject("openCashBox_failed", e.getMessage());
787
- }
788
-
789
- }
790
-
791
-
792
- @ReactMethod
793
- public void setInitIminPrinter(boolean isDefault, final Promise promise) {
794
- try {
795
- if (iminPrintUtils != null) {
796
- iminPrintUtils.setInitIminPrinter(isDefault);
797
- }
798
- promise.resolve(null);
799
- } catch (Exception e) {
800
- promise.reject("setInitIminPrinter_failed", e.getMessage());
801
- }
802
- }
803
-
804
- @ReactMethod
805
- public void resetDevice(final Promise promise) {
806
- try {
807
- if (iminPrintUtils != null) {
808
- iminPrintUtils.resetDevice();
809
- }
810
- promise.resolve(null);
811
- } catch (Exception e) {
812
- promise.reject("resetDevice_failed", e.getMessage());
813
- }
814
- }
815
-
816
- @ReactMethod
817
- public void printerSelfChecking(final Promise promise) {
818
- try {
819
- if (iminPrintUtils == null) {
820
- PrinterHelper.getInstance().printerSelfChecking(null);
821
- }
822
- promise.resolve(null);
823
- } catch (Exception e) {
824
- promise.reject("printerSelfChecking_failed", e.getMessage());
825
- }
826
- }
827
-
828
- @ReactMethod
829
- public void openLogs(int open, final Promise promise) {
830
- try {
831
- if (iminPrintUtils != null) {
832
- iminPrintUtils.setIsOpenLog(open);
833
- }
834
- promise.resolve(null);
835
- } catch (Exception e) {
836
- promise.reject("setIsOpenLog_failed", e.getMessage());
837
- }
838
- }
839
-
840
- @ReactMethod
841
- public void sendRAWDataHexStr(String byteStr, final Promise promise) {
842
- try {
843
- if (iminPrintUtils != null) {
844
- iminPrintUtils.sendRAWData(byteStr);
845
- }
846
- promise.resolve(null);
847
- } catch (Exception e) {
848
- promise.reject("sendRAWDataHexStr_failed", e.getMessage());
849
- }
850
- }
851
-
852
- @ReactMethod
853
- public void printSingleBitmapColorChart(ReadableMap config, final Promise promise) {
854
- try {
855
- String url = config.getString("url");
856
- if (config.hasKey("width") && config.hasKey("height")) {
857
- int width = config.getInt("width");
858
- int height = config.getInt("height");
859
- Bitmap image = Glide.with(reactContext).asBitmap().load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit(width, height).get();
860
- if (image.isRecycled()) {
861
- return;
862
- }
863
- if (iminPrintUtils == null) {
864
- if (config.hasKey("align")) {
865
- int align = config.getInt("align");
866
- PrinterHelper.getInstance().printBitmapColorChartWithAlign(image, align, null);
867
- } else {
868
- PrinterHelper.getInstance().printBitmapColorChart(image, null);
869
- }
870
- }
871
- } else {
872
- Bitmap image = Glide.with(reactContext).asBitmap().load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit().get();
873
- if (image.isRecycled()) {
874
- return;
875
- }
876
- if (iminPrintUtils == null) {
877
- if (config.hasKey("align")) {
878
- int align = config.getInt("align");
879
- PrinterHelper.getInstance().printBitmapColorChartWithAlign(image, align, null);
880
- } else {
881
- PrinterHelper.getInstance().printBitmapColorChart(image, null);
882
- }
883
- }
884
- }
885
- promise.resolve(null);
886
- } catch (Exception e) {
887
- promise.reject("printSingleBitmap_failed", e.getMessage());
888
- }
889
- }
890
-
891
-
892
- @ReactMethod
893
- public void sendRAWData(ReadableArray bytes, final Promise promise) {
894
- try {
895
- // 处理接收到的 Uint8Array 数据
896
- byte[] rawData = new byte[bytes.size()];
897
- for (int i = 0; i < bytes.size(); i++) {
898
- rawData[i] = (byte) bytes.getInt(i);
899
- }
900
- if (iminPrintUtils == null) {
901
- PrinterHelper.getInstance().sendRAWData(rawData, null);
902
- }
903
- promise.resolve(null);
904
- } catch (Exception e) {
905
- promise.reject("sendRAWData_failed", e.getMessage());
906
- }
907
- }
908
-
909
- @ReactMethod
910
- public void printColumnsString(ReadableArray cols, final Promise promise) {
911
- try {
912
- String[] colsText = new String[cols.size()];
913
- int[] colsWidth = new int[cols.size()];
914
- int[] colsAlign = new int[cols.size()];
915
- int[] colsFontSize = new int[cols.size()];
916
- for (int i = 0; i < cols.size(); i++) {
917
- ReadableMap col = cols.getMap(i);
918
- String textColumn = col.getString("text");
919
- int widthColumn = col.getInt("width");
920
- int alignColumn = col.getInt("align");
921
- int fontSizeColumn = col.getInt("fontSize");
922
- colsText[i] = textColumn;
923
- colsWidth[i] = widthColumn;
924
- colsAlign[i] = alignColumn;
925
- colsFontSize[i] = fontSizeColumn;
926
- }
927
- if (iminPrintUtils == null) {
928
- PrinterHelper.getInstance().printColumnsString(colsText, colsWidth, colsAlign, colsFontSize, null);
929
- }
930
- promise.resolve(null);
931
- } catch (Exception e) {
932
- promise.reject("printColumnsString_failed", e.getMessage());
933
- }
934
- }
935
-
936
- @ReactMethod
937
- public void unBindService(final Promise promise) {
938
- try {
939
- if (iminPrintUtils == null) {
940
- PrinterHelper.getInstance().deInitPrinterService(reactContext);
941
- }
942
- } catch (Exception e) {
943
- promise.reject("unBindService_failed", e.getMessage());
944
- }
945
- }
946
-
947
- @ReactMethod
948
- public void initPrinterParams(final Promise promise) {
949
- try {
950
- if (iminPrintUtils == null) {
951
- PrinterHelper.getInstance().initPrinterParams();
952
- }
953
- promise.resolve(null);
954
- } catch (Exception e) {
955
- promise.reject("initPrinterParams_failed", e.getMessage());
956
- }
957
- }
958
-
959
- @ReactMethod
960
- public void getFontCodepage(final Promise promise) {
961
- try {
962
- if (iminPrintUtils == null) {
963
- List<String> fontCodepage = PrinterHelper.getInstance().getFontCodepage();
964
- promise.resolve(fontCodepage);
965
- } else {
966
- promise.resolve(null);
967
- }
968
- } catch (Exception e) {
969
- promise.reject("getFontCodepage_failed", e.getMessage());
970
- }
971
- }
972
-
973
- @ReactMethod
974
- public void setFontCodepage(int codepage, final Promise promise) {
975
- try {
976
- if (iminPrintUtils == null) {
977
- PrinterHelper.getInstance().setFontCodepage(codepage);
978
- }
979
- promise.resolve(null);
980
- } catch (Exception e) {
981
- promise.reject("setFontCodepage_failed", e.getMessage());
982
- }
983
- }
984
-
985
- @ReactMethod
986
- public void getCurCodepage(final Promise promise) {
987
- try {
988
- if (iminPrintUtils == null) {
989
- String curCodepage = PrinterHelper.getInstance().getCurCodepage();
990
- promise.resolve(curCodepage);
991
- } else {
992
- promise.resolve(null);
993
- }
994
- } catch (Exception e) {
995
- promise.reject("getCurCodepage_failed", e.getMessage());
996
- }
997
- }
998
-
999
- @ReactMethod
1000
- public void getEncodeList(final Promise promise) {
1001
- try {
1002
- if (iminPrintUtils == null) {
1003
- List<String> encodeList = PrinterHelper.getInstance().getEncodeList();
1004
- promise.resolve(encodeList);
1005
- } else {
1006
- promise.resolve(null);
1007
- }
1008
- } catch (Exception e) {
1009
- promise.reject("getEncodeList_failed", e.getMessage());
1010
- }
1011
- }
1012
-
1013
- @ReactMethod
1014
- public void setPrinterEncode(int encode, final Promise promise) {
1015
- try {
1016
- if (iminPrintUtils == null) {
1017
- PrinterHelper.getInstance().setPrinterEncode(encode);
1018
- }
1019
- promise.resolve(null);
1020
- } catch (Exception e) {
1021
- promise.reject("setPrinterEncode_failed", e.getMessage());
1022
- }
1023
- }
1024
-
1025
- @ReactMethod
1026
- public void getCurEncode(final Promise promise) {
1027
- try {
1028
- if (iminPrintUtils == null) {
1029
- String curEncode = PrinterHelper.getInstance().getCurEncode();
1030
- promise.resolve(curEncode);
1031
- } else {
1032
- promise.resolve(null);
1033
- }
1034
- } catch (Exception e) {
1035
- promise.reject("getCurEncode_failed", e.getMessage());
1036
- }
1037
- }
1038
-
1039
- @ReactMethod
1040
- public void getPrinterDensityList(final Promise promise) {
1041
- try {
1042
- if (iminPrintUtils == null) {
1043
- List<String> printerDensityList = PrinterHelper.getInstance().getPrinterDensityList();
1044
- promise.resolve(printerDensityList);
1045
- } else {
1046
- promise.resolve(null);
1047
- }
1048
- } catch (Exception e) {
1049
- promise.reject("getPrinterDensityList_failed", e.getMessage());
1050
- }
1051
- }
1052
-
1053
- @ReactMethod
1054
- public void setPrinterDensity(int density, final Promise promise) {
1055
- try {
1056
- if (iminPrintUtils == null) {
1057
- PrinterHelper.getInstance().setPrinterDensity(density);
1058
- }
1059
- promise.resolve(null);
1060
- } catch (Exception e) {
1061
- promise.reject("setPrinterDensity_failed", e.getMessage());
1062
- }
1063
- }
1064
-
1065
- @ReactMethod
1066
- public void getPrinterDensity(final Promise promise) {
1067
- try {
1068
- if (iminPrintUtils == null) {
1069
- promise.resolve(PrinterHelper.getInstance().getPrinterDensity());
1070
- } else {
1071
- promise.resolve(null);
1072
- }
1073
-
1074
- } catch (Exception e) {
1075
- promise.reject("getPrinterDensity_failed", e.getMessage());
1076
- }
1077
- }
1078
-
1079
- @ReactMethod
1080
- public void getPrinterSpeedList(final Promise promise) {
1081
- try {
1082
- if (iminPrintUtils == null) {
1083
- List<String> printerSpeedList = PrinterHelper.getInstance().getPrinterSpeedList();
1084
- promise.resolve(printerSpeedList);
1085
- } else {
1086
- promise.resolve(null);
1087
- }
1088
-
1089
- } catch (Exception e) {
1090
- promise.reject("getPrinterSpeedList_failed", e.getMessage());
1091
- }
1092
- }
1093
-
1094
- @ReactMethod
1095
- public void setPrinterSpeed(int speed, final Promise promise) {
1096
- try {
1097
- if (iminPrintUtils == null) {
1098
- PrinterHelper.getInstance().setPrinterSpeed(speed);
1099
- }
1100
- promise.resolve(null);
1101
- } catch (Exception e) {
1102
- promise.reject("setPrinterSpeed_failed", e.getMessage());
1103
- }
1104
- }
1105
-
1106
- @ReactMethod
1107
- public void getPrinterSpeed(final Promise promise) {
1108
- try {
1109
- if (iminPrintUtils == null) {
1110
- int printerSpeed = PrinterHelper.getInstance().getPrinterSpeed();
1111
- promise.resolve(printerSpeed);
1112
- } else {
1113
- promise.resolve(null);
1114
- }
1115
- } catch (Exception e) {
1116
- promise.reject("getPrinterSpeed_failed", e.getMessage());
1117
- }
1118
- }
1119
-
1120
- @ReactMethod
1121
- public void getPrinterPaperTypeList(final Promise promise) {
1122
- try {
1123
- if (iminPrintUtils == null) {
1124
- List<String> printerPaperTypeList = PrinterHelper.getInstance().getPrinterPaperTypeList();
1125
- promise.resolve(printerPaperTypeList);
1126
- } else {
1127
- promise.resolve(null);
1128
- }
1129
- } catch (Exception e) {
1130
- promise.reject("getPrinterPaperTypeList_failed", e.getMessage());
1131
- }
1132
- }
1133
-
1134
- @ReactMethod
1135
- public void getPrinterPaperType(final Promise promise) {
1136
- try {
1137
- if (iminPrintUtils == null) {
1138
- promise.resolve(PrinterHelper.getInstance().getPrinterPaperType());
1139
- } else {
1140
- promise.resolve(null);
1141
- }
1142
- } catch (Exception e) {
1143
- promise.reject("getPrinterPaperType_failed", e.getMessage());
1144
- }
1145
- }
1146
-
1147
- @ReactMethod
1148
- public void getPrinterSerialNumber(final Promise promise) {
1149
- try {
1150
- if (iminPrintUtils == null) {
1151
- PrinterHelper.getInstance().getPrinterSerialNumber(new INeoPrinterCallback() {
1152
- @Override
1153
- public void onRunResult(boolean isSuccess) throws RemoteException {
1154
- }
1155
-
1156
- @Override
1157
- public void onReturnString(String s) throws RemoteException {
1158
- promise.resolve(s);
1159
- }
1160
-
1161
- @Override
1162
- public void onRaiseException(int code, String msg) throws RemoteException {
1163
- }
1164
-
1165
- @Override
1166
- public void onPrintResult(int code, String msg) throws RemoteException {
1167
- }
1168
- });
1169
- } else {
1170
- promise.resolve(null);
1171
- }
1172
- } catch (Exception e) {
1173
- promise.reject("getPrinterSerialNumber_failed", e.getMessage());
1174
- }
1175
- }
1176
-
1177
- @ReactMethod
1178
- public void getPrinterModelName(final Promise promise) {
1179
- try {
1180
- if (iminPrintUtils == null) {
1181
- PrinterHelper.getInstance().getPrinterModelName(new INeoPrinterCallback() {
1182
- @Override
1183
- public void onRunResult(boolean isSuccess) throws RemoteException {
1184
- }
1185
-
1186
- @Override
1187
- public void onReturnString(String s) throws RemoteException {
1188
- promise.resolve(s);
1189
- }
1190
-
1191
- @Override
1192
- public void onRaiseException(int code, String msg) throws RemoteException {
1193
- }
1194
-
1195
- @Override
1196
- public void onPrintResult(int code, String msg) throws RemoteException {
1197
- }
1198
- });
1199
- } else {
1200
- promise.resolve(null);
1201
- }
1202
- } catch (Exception e) {
1203
- promise.reject("getPrinterModelName_failed", e.getMessage());
1204
- }
1205
- }
1206
-
1207
- @ReactMethod
1208
- public void getPrinterThermalHead(final Promise promise) {
1209
- try {
1210
- if (iminPrintUtils == null) {
1211
- PrinterHelper.getInstance().getPrinterThermalHead(new INeoPrinterCallback() {
1212
- @Override
1213
- public void onRunResult(boolean isSuccess) throws RemoteException {
1214
- }
1215
-
1216
- @Override
1217
- public void onReturnString(String s) throws RemoteException {
1218
- promise.resolve(s);
1219
- }
1220
-
1221
- @Override
1222
- public void onRaiseException(int code, String msg) throws RemoteException {
1223
- }
1224
-
1225
- @Override
1226
- public void onPrintResult(int code, String msg) throws RemoteException {
1227
- }
1228
- });
1229
- } else {
1230
- promise.resolve(null);
1231
- }
1232
- } catch (Exception e) {
1233
- promise.reject("getPrinterThermalHead_failed", e.getMessage());
1234
- }
1235
- }
1236
-
1237
- @ReactMethod
1238
- public void getPrinterFirmwareVersion(final Promise promise) {
1239
- try {
1240
- if (iminPrintUtils == null) {
1241
- PrinterHelper.getInstance().getPrinterFirmwareVersion(new INeoPrinterCallback() {
1242
- @Override
1243
- public void onRunResult(boolean isSuccess) throws RemoteException {
1244
- }
1245
-
1246
- @Override
1247
- public void onReturnString(String s) throws RemoteException {
1248
- promise.resolve(s);
1249
- }
1250
-
1251
- @Override
1252
- public void onRaiseException(int code, String msg) throws RemoteException {
1253
- }
1254
-
1255
- @Override
1256
- public void onPrintResult(int code, String msg) throws RemoteException {
1257
- }
1258
- });
1259
- } else {
1260
- promise.resolve(null);
1261
- }
1262
- } catch (Exception e) {
1263
- promise.reject("getPrinterFirmwareVersion_failed", e.getMessage());
1264
- }
1265
- }
1266
-
1267
- @ReactMethod
1268
- public void getPrinterHardwareVersion(final Promise promise) {
1269
- try {
1270
- if (iminPrintUtils == null) {
1271
- PrinterHelper.getInstance().getPrinterHardwareVersion(new INeoPrinterCallback() {
1272
- @Override
1273
- public void onRunResult(boolean isSuccess) throws RemoteException {
1274
- }
1275
-
1276
- @Override
1277
- public void onReturnString(String s) throws RemoteException {
1278
- promise.resolve(s);
1279
- }
1280
-
1281
- @Override
1282
- public void onRaiseException(int code, String msg) throws RemoteException {
1283
- }
1284
-
1285
- @Override
1286
- public void onPrintResult(int code, String msg) throws RemoteException {
1287
- }
1288
- });
1289
- } else {
1290
- promise.resolve(null);
1291
- }
1292
- } catch (Exception e) {
1293
- promise.reject("getPrinterHardwareVersion_failed", e.getMessage());
1294
- }
1295
- }
1296
-
1297
- @ReactMethod
1298
- public void getPrinterPaperDistance(final Promise promise) {
1299
- try {
1300
- if (iminPrintUtils == null) {
1301
- PrinterHelper.getInstance().getPrinterPaperDistance(new INeoPrinterCallback() {
1302
- @Override
1303
- public void onRunResult(boolean isSuccess) throws RemoteException {
1304
- }
1305
-
1306
- @Override
1307
- public void onReturnString(String s) throws RemoteException {
1308
- promise.resolve(s);
1309
- }
1310
-
1311
- @Override
1312
- public void onRaiseException(int code, String msg) throws RemoteException {
1313
- }
1314
-
1315
- @Override
1316
- public void onPrintResult(int code, String msg) throws RemoteException {
1317
- }
1318
- });
1319
- } else {
1320
- promise.resolve(null);
1321
- }
1322
- } catch (Exception e) {
1323
- promise.reject("getPrinterPaperDistance_failed", e.getMessage());
1324
- }
1325
- }
1326
-
1327
- @ReactMethod
1328
- public void getPrinterCutTimes(final Promise promise) {
1329
- try {
1330
- if (iminPrintUtils == null) {
1331
- PrinterHelper.getInstance().getPrinterCutTimes(new INeoPrinterCallback() {
1332
- @Override
1333
- public void onRunResult(boolean isSuccess) throws RemoteException {
1334
- }
1335
-
1336
- @Override
1337
- public void onReturnString(String s) throws RemoteException {
1338
- promise.resolve(s);
1339
- }
1340
-
1341
- @Override
1342
- public void onRaiseException(int code, String msg) throws RemoteException {
1343
- }
1344
-
1345
- @Override
1346
- public void onPrintResult(int code, String msg) throws RemoteException {
1347
- }
1348
- });
1349
- } else {
1350
- promise.resolve(null);
1351
- }
1352
- } catch (Exception e) {
1353
- promise.reject("getPrinterCutTimes_failed", e.getMessage());
1354
- }
1355
- }
1356
-
1357
-
1358
- @ReactMethod
1359
- public void getPrinterMode(final Promise promise) {
1360
- try {
1361
- if (iminPrintUtils == null) {
1362
- promise.resolve(PrinterHelper.getInstance().getPrinterMode());
1363
- } else {
1364
- promise.resolve(null);
1365
- }
1366
- } catch (Exception e) {
1367
- promise.reject("getPrinterMode_failed", e.getMessage());
1368
- }
1369
- }
1370
-
1371
- @ReactMethod
1372
- public void getDrawerStatus(final Promise promise) {
1373
- try {
1374
- if (iminPrintUtils == null) {
1375
- promise.resolve(PrinterHelper.getInstance().getDrawerStatus());
1376
- } else {
1377
- promise.resolve(null);
1378
- }
1379
- } catch (Exception e) {
1380
- promise.reject("getDrawerStatus_failed", e.getMessage());
1381
- }
1382
- }
1383
-
1384
- @ReactMethod
1385
- public void getOpenDrawerTimes(final Promise promise) {
1386
- try {
1387
- if (iminPrintUtils == null) {
1388
- promise.resolve(PrinterHelper.getInstance().getOpenDrawerTimes());
1389
- } else {
1390
- promise.resolve(null);
1391
- }
1392
- } catch (Exception e) {
1393
- promise.reject("getOpenDrawerTimes_failed", e.getMessage());
1394
- }
1395
- }
1396
-
1397
- @ReactMethod
1398
- public void getServiceVersion(final Promise promise) {
1399
- try {
1400
- if (iminPrintUtils == null) {
1401
- promise.resolve(PrinterHelper.getInstance().getServiceVersion());
1402
- } else {
1403
- promise.resolve(null);
1404
- }
1405
- } catch (Exception e) {
1406
- promise.reject("getServiceVersion_failed", e.getMessage());
1407
- }
1408
- }
1409
-
1410
- @ReactMethod
1411
- public void getUsbPrinterVidPid(final Promise promise) {
1412
- try {
1413
- if (iminPrintUtils == null) {
1414
- promise.resolve(PrinterHelper.getInstance().getUsbPrinterVidPid());
1415
- } else {
1416
- promise.resolve(null);
1417
- }
1418
- } catch (Exception e) {
1419
- promise.reject("getUsbPrinterVidPid_failed", e.getMessage());
1420
- }
1421
- }
1422
-
1423
- @ReactMethod
1424
- public void getUsbDevicesName(final Promise promise) {
1425
- try {
1426
- if (iminPrintUtils == null) {
1427
- promise.resolve(PrinterHelper.getInstance().getUsbDevicesName());
1428
- } else {
1429
- promise.resolve(null);
1430
- }
1431
- } catch (Exception e) {
1432
- promise.reject("getUsbDevicesName_failed", e.getMessage());
1433
- }
1434
- }
1435
-
1436
-
1437
- @ReactMethod
1438
- public void setCodeAlignment(int align, final Promise promise) {
1439
- try {
1440
- if (iminPrintUtils == null) {
1441
- PrinterHelper.getInstance().setCodeAlignment(align);
1442
- }
1443
- promise.resolve(null);
1444
- } catch (Exception e) {
1445
- promise.reject("setCodeAlignment_failed", e.getMessage());
1446
- }
1447
- }
1448
-
1449
- @ReactMethod
1450
- public void setTextBitmapTypeface(int typeface, final Promise promise) {
1451
- try {
1452
- if (iminPrintUtils == null) {
1453
- switch (typeface) {
1454
- case 1:
1455
- PrinterHelper.getInstance().setTextBitmapTypeface("Typeface.MONOSPACE");
1456
- break;
1457
- case 2:
1458
- PrinterHelper.getInstance().setTextBitmapTypeface("Typeface.DEFAULT_BOLD");
1459
- break;
1460
- case 3:
1461
- PrinterHelper.getInstance().setTextBitmapTypeface("Typeface.SANS_SERIF");
1462
- break;
1463
- case 4:
1464
- PrinterHelper.getInstance().setTextBitmapTypeface("Typeface.SERIF");
1465
- break;
1466
- default:
1467
- PrinterHelper.getInstance().setTextBitmapTypeface("Typeface.DEFAULT");
1468
- break;
1469
- }
1470
- }
1471
- promise.resolve(null);
1472
- } catch (Exception e) {
1473
- promise.reject("setTextBitmapTypeface_failed", e.getMessage());
1474
- }
1475
- }
1476
-
1477
- @ReactMethod
1478
- public void setTextBitmapSize(int size, final Promise promise) {
1479
- try {
1480
- if (iminPrintUtils == null) {
1481
- PrinterHelper.getInstance().setTextBitmapSize(size);
1482
- }
1483
- promise.resolve(null);
1484
- } catch (Exception e) {
1485
- promise.reject("setTextBitmapSize_failed", e.getMessage());
1486
- }
1487
- }
1488
-
1489
- @ReactMethod
1490
- public void setTextBitmapStyle(int style, final Promise promise) {
1491
- try {
1492
- if (iminPrintUtils == null) {
1493
- PrinterHelper.getInstance().setTextBitmapStyle(style);
1494
- }
1495
- promise.resolve(null);
1496
- } catch (Exception e) {
1497
- promise.reject("setTextBitmapStyle_failed", e.getMessage());
1498
- }
1499
- }
1500
-
1501
- @ReactMethod
1502
- public void setTextBitmapStrikeThru(boolean strikeThru, final Promise promise) {
1503
- try {
1504
- if (iminPrintUtils == null) {
1505
- PrinterHelper.getInstance().setTextBitmapStrikeThru(strikeThru);
1506
- }
1507
- promise.resolve(null);
1508
- } catch (Exception e) {
1509
- promise.reject("setTextBitmapStrikeThru_failed", e.getMessage());
1510
- }
1511
- }
1512
-
1513
- @ReactMethod
1514
- public void setTextBitmapUnderline(boolean haveUnderline, final Promise promise) {
1515
- try {
1516
- if (iminPrintUtils == null) {
1517
- PrinterHelper.getInstance().setTextBitmapUnderline(haveUnderline);
1518
- }
1519
- promise.resolve(null);
1520
- } catch (Exception e) {
1521
- promise.reject("setTextBitmapUnderline_failed", e.getMessage());
1522
- }
1523
- }
1524
-
1525
- @ReactMethod
1526
- public void setTextBitmapLineSpacing(float lineHeight, final Promise promise) {
1527
- try {
1528
- if (iminPrintUtils == null) {
1529
- PrinterHelper.getInstance().setTextBitmapLineSpacing(lineHeight);
1530
- }
1531
- promise.resolve(null);
1532
- } catch (Exception e) {
1533
- promise.reject("setTextBitmapLineSpacing_failed", e.getMessage());
1534
- }
1535
- }
1536
-
1537
- @ReactMethod
1538
- public void setTextBitmapLetterSpacing(float space, final Promise promise) {
1539
- try {
1540
- if (iminPrintUtils == null) {
1541
- PrinterHelper.getInstance().setTextBitmapLetterSpacing(space);
1542
- }
1543
- promise.resolve(null);
1544
- } catch (Exception e) {
1545
- promise.reject("setTextBitmapLetterSpacing_failed", e.getMessage());
1546
- }
1547
- }
1548
-
1549
- @ReactMethod
1550
- public void setTextBitmapAntiWhite(boolean antiWhite, final Promise promise) {
1551
- try {
1552
- if (iminPrintUtils == null) {
1553
- PrinterHelper.getInstance().setTextBitmapAntiWhite(antiWhite);
1554
- }
1555
- promise.resolve(null);
1556
- } catch (Exception e) {
1557
- promise.reject("setTextBitmapAntiWhite_failed", e.getMessage());
1558
- }
1559
- }
1560
-
1561
- @ReactMethod
1562
- public void printTextBitmap(ReadableMap config, final Promise promise) {
1563
- try {
1564
- if (iminPrintUtils == null) {
1565
- String text = config.getString("text");
1566
- if (config.hasKey("align")) {
1567
- int align = config.getInt("align");
1568
- PrinterHelper.getInstance().printTextBitmapWithAli(text + "\n", align, null);
1569
- } else {
1570
- PrinterHelper.getInstance().printTextBitmap(text + "\n", null);
1571
- }
1572
- }
1573
- promise.resolve(null);
1574
- } catch (Exception e) {
1575
- promise.reject("printTextBitmap_failed", e.getMessage());
1576
- }
1577
- }
1578
-
1579
- @ReactMethod
1580
- public void enterPrinterBuffer(boolean isClean, final Promise promise) {
1581
- try {
1582
- if (iminPrintUtils == null) {
1583
- PrinterHelper.getInstance().enterPrinterBuffer(isClean);
1584
- }
1585
- promise.resolve(null);
1586
- } catch (Exception e) {
1587
- promise.reject("enterPrinterBuffer_failed", e.getMessage());
1588
- }
1589
- }
1590
-
1591
- @ReactMethod
1592
- public void commitPrinterBuffer(final Promise promise) {
1593
- try {
1594
- if (iminPrintUtils == null) {
1595
- PrinterHelper.getInstance().commitPrinterBuffer(null);
1596
- }
1597
- promise.resolve(null);
1598
- } catch (Exception e) {
1599
- promise.reject("commitPrinterBuffer_failed", e.getMessage());
1600
- }
1601
- }
1602
-
1603
- @ReactMethod
1604
- public void exitPrinterBuffer(boolean isCommit, final Promise promise) {
1605
- try {
1606
- if (iminPrintUtils == null) {
1607
- PrinterHelper.getInstance().exitPrinterBuffer(isCommit);
1608
- }
1609
- promise.resolve(null);
1610
- } catch (Exception e) {
1611
- promise.reject("exitPrinterBuffer_failed", e.getMessage());
1612
- }
1613
- }
1614
-
1615
- private BroadcastReceiver createChargingStateBroadcastReceiver() {
1616
- return new BroadcastReceiver() {
1617
- @Override
1618
- public void onReceive(Context context, Intent intent) {
1619
- int status = intent.getIntExtra(ACTION_PRITER_STATUS, -1);
1620
- Log.d(TAG, "打印机状态:" + intent.getAction());
1621
- // 发送事件到React Native
1622
- if (intent.getAction().equals(ACTION_PRITER_STATUS_CHANGE)) {
1623
- WritableMap result = Arguments.createMap();
1624
- WritableMap payload = Arguments.createMap();
1625
- payload.putString("message", Utils.getInstance().getPrinterStatusText(status));
1626
- payload.putString("code", String.format("%d", status));
1627
- result.putMap("eventData", payload);
1628
- result.putString("eventName", "printer_status");
1629
- sendEvent(reactContext, "eventBroadcast", result);
1630
- }
1631
- }
1632
- };
1633
- }
1634
-
1635
- private void initializeBroadcastReceiver() {
1636
- IntentFilter intentFilter = new IntentFilter();
1637
- mBroadcastReceiver = createChargingStateBroadcastReceiver();
1638
- intentFilter.addAction(ACTION_PRITER_STATUS_CHANGE);
1639
- intentFilter.addAction(ACTION_POGOPIN_STATUS_CHANGE);
1640
- getReactApplicationContext().registerReceiver(mBroadcastReceiver, intentFilter);
1641
- }
1642
-
1643
- @ReactMethod
1644
- public void addListener(String eventName) {
1645
- }
1646
-
1647
- @ReactMethod
1648
- public void removeListeners(Integer count) {
1649
- }
1650
-
1651
- @Override
1652
- public Map<String, Object> getConstants() {
1653
- final Map<String, Object> constants = new HashMap<>();
1654
- constants.put("SDK_VERSION_IMIN", sdkVersion);
1655
- return constants;
1656
- }
1657
-
1658
- }
1
+ package com.printerimin;
2
+
3
+
4
+ import com.bumptech.glide.Glide;
5
+ import com.bumptech.glide.load.engine.DiskCacheStrategy;
6
+
7
+ import android.annotation.SuppressLint;
8
+ import android.graphics.Bitmap;
9
+ import android.graphics.Typeface;
10
+ import android.os.IBinder;
11
+ import android.os.RemoteException;
12
+ import android.graphics.BitmapFactory;
13
+ import android.os.Build;
14
+ import android.util.Log;
15
+
16
+ import androidx.annotation.NonNull;
17
+ import androidx.annotation.Nullable;
18
+
19
+ import com.facebook.react.bridge.Arguments;
20
+ import com.facebook.react.bridge.Promise;
21
+ import com.facebook.react.bridge.ReactApplicationContext;
22
+ import com.facebook.react.bridge.ReactContext;
23
+ import com.facebook.react.bridge.ReactContextBaseJavaModule;
24
+ import com.facebook.react.bridge.ReactMethod;
25
+ import com.facebook.react.bridge.ReadableArray;
26
+ import com.facebook.react.bridge.ReadableMap;
27
+ import com.facebook.react.bridge.WritableMap;
28
+
29
+ import com.facebook.react.module.annotations.ReactModule;
30
+ import com.facebook.react.modules.core.DeviceEventManagerModule;
31
+
32
+
33
+ import com.imin.printer.ILabelPrintResult;
34
+ import com.imin.printer.INeoPrinterCallback;
35
+ import com.imin.printer.PrinterHelper;
36
+ import com.imin.printer.enums.Align;
37
+ import com.imin.printer.enums.ErrorLevel;
38
+ import com.imin.printer.enums.HumanReadable;
39
+ import com.imin.printer.enums.ImageAlgorithm;
40
+ import com.imin.printer.enums.Rotate;
41
+ import com.imin.printer.enums.Shape;
42
+ import com.imin.printer.enums.Symbology;
43
+ import com.imin.printer.label.LabelAreaStyle;
44
+ import com.imin.printer.label.LabelBarCodeStyle;
45
+ import com.imin.printer.label.LabelBitmapStyle;
46
+ import com.imin.printer.label.LabelCanvasStyle;
47
+ import com.imin.printer.label.LabelQrCodeStyle;
48
+ import com.imin.printer.label.LabelTextStyle;
49
+ import com.imin.printerlib.Callback;
50
+ import com.imin.printerlib.IminPrintUtils;
51
+
52
+ import java.util.ArrayList;
53
+ import java.util.Arrays;
54
+ import java.util.HashMap;
55
+ import java.util.List;
56
+ import java.util.Map;
57
+
58
+ import org.json.JSONArray;
59
+ import org.json.JSONObject;
60
+
61
+ import org.json.JSONException;
62
+
63
+
64
+ import java.lang.RuntimeException;
65
+
66
+ import android.content.Context;
67
+ import android.content.Intent;
68
+ import android.content.IntentFilter;
69
+ import android.content.BroadcastReceiver;
70
+
71
+ @ReactModule(name = PrinterIminModule.NAME)
72
+ public class PrinterIminModule extends ReactContextBaseJavaModule {
73
+ private final ReactApplicationContext reactContext;
74
+ public static final String NAME = "PrinterImin";
75
+ private IminPrintUtils iminPrintUtils;
76
+ // private String[] modelArry = {"W27_Pro", "I23M01", "I23M02", "I23D01", "D4-503 Pro", "D4-504 Pro", "D4-505 Pro", "MS2-11", "MS2-12", "MS1-15"};
77
+ private static final String ACTION_PRITER_STATUS_CHANGE = "com.imin.printerservice.PRITER_STATUS_CHANGE";
78
+ private static final String ACTION_POGOPIN_STATUS_CHANGE = "com.imin.printerservice.PRITER_CONNECT_STATUS_CHANGE";
79
+ private String sdkVersion = "1.0.0";
80
+ private static final String ACTION_PRITER_STATUS = "status";
81
+ private IminPrintUtils.PrintConnectType connectType = IminPrintUtils.PrintConnectType.USB;
82
+ public static final String TAG = "IminPrinterReactNativePlugin";
83
+ private BroadcastReceiver mBroadcastReceiver;
84
+
85
+ public PrinterIminModule(ReactApplicationContext reactContext) {
86
+ super(reactContext);
87
+ this.reactContext = reactContext;
88
+ // List<String> modelList = Arrays.asList(modelArry);
89
+ if (Build.MODEL.contains("W27_Pro") || Build.MODEL.contains("I23D") || Build.MODEL.contains("I23M") || Build.MODEL.contains("I24D") || Build.MODEL.contains("I24T") || Build.MODEL.contains("I24M")) {
90
+ //初始化 2.0 的 SDK。
91
+ PrinterHelper.getInstance().initPrinterService(reactContext);
92
+ sdkVersion = "2.0.0";
93
+ }
94
+ // if (modelList.contains(Build.MODEL)) {
95
+ // //初始化 2.0 的 SDK。
96
+ // PrinterHelper.getInstance().initPrinterService(reactContext);
97
+ // sdkVersion = "2.0.0";
98
+ // }
99
+ else {
100
+ //初始化 1.0 SDK
101
+
102
+ iminPrintUtils = IminPrintUtils.getInstance(reactContext);
103
+ String deviceModel = Utils.getInstance().getModel();
104
+ if (deviceModel.contains("M2-203") || deviceModel.contains("M2-202") || deviceModel.contains("M2-Pro")) {
105
+ connectType = IminPrintUtils.PrintConnectType.SPI;
106
+ } else {
107
+ connectType = IminPrintUtils.PrintConnectType.USB;
108
+ }
109
+ iminPrintUtils.setIsOpenLog(IminPrintUtils.isOpenLog == 1 ? 0 : 1);
110
+ iminPrintUtils.resetDevice();
111
+ sdkVersion = "1.0.0";
112
+ }
113
+ initializeBroadcastReceiver();
114
+ }
115
+
116
+ @Override
117
+ @NonNull
118
+ public String getName() {
119
+ return NAME;
120
+ }
121
+
122
+ private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
123
+
124
+ try {
125
+ reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
126
+ } catch (RuntimeException e) {
127
+ Log.e(TAG, "java.lang.RuntimeException: Trying to invoke JS before CatalystInstance has been set!", e);
128
+ }
129
+ }
130
+
131
+ @ReactMethod
132
+ public void initPrinter(final Promise promise) {
133
+ try {
134
+ if (iminPrintUtils != null) {
135
+ iminPrintUtils.initPrinter(connectType);
136
+ } else {
137
+ PrinterHelper.getInstance().initPrinter(reactContext.getPackageName(), null);
138
+ }
139
+ promise.resolve(true);
140
+ } catch (Exception e) {
141
+ promise.reject("ININTPRINTER_FAILED", e.getMessage());
142
+ }
143
+ }
144
+
145
+ @ReactMethod
146
+ public void getPrinterStatus(final Promise promise) {
147
+ try {
148
+ WritableMap payload = Arguments.createMap();
149
+ if (iminPrintUtils != null) {
150
+ if (connectType.equals(IminPrintUtils.PrintConnectType.SPI)) {
151
+ iminPrintUtils.getPrinterStatus(connectType, new Callback() {
152
+ @Override
153
+ public void callback(int status) {
154
+ payload.putString("message", Utils.getInstance().getPrinterStatusText(status));
155
+ payload.putString("code", String.format("%d", status));
156
+ promise.resolve(payload);
157
+ }
158
+ });
159
+ } else {
160
+ int status = iminPrintUtils.getPrinterStatus(connectType);
161
+ payload.putString("message", Utils.getInstance().getPrinterStatusText(status));
162
+ payload.putString("code", String.format("%d", status));
163
+ promise.resolve(payload);
164
+ }
165
+ } else {
166
+ int status = PrinterHelper.getInstance().getPrinterStatus();
167
+ payload.putString("message", Utils.getInstance().getPrinterStatusText(status));
168
+ payload.putString("code", String.format("%d", status));
169
+ promise.resolve(payload);
170
+ }
171
+ payload.putString("message", "sdsd");
172
+ payload.putString("code", "1");
173
+ promise.resolve(payload);
174
+ } catch (Exception e) {
175
+ promise.reject("getPrinterStatus_failed", e.getMessage());
176
+ }
177
+
178
+ }
179
+
180
+ @ReactMethod
181
+ public void setTextSize(int size, final Promise promise) {
182
+ try {
183
+ if (iminPrintUtils != null) {
184
+ iminPrintUtils.setTextSize(size);
185
+ }
186
+ promise.resolve(null);
187
+ } catch (Exception e) {
188
+ promise.reject("setTextSize_failed", e.getMessage());
189
+ }
190
+ }
191
+
192
+ @ReactMethod
193
+ public void setTextTypeface(int font, final Promise promise) {
194
+ try {
195
+ if (iminPrintUtils != null) {
196
+ switch (font) {
197
+ case 1:
198
+ iminPrintUtils.setTextTypeface(Typeface.MONOSPACE);
199
+ break;
200
+ case 2:
201
+ iminPrintUtils.setTextTypeface(Typeface.DEFAULT_BOLD);
202
+ break;
203
+ case 3:
204
+ iminPrintUtils.setTextTypeface(Typeface.SANS_SERIF);
205
+ break;
206
+ case 4:
207
+ iminPrintUtils.setTextTypeface(Typeface.SERIF);
208
+ break;
209
+ default:
210
+ iminPrintUtils.setTextTypeface(Typeface.DEFAULT);
211
+ break;
212
+ }
213
+ }
214
+ promise.resolve(null);
215
+ } catch (Exception e) {
216
+ promise.reject("setTextTypeface_failed", e.getMessage());
217
+ }
218
+ }
219
+
220
+ @ReactMethod
221
+ public void setTextStyle(int style, final Promise promise) {
222
+ try {
223
+ if (iminPrintUtils != null) {
224
+ iminPrintUtils.setTextStyle(style);
225
+ }
226
+ promise.resolve(null);
227
+ } catch (Exception e) {
228
+ promise.reject("setTextStyle_failed", e.getMessage());
229
+ }
230
+ }
231
+
232
+ @ReactMethod
233
+ public void setAlignment(int alignment, final Promise promise) {
234
+ try {
235
+ if (iminPrintUtils != null) {
236
+ iminPrintUtils.setAlignment(alignment);
237
+ }
238
+ promise.resolve(null);
239
+ } catch (Exception e) {
240
+ promise.reject("setAlignment_failed", e.getMessage());
241
+ }
242
+ }
243
+
244
+ @ReactMethod
245
+ public void setTextLineSpacing(float space, final Promise promise) {
246
+ try {
247
+ if (iminPrintUtils != null) {
248
+ iminPrintUtils.setTextLineSpacing(space);
249
+ }
250
+ promise.resolve(null);
251
+ } catch (Exception e) {
252
+ promise.reject("setTextLineSpacing_failed", e.getMessage());
253
+ }
254
+ }
255
+
256
+ @ReactMethod
257
+ public void printText(String text, final Promise promise) {
258
+ try {
259
+ if (iminPrintUtils != null) {
260
+ iminPrintUtils.printText(text + "\n");
261
+ } else {
262
+ PrinterHelper.getInstance().printText(text + "\n", null);
263
+ }
264
+ promise.resolve(null);
265
+ } catch (Exception e) {
266
+ promise.reject("printText_failed", e.getMessage());
267
+ }
268
+ }
269
+
270
+ @ReactMethod
271
+ public void printAntiWhiteText(String text, final Promise promise) {
272
+ try {
273
+ if (iminPrintUtils != null) {
274
+ iminPrintUtils.printAntiWhiteText(text);
275
+ }
276
+ promise.resolve(null);
277
+ } catch (Exception e) {
278
+ promise.reject("printAntiWhiteText_failed", e.getMessage());
279
+ }
280
+ }
281
+
282
+ @ReactMethod
283
+ public void setTextWidth(int width, final Promise promise) {
284
+ try {
285
+ if (iminPrintUtils != null) {
286
+ iminPrintUtils.setTextWidth(width);
287
+ }
288
+ promise.resolve(null);
289
+ } catch (Exception e) {
290
+ promise.reject("setTextWidth_failed", e.getMessage());
291
+ }
292
+ }
293
+
294
+ @ReactMethod
295
+ public void printAndLineFeed(final Promise promise) {
296
+ try {
297
+ if (iminPrintUtils != null) {
298
+ iminPrintUtils.printAndLineFeed();
299
+ } else {
300
+ PrinterHelper.getInstance().printAndLineFeed();
301
+ }
302
+ promise.resolve(null);
303
+ } catch (Exception e) {
304
+ promise.reject("printAndLineFeed_failed", e.getMessage());
305
+ }
306
+ }
307
+
308
+ @ReactMethod
309
+ public void printAndFeedPaper(int height, final Promise promise) {
310
+ try {
311
+ if (iminPrintUtils != null) {
312
+ iminPrintUtils.printAndFeedPaper(height);
313
+ } else {
314
+ PrinterHelper.getInstance().printAndFeedPaper(height);
315
+ }
316
+ promise.resolve(null);
317
+ } catch (Exception e) {
318
+ promise.reject("printAndLineFeed_failed", e.getMessage());
319
+ }
320
+ }
321
+
322
+ @SuppressLint("LongLogTag")
323
+ @ReactMethod
324
+ public void printColumnsText(ReadableArray cols, final Promise promise) {
325
+ try {
326
+ String[] colsText = new String[cols.size()];
327
+ int[] colsWidth = new int[cols.size()];
328
+ int[] colsAlign = new int[cols.size()];
329
+ int[] colsFontSize = new int[cols.size()];
330
+ for (int i = 0; i < cols.size(); i++) {
331
+ ReadableMap col = cols.getMap(i);
332
+ String textColumn = col.getString("text");
333
+ int widthColumn = col.getInt("width");
334
+ int alignColumn = col.getInt("align");
335
+ Log.d(TAG, "printColumnsText: colsText" + textColumn);
336
+ int fontSizeColumn = col.getInt("fontSize");
337
+ colsText[i] = textColumn;
338
+ colsWidth[i] = widthColumn;
339
+ colsAlign[i] = alignColumn;
340
+ colsFontSize[i] = fontSizeColumn;
341
+ }
342
+ Log.d(TAG, "printColumnsText: colsText" + colsText);
343
+ Log.d(TAG, "printColumnsText: colsWidth" + colsWidth);
344
+ Log.d(TAG, "printColumnsText: colsAlign" + colsAlign);
345
+ Log.d(TAG, "printColumnsText: colsFontSize" + colsFontSize);
346
+ if (iminPrintUtils != null) {
347
+ iminPrintUtils.printColumnsText(colsText, colsWidth, colsAlign, colsFontSize);
348
+ } else {
349
+ PrinterHelper.getInstance().printColumnsText(colsText, colsWidth, colsAlign, colsFontSize, null);
350
+ }
351
+ promise.resolve(null);
352
+ } catch (Exception e) {
353
+ promise.reject("printColumnsText_failed", e.getMessage());
354
+ }
355
+ }
356
+
357
+ @ReactMethod
358
+ public void setPageFormat(int style, final Promise promise) {
359
+ try {
360
+ if (iminPrintUtils != null) {
361
+ iminPrintUtils.setPageFormat(style);
362
+ } else {
363
+ PrinterHelper.getInstance().setPageFormat(style);
364
+ }
365
+ promise.resolve(null);
366
+ } catch (Exception e) {
367
+ promise.reject("setPageFormat_failed", e.getMessage());
368
+ }
369
+ }
370
+
371
+ @ReactMethod
372
+ public void partialCut(final Promise promise) {
373
+ try {
374
+ if (iminPrintUtils != null) {
375
+ iminPrintUtils.partialCut();
376
+ } else {
377
+ PrinterHelper.getInstance().partialCut();
378
+ }
379
+ promise.resolve(null);
380
+ } catch (Exception e) {
381
+ promise.reject("partialCut_failed", e.getMessage());
382
+ }
383
+ }
384
+
385
+ @ReactMethod
386
+ public void fullCut(final Promise promise) {
387
+ try {
388
+ if (iminPrintUtils == null) {
389
+ PrinterHelper.getInstance().fullCut();
390
+ }
391
+ promise.resolve(null);
392
+ } catch (Exception e) {
393
+ promise.reject("fullCut_failed", e.getMessage());
394
+ }
395
+ }
396
+
397
+ @ReactMethod
398
+ public void printSingleBitmap(ReadableMap config, final Promise promise) {
399
+ try {
400
+ String url = config.getString("url");
401
+ if (config.hasKey("width") && config.hasKey("height")) {
402
+ int width = config.getInt("width");
403
+ int height = config.getInt("height");
404
+ Bitmap image = Glide.with(reactContext).asBitmap().load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit(width, height).get();
405
+ if (image.isRecycled()) {
406
+ return;
407
+ }
408
+ if (iminPrintUtils != null) {
409
+ if (config.hasKey("align")) {
410
+ int align = config.getInt("align");
411
+ iminPrintUtils.printSingleBitmap(image, align);
412
+ } else {
413
+ iminPrintUtils.printSingleBitmap(image);
414
+ }
415
+ } else {
416
+ if (config.hasKey("align")) {
417
+ int align = config.getInt("align");
418
+ PrinterHelper.getInstance().printBitmapWithAlign(image, align, null);
419
+ } else {
420
+ PrinterHelper.getInstance().printBitmap(image, null);
421
+ }
422
+ }
423
+ } else {
424
+ Bitmap image = Glide.with(reactContext).asBitmap().load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit().get();
425
+ if (image.isRecycled()) {
426
+ return;
427
+ }
428
+ if (iminPrintUtils != null) {
429
+ if (config.hasKey("align")) {
430
+ int align = config.getInt("align");
431
+ iminPrintUtils.printSingleBitmap(image, align);
432
+ } else {
433
+ iminPrintUtils.printSingleBitmap(image);
434
+ }
435
+ } else {
436
+ if (config.hasKey("align")) {
437
+ int align = config.getInt("align");
438
+ PrinterHelper.getInstance().printBitmapWithAlign(image, align, null);
439
+ } else {
440
+ PrinterHelper.getInstance().printBitmap(image, null);
441
+ }
442
+ }
443
+ }
444
+ promise.resolve(null);
445
+ } catch (Exception e) {
446
+ promise.reject("printSingleBitmap_failed", e.getMessage());
447
+ }
448
+ }
449
+
450
+ @ReactMethod
451
+ public void printMultiBitmap(ReadableMap config, final Promise promise) {
452
+ try {
453
+ ReadableArray urls = config.getArray("urls");
454
+ ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
455
+ if (config.hasKey("width") && config.hasKey("height")) {
456
+ int width = config.getInt("width");
457
+ int height = config.getInt("height");
458
+ for (int i = 0; i < urls.size(); i++) {
459
+ Bitmap image = Glide.with(reactContext).asBitmap().load(urls.getString(i)).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit(width, height).get();
460
+ if (!image.isRecycled()) {
461
+ bitmaps.add(image);
462
+ }
463
+ }
464
+ if (iminPrintUtils != null) {
465
+ if (config.hasKey("align")) {
466
+ int align = config.getInt("align");
467
+ iminPrintUtils.printMultiBitmap(bitmaps, align);
468
+ } else {
469
+ iminPrintUtils.printMultiBitmap(bitmaps, 0);
470
+ }
471
+ } else {
472
+ if (config.hasKey("align")) {
473
+ int align = config.getInt("align");
474
+ PrinterHelper.getInstance().printMultiBitmapWithAlign(bitmaps, align, null);
475
+ } else {
476
+ PrinterHelper.getInstance().printMultiBitmap(bitmaps, null);
477
+ }
478
+ }
479
+ } else {
480
+ for (int i = 0; i < urls.size(); i++) {
481
+ Bitmap image = Glide.with(reactContext).asBitmap().load(urls.getString(i)).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit().get();
482
+ if (!image.isRecycled()) {
483
+ bitmaps.add(image);
484
+ }
485
+ }
486
+ if (iminPrintUtils != null) {
487
+ if (config.hasKey("align")) {
488
+ int align = config.getInt("align");
489
+ iminPrintUtils.printMultiBitmap(bitmaps, align);
490
+ } else {
491
+ iminPrintUtils.printMultiBitmap(bitmaps, 0);
492
+ }
493
+ } else {
494
+ if (config.hasKey("align")) {
495
+ int align = config.getInt("align");
496
+ PrinterHelper.getInstance().printMultiBitmapWithAlign(bitmaps, align, null);
497
+ } else {
498
+ PrinterHelper.getInstance().printMultiBitmap(bitmaps, null);
499
+ }
500
+ }
501
+ }
502
+ promise.resolve(null);
503
+ } catch (Exception e) {
504
+ promise.reject("printMultiBitmap_failed", e.getMessage());
505
+ }
506
+ }
507
+
508
+ @ReactMethod
509
+ public void printSingleBitmapBlackWhite(ReadableMap config, final Promise promise) {
510
+ try {
511
+ String url = config.getString("url");
512
+ if (config.hasKey("width") && config.hasKey("height")) {
513
+ int width = config.getInt("width");
514
+ int height = config.getInt("height");
515
+ Bitmap image = Glide.with(reactContext).asBitmap().load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit(width, height).get();
516
+ if (image.isRecycled()) {
517
+ return;
518
+ }
519
+ if (iminPrintUtils != null) {
520
+ iminPrintUtils.printSingleBitmapBlackWhite(image);
521
+ }
522
+ } else {
523
+ Bitmap image = Glide.with(reactContext).asBitmap().load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit().get();
524
+ if (image.isRecycled()) {
525
+ return;
526
+ }
527
+ if (iminPrintUtils != null) {
528
+ iminPrintUtils.printSingleBitmapBlackWhite(image);
529
+ }
530
+ }
531
+ promise.resolve(null);
532
+ } catch (Exception e) {
533
+ promise.reject("printSingleBitmapBlackWhite_failed", e.getMessage());
534
+ }
535
+ }
536
+
537
+ @ReactMethod
538
+ public void setQrCodeSize(int qrSize, final Promise promise) {
539
+ try {
540
+ if (iminPrintUtils != null) {
541
+ iminPrintUtils.setQrCodeSize(qrSize);
542
+ } else {
543
+ PrinterHelper.getInstance().setQrCodeSize(qrSize);
544
+ }
545
+ promise.resolve(null);
546
+ } catch (Exception e) {
547
+ promise.reject("setQrCodeSize_failed", e.getMessage());
548
+ }
549
+ }
550
+
551
+ @ReactMethod
552
+ public void setLeftMargin(int margin, final Promise promise) {
553
+ try {
554
+ if (iminPrintUtils != null) {
555
+ iminPrintUtils.setLeftMargin(margin);
556
+ } else {
557
+ PrinterHelper.getInstance().setLeftMargin(margin);
558
+ }
559
+ promise.resolve(null);
560
+ } catch (Exception e) {
561
+ promise.reject("setLeftMargin_failed", e.getMessage());
562
+ }
563
+ }
564
+
565
+ @ReactMethod
566
+ public void setBarCodeWidth(int width, final Promise promise) {
567
+ try {
568
+ if (iminPrintUtils != null) {
569
+ iminPrintUtils.setBarCodeWidth(width);
570
+ } else {
571
+ PrinterHelper.getInstance().setBarCodeWidth(width);
572
+ }
573
+ promise.resolve(null);
574
+ } catch (Exception e) {
575
+ promise.reject("setBarCodeWidth_failed", e.getMessage());
576
+ }
577
+ }
578
+
579
+ @ReactMethod
580
+ public void setBarCodeHeight(int height, final Promise promise) {
581
+ try {
582
+ if (iminPrintUtils != null) {
583
+ iminPrintUtils.setBarCodeHeight(height);
584
+ } else {
585
+ PrinterHelper.getInstance().setBarCodeHeight(height);
586
+ }
587
+ promise.resolve(null);
588
+ } catch (Exception e) {
589
+ promise.reject("setBarCodeHeight_failed", e.getMessage());
590
+ }
591
+ }
592
+
593
+ @ReactMethod
594
+ public void setBarCodeContentPrintPos(int position, final Promise promise) {
595
+ try {
596
+ if (iminPrintUtils != null) {
597
+ iminPrintUtils.setBarCodeContentPrintPos(position);
598
+ } else {
599
+ PrinterHelper.getInstance().setBarCodeContentPrintPos(position);
600
+ }
601
+ promise.resolve(null);
602
+ } catch (Exception e) {
603
+ promise.reject("setBarCodeContentPrintPos_failed", e.getMessage());
604
+ }
605
+ }
606
+
607
+ @ReactMethod
608
+ public void printBarCode(ReadableMap config, final Promise promise) {
609
+ try {
610
+ String barCodeContent = config.getString("data");
611
+ int barCodeType = config.getInt("type");
612
+ if (iminPrintUtils != null) {
613
+ if (config.hasKey("align")) {
614
+ int barCodeAlign = config.getInt("align");
615
+ iminPrintUtils.printBarCode(barCodeType, barCodeContent, barCodeAlign);
616
+ } else {
617
+ iminPrintUtils.printBarCode(barCodeType, barCodeContent);
618
+ }
619
+ } else {
620
+ if (config.hasKey("align")) {
621
+ int barCodeAlign = config.getInt("align");
622
+ if (config.hasKey("position") && config.hasKey("height") && config.hasKey("width")) {
623
+ int position = config.getInt("position");
624
+ int height = config.getInt("height");
625
+ int width = config.getInt("width");
626
+ PrinterHelper.getInstance().printBarCodeWithFull(barCodeContent, barCodeType, width, height, barCodeAlign, position, null);
627
+ } else {
628
+ PrinterHelper.getInstance().printBarCodeWithAlign(barCodeContent, barCodeType, barCodeAlign, null);
629
+ }
630
+ } else {
631
+ PrinterHelper.getInstance().printBarCode(barCodeContent, barCodeType, null);
632
+ }
633
+ }
634
+ promise.resolve(null);
635
+ } catch (Exception e) {
636
+ promise.reject("printBarCode_failed", e.getMessage());
637
+ }
638
+
639
+ }
640
+
641
+ @ReactMethod
642
+ public void setDoubleQRSize(int size, final Promise promise) {
643
+ try {
644
+ if (iminPrintUtils != null) {
645
+ iminPrintUtils.setDoubleQRSize(size);
646
+ } else {
647
+ PrinterHelper.getInstance().setDoubleQRSize(size);
648
+ }
649
+ promise.resolve(null);
650
+ } catch (Exception e) {
651
+ promise.reject("setDoubleQRSize_failed", e.getMessage());
652
+ }
653
+ }
654
+
655
+ @ReactMethod
656
+ public void setDoubleQR1Level(int level, final Promise promise) {
657
+ try {
658
+ if (iminPrintUtils != null) {
659
+ iminPrintUtils.setDoubleQR1Level(level);
660
+ } else {
661
+ PrinterHelper.getInstance().setDoubleQR1Level(level);
662
+ }
663
+ promise.resolve(null);
664
+ } catch (Exception e) {
665
+ promise.reject("setDoubleQR1Level_failed", e.getMessage());
666
+ }
667
+ }
668
+
669
+ @ReactMethod
670
+ public void setDoubleQR2Level(int level, final Promise promise) {
671
+ try {
672
+ if (iminPrintUtils != null) {
673
+ iminPrintUtils.setDoubleQR2Level(level);
674
+ } else {
675
+ PrinterHelper.getInstance().setDoubleQR2Level(level);
676
+ }
677
+ promise.resolve(null);
678
+ } catch (Exception e) {
679
+ promise.reject("setDoubleQR2Level_failed", e.getMessage());
680
+ }
681
+ }
682
+
683
+ @ReactMethod
684
+ public void setDoubleQR1MarginLeft(int leftMargin, final Promise promise) {
685
+ try {
686
+ if (iminPrintUtils != null) {
687
+ iminPrintUtils.setDoubleQR1MarginLeft(leftMargin);
688
+ } else {
689
+ PrinterHelper.getInstance().setDoubleQR1MarginLeft(leftMargin);
690
+ }
691
+ promise.resolve(null);
692
+ } catch (Exception e) {
693
+ promise.reject("setDoubleQR1MarginLeft_failed", e.getMessage());
694
+ }
695
+ }
696
+
697
+ @ReactMethod
698
+ public void setDoubleQR2MarginLeft(int leftMargin, final Promise promise) {
699
+ try {
700
+ if (iminPrintUtils != null) {
701
+ iminPrintUtils.setDoubleQR2MarginLeft(leftMargin);
702
+ } else {
703
+ PrinterHelper.getInstance().setDoubleQR2MarginLeft(leftMargin);
704
+ }
705
+ promise.resolve(null);
706
+ } catch (Exception e) {
707
+ promise.reject("setDoubleQR2MarginLeft_failed", e.getMessage());
708
+ }
709
+ }
710
+
711
+ @ReactMethod
712
+ public void setQrCodeErrorCorrectionLev(int level, final Promise promise) {
713
+ try {
714
+ if (iminPrintUtils != null) {
715
+ iminPrintUtils.setQrCodeErrorCorrectionLev(level);
716
+ } else {
717
+ PrinterHelper.getInstance().setQrCodeErrorCorrectionLev(level);
718
+ }
719
+ promise.resolve(null);
720
+ } catch (Exception e) {
721
+ promise.reject("setQrCodeErrorCorrectionLev_failed", e.getMessage());
722
+ }
723
+ }
724
+
725
+ @ReactMethod
726
+ public void setDoubleQR1Version(int version, final Promise promise) {
727
+ try {
728
+ if (iminPrintUtils != null) {
729
+ iminPrintUtils.setDoubleQR1Version(version);
730
+ } else {
731
+ PrinterHelper.getInstance().setDoubleQR1Version(version);
732
+ }
733
+ promise.resolve(null);
734
+ } catch (Exception e) {
735
+ promise.reject("setDoubleQR1Version_failed", e.getMessage());
736
+ }
737
+ }
738
+
739
+ @ReactMethod
740
+ public void setDoubleQR2Version(int version, final Promise promise) {
741
+ try {
742
+ if (iminPrintUtils != null) {
743
+ iminPrintUtils.setDoubleQR2Version(version);
744
+ } else {
745
+ PrinterHelper.getInstance().setDoubleQR2Version(version);
746
+ }
747
+ promise.resolve(null);
748
+ } catch (Exception e) {
749
+ promise.reject("setDoubleQR2Version_failed", e.getMessage());
750
+ }
751
+ }
752
+
753
+ @ReactMethod
754
+ public void printQrCode(ReadableMap config, final Promise promise) {
755
+ try {
756
+ String qrStr = config.getString("data");
757
+ if (config.hasKey("align")) {
758
+ int align = config.getInt("align");
759
+ if (iminPrintUtils != null) {
760
+ iminPrintUtils.printQrCode(qrStr, align);
761
+ } else {
762
+ if (config.hasKey("qrSize") && config.hasKey("level")) {
763
+ int qrSize = config.getInt("qrSize");
764
+ int qrLevel = config.getInt("level");
765
+ PrinterHelper.getInstance().printQRCodeWithFull(qrStr, qrSize, qrLevel, align, null);
766
+ } else {
767
+ PrinterHelper.getInstance().printQrCodeWithAlign(qrStr, align, null);
768
+ }
769
+ }
770
+ } else {
771
+ if (iminPrintUtils != null) {
772
+ iminPrintUtils.printQrCode(qrStr);
773
+ } else {
774
+ PrinterHelper.getInstance().printQrCode(qrStr, null);
775
+ }
776
+ }
777
+ promise.resolve(null);
778
+ } catch (Exception e) {
779
+ promise.reject("printQrCode_failed", e.getMessage());
780
+ }
781
+ }
782
+
783
+ @ReactMethod
784
+ public void printDoubleQR(ReadableMap config, final Promise promise) {
785
+ try {
786
+ String qrCode1Text = config.getString("qrCode1Text");
787
+ String qrCode2Text = config.getString("qrCode2Text");
788
+ if (iminPrintUtils != null) {
789
+ iminPrintUtils.printDoubleQR(qrCode1Text, qrCode2Text);
790
+ } else {
791
+ PrinterHelper.getInstance().printDoubleQR(qrCode1Text, qrCode2Text, null);
792
+ }
793
+ promise.resolve(null);
794
+ } catch (Exception e) {
795
+ promise.reject("printDoubleQR_failed", e.getMessage());
796
+ }
797
+ }
798
+
799
+ @ReactMethod
800
+ public void openCashBox(final Promise promise) {
801
+ try {
802
+ if (iminPrintUtils != null) {
803
+ Utils.getInstance().opencashBox();
804
+ } else {
805
+ PrinterHelper.getInstance().openDrawer();
806
+ }
807
+ promise.resolve(null);
808
+ } catch (Exception e) {
809
+ promise.reject("openCashBox_failed", e.getMessage());
810
+ }
811
+
812
+ }
813
+
814
+
815
+ @ReactMethod
816
+ public void setInitIminPrinter(boolean isDefault, final Promise promise) {
817
+ try {
818
+ if (iminPrintUtils != null) {
819
+ iminPrintUtils.setInitIminPrinter(isDefault);
820
+ }
821
+ promise.resolve(null);
822
+ } catch (Exception e) {
823
+ promise.reject("setInitIminPrinter_failed", e.getMessage());
824
+ }
825
+ }
826
+
827
+ @ReactMethod
828
+ public void resetDevice(final Promise promise) {
829
+ try {
830
+ if (iminPrintUtils != null) {
831
+ iminPrintUtils.resetDevice();
832
+ }
833
+ promise.resolve(null);
834
+ } catch (Exception e) {
835
+ promise.reject("resetDevice_failed", e.getMessage());
836
+ }
837
+ }
838
+
839
+ @ReactMethod
840
+ public void printerSelfChecking(final Promise promise) {
841
+ try {
842
+ if (iminPrintUtils == null) {
843
+ PrinterHelper.getInstance().printerSelfChecking(null);
844
+ }
845
+ promise.resolve(null);
846
+ } catch (Exception e) {
847
+ promise.reject("printerSelfChecking_failed", e.getMessage());
848
+ }
849
+ }
850
+
851
+ @ReactMethod
852
+ public void openLogs(int open, final Promise promise) {
853
+ try {
854
+ if (iminPrintUtils != null) {
855
+ iminPrintUtils.setIsOpenLog(open);
856
+ }
857
+ promise.resolve(null);
858
+ } catch (Exception e) {
859
+ promise.reject("setIsOpenLog_failed", e.getMessage());
860
+ }
861
+ }
862
+
863
+ @ReactMethod
864
+ public void sendRAWDataHexStr(String byteStr, final Promise promise) {
865
+ try {
866
+ if (iminPrintUtils != null) {
867
+ iminPrintUtils.sendRAWData(byteStr);
868
+ }
869
+ promise.resolve(null);
870
+ } catch (Exception e) {
871
+ promise.reject("sendRAWDataHexStr_failed", e.getMessage());
872
+ }
873
+ }
874
+
875
+ @ReactMethod
876
+ public void printSingleBitmapColorChart(ReadableMap config, final Promise promise) {
877
+ try {
878
+ String url = config.getString("url");
879
+ if (config.hasKey("width") && config.hasKey("height")) {
880
+ int width = config.getInt("width");
881
+ int height = config.getInt("height");
882
+ Bitmap image = Glide.with(reactContext).asBitmap().load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit(width, height).get();
883
+ if (image.isRecycled()) {
884
+ return;
885
+ }
886
+ if (iminPrintUtils == null) {
887
+ if (config.hasKey("align")) {
888
+ int align = config.getInt("align");
889
+ PrinterHelper.getInstance().printBitmapColorChartWithAlign(image, align, null);
890
+ } else {
891
+ PrinterHelper.getInstance().printBitmapColorChart(image, null);
892
+ }
893
+ }
894
+ } else {
895
+ Bitmap image = Glide.with(reactContext).asBitmap().load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit().get();
896
+ if (image.isRecycled()) {
897
+ return;
898
+ }
899
+ if (iminPrintUtils == null) {
900
+ if (config.hasKey("align")) {
901
+ int align = config.getInt("align");
902
+ PrinterHelper.getInstance().printBitmapColorChartWithAlign(image, align, null);
903
+ } else {
904
+ PrinterHelper.getInstance().printBitmapColorChart(image, null);
905
+ }
906
+ }
907
+ }
908
+ promise.resolve(null);
909
+ } catch (Exception e) {
910
+ promise.reject("printSingleBitmap_failed", e.getMessage());
911
+ }
912
+ }
913
+
914
+
915
+ @ReactMethod
916
+ public void sendRAWData(ReadableArray bytes, final Promise promise) {
917
+ try {
918
+ // 处理接收到的 Uint8Array 数据
919
+ byte[] rawData = new byte[bytes.size()];
920
+ for (int i = 0; i < bytes.size(); i++) {
921
+ rawData[i] = (byte) bytes.getInt(i);
922
+ }
923
+ if (iminPrintUtils == null) {
924
+ PrinterHelper.getInstance().sendRAWData(rawData, null);
925
+ }
926
+ promise.resolve(null);
927
+ } catch (Exception e) {
928
+ promise.reject("sendRAWData_failed", e.getMessage());
929
+ }
930
+ }
931
+
932
+ @ReactMethod
933
+ public void printColumnsString(ReadableArray cols, final Promise promise) {
934
+ try {
935
+ String[] colsText = new String[cols.size()];
936
+ int[] colsWidth = new int[cols.size()];
937
+ int[] colsAlign = new int[cols.size()];
938
+ int[] colsFontSize = new int[cols.size()];
939
+ for (int i = 0; i < cols.size(); i++) {
940
+ ReadableMap col = cols.getMap(i);
941
+ String textColumn = col.getString("text");
942
+ int widthColumn = col.getInt("width");
943
+ int alignColumn = col.getInt("align");
944
+ int fontSizeColumn = col.getInt("fontSize");
945
+ colsText[i] = textColumn;
946
+ colsWidth[i] = widthColumn;
947
+ colsAlign[i] = alignColumn;
948
+ colsFontSize[i] = fontSizeColumn;
949
+ }
950
+ if (iminPrintUtils == null) {
951
+ PrinterHelper.getInstance().printColumnsString(colsText, colsWidth, colsAlign, colsFontSize, null);
952
+ }
953
+ promise.resolve(null);
954
+ } catch (Exception e) {
955
+ promise.reject("printColumnsString_failed", e.getMessage());
956
+ }
957
+ }
958
+
959
+ @ReactMethod
960
+ public void unBindService(final Promise promise) {
961
+ try {
962
+ if (iminPrintUtils == null) {
963
+ PrinterHelper.getInstance().deInitPrinterService(reactContext);
964
+ }
965
+ } catch (Exception e) {
966
+ promise.reject("unBindService_failed", e.getMessage());
967
+ }
968
+ }
969
+
970
+ @ReactMethod
971
+ public void initPrinterParams(final Promise promise) {
972
+ try {
973
+ if (iminPrintUtils == null) {
974
+ PrinterHelper.getInstance().initPrinterParams();
975
+ }
976
+ promise.resolve(null);
977
+ } catch (Exception e) {
978
+ promise.reject("initPrinterParams_failed", e.getMessage());
979
+ }
980
+ }
981
+
982
+ @ReactMethod
983
+ public void getFontCodepage(final Promise promise) {
984
+ try {
985
+ if (iminPrintUtils == null) {
986
+ List<String> fontCodepage = PrinterHelper.getInstance().getFontCodepage();
987
+ promise.resolve(fontCodepage);
988
+ } else {
989
+ promise.resolve(null);
990
+ }
991
+ } catch (Exception e) {
992
+ promise.reject("getFontCodepage_failed", e.getMessage());
993
+ }
994
+ }
995
+
996
+ @ReactMethod
997
+ public void setFontCodepage(int codepage, final Promise promise) {
998
+ try {
999
+ if (iminPrintUtils == null) {
1000
+ PrinterHelper.getInstance().setFontCodepage(codepage);
1001
+ }
1002
+ promise.resolve(null);
1003
+ } catch (Exception e) {
1004
+ promise.reject("setFontCodepage_failed", e.getMessage());
1005
+ }
1006
+ }
1007
+
1008
+ @ReactMethod
1009
+ public void getCurCodepage(final Promise promise) {
1010
+ try {
1011
+ if (iminPrintUtils == null) {
1012
+ String curCodepage = PrinterHelper.getInstance().getCurCodepage();
1013
+ promise.resolve(curCodepage);
1014
+ } else {
1015
+ promise.resolve(null);
1016
+ }
1017
+ } catch (Exception e) {
1018
+ promise.reject("getCurCodepage_failed", e.getMessage());
1019
+ }
1020
+ }
1021
+
1022
+ @ReactMethod
1023
+ public void getEncodeList(final Promise promise) {
1024
+ try {
1025
+ if (iminPrintUtils == null) {
1026
+ List<String> encodeList = PrinterHelper.getInstance().getEncodeList();
1027
+ promise.resolve(encodeList);
1028
+ } else {
1029
+ promise.resolve(null);
1030
+ }
1031
+ } catch (Exception e) {
1032
+ promise.reject("getEncodeList_failed", e.getMessage());
1033
+ }
1034
+ }
1035
+
1036
+ @ReactMethod
1037
+ public void setPrinterEncode(int encode, final Promise promise) {
1038
+ try {
1039
+ if (iminPrintUtils == null) {
1040
+ PrinterHelper.getInstance().setPrinterEncode(encode);
1041
+ }
1042
+ promise.resolve(null);
1043
+ } catch (Exception e) {
1044
+ promise.reject("setPrinterEncode_failed", e.getMessage());
1045
+ }
1046
+ }
1047
+
1048
+ @ReactMethod
1049
+ public void getCurEncode(final Promise promise) {
1050
+ try {
1051
+ if (iminPrintUtils == null) {
1052
+ String curEncode = PrinterHelper.getInstance().getCurEncode();
1053
+ promise.resolve(curEncode);
1054
+ } else {
1055
+ promise.resolve(null);
1056
+ }
1057
+ } catch (Exception e) {
1058
+ promise.reject("getCurEncode_failed", e.getMessage());
1059
+ }
1060
+ }
1061
+
1062
+ @ReactMethod
1063
+ public void getPrinterDensityList(final Promise promise) {
1064
+ try {
1065
+ if (iminPrintUtils == null) {
1066
+ List<String> printerDensityList = PrinterHelper.getInstance().getPrinterDensityList();
1067
+ promise.resolve(printerDensityList);
1068
+ } else {
1069
+ promise.resolve(null);
1070
+ }
1071
+ } catch (Exception e) {
1072
+ promise.reject("getPrinterDensityList_failed", e.getMessage());
1073
+ }
1074
+ }
1075
+
1076
+ @ReactMethod
1077
+ public void setPrinterDensity(int density, final Promise promise) {
1078
+ try {
1079
+ if (iminPrintUtils == null) {
1080
+ PrinterHelper.getInstance().setPrinterDensity(density);
1081
+ }
1082
+ promise.resolve(null);
1083
+ } catch (Exception e) {
1084
+ promise.reject("setPrinterDensity_failed", e.getMessage());
1085
+ }
1086
+ }
1087
+
1088
+ @ReactMethod
1089
+ public void getPrinterDensity(final Promise promise) {
1090
+ try {
1091
+ if (iminPrintUtils == null) {
1092
+ promise.resolve(PrinterHelper.getInstance().getPrinterDensity());
1093
+ } else {
1094
+ promise.resolve(null);
1095
+ }
1096
+
1097
+ } catch (Exception e) {
1098
+ promise.reject("getPrinterDensity_failed", e.getMessage());
1099
+ }
1100
+ }
1101
+
1102
+ @ReactMethod
1103
+ public void getPrinterSpeedList(final Promise promise) {
1104
+ try {
1105
+ if (iminPrintUtils == null) {
1106
+ List<String> printerSpeedList = PrinterHelper.getInstance().getPrinterSpeedList();
1107
+ promise.resolve(printerSpeedList);
1108
+ } else {
1109
+ promise.resolve(null);
1110
+ }
1111
+
1112
+ } catch (Exception e) {
1113
+ promise.reject("getPrinterSpeedList_failed", e.getMessage());
1114
+ }
1115
+ }
1116
+
1117
+ @ReactMethod
1118
+ public void setPrinterSpeed(int speed, final Promise promise) {
1119
+ try {
1120
+ if (iminPrintUtils == null) {
1121
+ PrinterHelper.getInstance().setPrinterSpeed(speed);
1122
+ }
1123
+ promise.resolve(null);
1124
+ } catch (Exception e) {
1125
+ promise.reject("setPrinterSpeed_failed", e.getMessage());
1126
+ }
1127
+ }
1128
+
1129
+ @ReactMethod
1130
+ public void getPrinterSpeed(final Promise promise) {
1131
+ try {
1132
+ if (iminPrintUtils == null) {
1133
+ int printerSpeed = PrinterHelper.getInstance().getPrinterSpeed();
1134
+ promise.resolve(printerSpeed);
1135
+ } else {
1136
+ promise.resolve(null);
1137
+ }
1138
+ } catch (Exception e) {
1139
+ promise.reject("getPrinterSpeed_failed", e.getMessage());
1140
+ }
1141
+ }
1142
+
1143
+ @ReactMethod
1144
+ public void getPrinterPaperTypeList(final Promise promise) {
1145
+ try {
1146
+ if (iminPrintUtils == null) {
1147
+ List<String> printerPaperTypeList = PrinterHelper.getInstance().getPrinterPaperTypeList();
1148
+ promise.resolve(printerPaperTypeList);
1149
+ } else {
1150
+ promise.resolve(null);
1151
+ }
1152
+ } catch (Exception e) {
1153
+ promise.reject("getPrinterPaperTypeList_failed", e.getMessage());
1154
+ }
1155
+ }
1156
+
1157
+ @ReactMethod
1158
+ public void getPrinterPaperType(final Promise promise) {
1159
+ try {
1160
+ if (iminPrintUtils == null) {
1161
+ promise.resolve(PrinterHelper.getInstance().getPrinterPaperType());
1162
+ } else {
1163
+ promise.resolve(null);
1164
+ }
1165
+ } catch (Exception e) {
1166
+ promise.reject("getPrinterPaperType_failed", e.getMessage());
1167
+ }
1168
+ }
1169
+
1170
+ @ReactMethod
1171
+ public void getPrinterSerialNumber(final Promise promise) {
1172
+ try {
1173
+ if (iminPrintUtils == null) {
1174
+ PrinterHelper.getInstance().getPrinterSerialNumber(new INeoPrinterCallback() {
1175
+ @Override
1176
+ public void onRunResult(boolean isSuccess) throws RemoteException {
1177
+ }
1178
+
1179
+ @Override
1180
+ public void onReturnString(String s) throws RemoteException {
1181
+ promise.resolve(s);
1182
+ }
1183
+
1184
+ @Override
1185
+ public void onRaiseException(int code, String msg) throws RemoteException {
1186
+ }
1187
+
1188
+ @Override
1189
+ public void onPrintResult(int code, String msg) throws RemoteException {
1190
+ }
1191
+ });
1192
+ } else {
1193
+ promise.resolve(null);
1194
+ }
1195
+ } catch (Exception e) {
1196
+ promise.reject("getPrinterSerialNumber_failed", e.getMessage());
1197
+ }
1198
+ }
1199
+
1200
+ @ReactMethod
1201
+ public void getPrinterModelName(final Promise promise) {
1202
+ try {
1203
+ if (iminPrintUtils == null) {
1204
+ PrinterHelper.getInstance().getPrinterModelName(new INeoPrinterCallback() {
1205
+ @Override
1206
+ public void onRunResult(boolean isSuccess) throws RemoteException {
1207
+ }
1208
+
1209
+ @Override
1210
+ public void onReturnString(String s) throws RemoteException {
1211
+ promise.resolve(s);
1212
+ }
1213
+
1214
+ @Override
1215
+ public void onRaiseException(int code, String msg) throws RemoteException {
1216
+ }
1217
+
1218
+ @Override
1219
+ public void onPrintResult(int code, String msg) throws RemoteException {
1220
+ }
1221
+ });
1222
+ } else {
1223
+ promise.resolve(null);
1224
+ }
1225
+ } catch (Exception e) {
1226
+ promise.reject("getPrinterModelName_failed", e.getMessage());
1227
+ }
1228
+ }
1229
+
1230
+ @ReactMethod
1231
+ public void getPrinterThermalHead(final Promise promise) {
1232
+ try {
1233
+ if (iminPrintUtils == null) {
1234
+ PrinterHelper.getInstance().getPrinterThermalHead(new INeoPrinterCallback() {
1235
+ @Override
1236
+ public void onRunResult(boolean isSuccess) throws RemoteException {
1237
+ }
1238
+
1239
+ @Override
1240
+ public void onReturnString(String s) throws RemoteException {
1241
+ promise.resolve(s);
1242
+ }
1243
+
1244
+ @Override
1245
+ public void onRaiseException(int code, String msg) throws RemoteException {
1246
+ }
1247
+
1248
+ @Override
1249
+ public void onPrintResult(int code, String msg) throws RemoteException {
1250
+ }
1251
+ });
1252
+ } else {
1253
+ promise.resolve(null);
1254
+ }
1255
+ } catch (Exception e) {
1256
+ promise.reject("getPrinterThermalHead_failed", e.getMessage());
1257
+ }
1258
+ }
1259
+
1260
+ @ReactMethod
1261
+ public void getPrinterFirmwareVersion(final Promise promise) {
1262
+ try {
1263
+ if (iminPrintUtils == null) {
1264
+ PrinterHelper.getInstance().getPrinterFirmwareVersion(new INeoPrinterCallback() {
1265
+ @Override
1266
+ public void onRunResult(boolean isSuccess) throws RemoteException {
1267
+ }
1268
+
1269
+ @Override
1270
+ public void onReturnString(String s) throws RemoteException {
1271
+ promise.resolve(s);
1272
+ }
1273
+
1274
+ @Override
1275
+ public void onRaiseException(int code, String msg) throws RemoteException {
1276
+ }
1277
+
1278
+ @Override
1279
+ public void onPrintResult(int code, String msg) throws RemoteException {
1280
+ }
1281
+ });
1282
+ } else {
1283
+ promise.resolve(null);
1284
+ }
1285
+ } catch (Exception e) {
1286
+ promise.reject("getPrinterFirmwareVersion_failed", e.getMessage());
1287
+ }
1288
+ }
1289
+
1290
+ @ReactMethod
1291
+ public void getPrinterHardwareVersion(final Promise promise) {
1292
+ try {
1293
+ if (iminPrintUtils == null) {
1294
+ PrinterHelper.getInstance().getPrinterHardwareVersion(new INeoPrinterCallback() {
1295
+ @Override
1296
+ public void onRunResult(boolean isSuccess) throws RemoteException {
1297
+ }
1298
+
1299
+ @Override
1300
+ public void onReturnString(String s) throws RemoteException {
1301
+ promise.resolve(s);
1302
+ }
1303
+
1304
+ @Override
1305
+ public void onRaiseException(int code, String msg) throws RemoteException {
1306
+ }
1307
+
1308
+ @Override
1309
+ public void onPrintResult(int code, String msg) throws RemoteException {
1310
+ }
1311
+ });
1312
+ } else {
1313
+ promise.resolve(null);
1314
+ }
1315
+ } catch (Exception e) {
1316
+ promise.reject("getPrinterHardwareVersion_failed", e.getMessage());
1317
+ }
1318
+ }
1319
+
1320
+ @ReactMethod
1321
+ public void getPrinterPaperDistance(final Promise promise) {
1322
+ try {
1323
+ if (iminPrintUtils == null) {
1324
+ PrinterHelper.getInstance().getPrinterPaperDistance(new INeoPrinterCallback() {
1325
+ @Override
1326
+ public void onRunResult(boolean isSuccess) throws RemoteException {
1327
+ }
1328
+
1329
+ @Override
1330
+ public void onReturnString(String s) throws RemoteException {
1331
+ promise.resolve(s);
1332
+ }
1333
+
1334
+ @Override
1335
+ public void onRaiseException(int code, String msg) throws RemoteException {
1336
+ }
1337
+
1338
+ @Override
1339
+ public void onPrintResult(int code, String msg) throws RemoteException {
1340
+ }
1341
+ });
1342
+ } else {
1343
+ promise.resolve(null);
1344
+ }
1345
+ } catch (Exception e) {
1346
+ promise.reject("getPrinterPaperDistance_failed", e.getMessage());
1347
+ }
1348
+ }
1349
+
1350
+ @ReactMethod
1351
+ public void getPrinterCutTimes(final Promise promise) {
1352
+ try {
1353
+ if (iminPrintUtils == null) {
1354
+ PrinterHelper.getInstance().getPrinterCutTimes(new INeoPrinterCallback() {
1355
+ @Override
1356
+ public void onRunResult(boolean isSuccess) throws RemoteException {
1357
+ }
1358
+
1359
+ @Override
1360
+ public void onReturnString(String s) throws RemoteException {
1361
+ promise.resolve(s);
1362
+ }
1363
+
1364
+ @Override
1365
+ public void onRaiseException(int code, String msg) throws RemoteException {
1366
+ }
1367
+
1368
+ @Override
1369
+ public void onPrintResult(int code, String msg) throws RemoteException {
1370
+ }
1371
+ });
1372
+ } else {
1373
+ promise.resolve(null);
1374
+ }
1375
+ } catch (Exception e) {
1376
+ promise.reject("getPrinterCutTimes_failed", e.getMessage());
1377
+ }
1378
+ }
1379
+
1380
+
1381
+ @ReactMethod
1382
+ public void getPrinterMode(final Promise promise) {
1383
+ try {
1384
+ if (iminPrintUtils == null) {
1385
+ Log.e("IminPrinter", "getPrintModel: 获取当前打印机模式");
1386
+ // WritableMap payload = Arguments.createMap();
1387
+ PrinterHelper.getInstance().labelGetPrinterMode(new INeoPrinterCallback() {
1388
+ @Override
1389
+ public void onRunResult(boolean isSuccess) throws RemoteException {
1390
+
1391
+ }
1392
+
1393
+ @Override
1394
+ public void onReturnString(String string) throws RemoteException {
1395
+ Log.e("IminPrinter", "getPrintModel: 获取当前打印机模式" + string);
1396
+ if (string != null && !string.isEmpty()) {
1397
+ // payload.putString("result",string);
1398
+ if (string.equalsIgnoreCase("Label")) {
1399
+ promise.resolve(1);
1400
+ } else {
1401
+ promise.resolve(0);
1402
+ }
1403
+ }
1404
+ }
1405
+
1406
+ @Override
1407
+ public void onRaiseException(int code, String msg) throws RemoteException {
1408
+ }
1409
+
1410
+ @Override
1411
+ public void onPrintResult(int code, String msg) throws RemoteException {
1412
+ }
1413
+
1414
+ @Override
1415
+ public IBinder asBinder() {
1416
+ return null;
1417
+ }
1418
+ });
1419
+
1420
+ // promise.resolve(PrinterHelper.getInstance().getPrinterMode());
1421
+ } else {
1422
+ promise.resolve(null);
1423
+ }
1424
+ } catch (Exception e) {
1425
+ promise.reject("getPrinterMode_failed", e.getMessage());
1426
+ }
1427
+ }
1428
+
1429
+ @ReactMethod
1430
+ public void getDrawerStatus(final Promise promise) {
1431
+ try {
1432
+ if (iminPrintUtils == null) {
1433
+ promise.resolve(PrinterHelper.getInstance().getDrawerStatus());
1434
+ } else {
1435
+ promise.resolve(null);
1436
+ }
1437
+ } catch (Exception e) {
1438
+ promise.reject("getDrawerStatus_failed", e.getMessage());
1439
+ }
1440
+ }
1441
+
1442
+ @ReactMethod
1443
+ public void getOpenDrawerTimes(final Promise promise) {
1444
+ try {
1445
+ if (iminPrintUtils == null) {
1446
+ promise.resolve(PrinterHelper.getInstance().getOpenDrawerTimes());
1447
+ } else {
1448
+ promise.resolve(null);
1449
+ }
1450
+ } catch (Exception e) {
1451
+ promise.reject("getOpenDrawerTimes_failed", e.getMessage());
1452
+ }
1453
+ }
1454
+
1455
+ @ReactMethod
1456
+ public void getServiceVersion(final Promise promise) {
1457
+ try {
1458
+ if (iminPrintUtils == null) {
1459
+ promise.resolve(PrinterHelper.getInstance().getServiceVersion());
1460
+ } else {
1461
+ promise.resolve(null);
1462
+ }
1463
+ } catch (Exception e) {
1464
+ promise.reject("getServiceVersion_failed", e.getMessage());
1465
+ }
1466
+ }
1467
+
1468
+ @ReactMethod
1469
+ public void getUsbPrinterVidPid(final Promise promise) {
1470
+ try {
1471
+ if (iminPrintUtils == null) {
1472
+ promise.resolve(PrinterHelper.getInstance().getUsbPrinterVidPid());
1473
+ } else {
1474
+ promise.resolve(null);
1475
+ }
1476
+ } catch (Exception e) {
1477
+ promise.reject("getUsbPrinterVidPid_failed", e.getMessage());
1478
+ }
1479
+ }
1480
+
1481
+ @ReactMethod
1482
+ public void getUsbDevicesName(final Promise promise) {
1483
+ try {
1484
+ if (iminPrintUtils == null) {
1485
+ promise.resolve(PrinterHelper.getInstance().getUsbDevicesName());
1486
+ } else {
1487
+ promise.resolve(null);
1488
+ }
1489
+ } catch (Exception e) {
1490
+ promise.reject("getUsbDevicesName_failed", e.getMessage());
1491
+ }
1492
+ }
1493
+
1494
+
1495
+ @ReactMethod
1496
+ public void setCodeAlignment(int align, final Promise promise) {
1497
+ try {
1498
+ if (iminPrintUtils == null) {
1499
+ PrinterHelper.getInstance().setCodeAlignment(align);
1500
+ }
1501
+ promise.resolve(null);
1502
+ } catch (Exception e) {
1503
+ promise.reject("setCodeAlignment_failed", e.getMessage());
1504
+ }
1505
+ }
1506
+
1507
+ @ReactMethod
1508
+ public void setTextBitmapTypeface(int typeface, final Promise promise) {
1509
+ try {
1510
+ if (iminPrintUtils == null) {
1511
+ switch (typeface) {
1512
+ case 1:
1513
+ PrinterHelper.getInstance().setTextBitmapTypeface("Typeface.MONOSPACE");
1514
+ break;
1515
+ case 2:
1516
+ PrinterHelper.getInstance().setTextBitmapTypeface("Typeface.DEFAULT_BOLD");
1517
+ break;
1518
+ case 3:
1519
+ PrinterHelper.getInstance().setTextBitmapTypeface("Typeface.SANS_SERIF");
1520
+ break;
1521
+ case 4:
1522
+ PrinterHelper.getInstance().setTextBitmapTypeface("Typeface.SERIF");
1523
+ break;
1524
+ default:
1525
+ PrinterHelper.getInstance().setTextBitmapTypeface("Typeface.DEFAULT");
1526
+ break;
1527
+ }
1528
+ }
1529
+ promise.resolve(null);
1530
+ } catch (Exception e) {
1531
+ promise.reject("setTextBitmapTypeface_failed", e.getMessage());
1532
+ }
1533
+ }
1534
+
1535
+ @ReactMethod
1536
+ public void setTextBitmapSize(int size, final Promise promise) {
1537
+ try {
1538
+ if (iminPrintUtils == null) {
1539
+ PrinterHelper.getInstance().setTextBitmapSize(size);
1540
+ }
1541
+ promise.resolve(null);
1542
+ } catch (Exception e) {
1543
+ promise.reject("setTextBitmapSize_failed", e.getMessage());
1544
+ }
1545
+ }
1546
+
1547
+ @ReactMethod
1548
+ public void setTextBitmapStyle(int style, final Promise promise) {
1549
+ try {
1550
+ if (iminPrintUtils == null) {
1551
+ PrinterHelper.getInstance().setTextBitmapStyle(style);
1552
+ }
1553
+ promise.resolve(null);
1554
+ } catch (Exception e) {
1555
+ promise.reject("setTextBitmapStyle_failed", e.getMessage());
1556
+ }
1557
+ }
1558
+
1559
+ @ReactMethod
1560
+ public void setTextBitmapStrikeThru(boolean strikeThru, final Promise promise) {
1561
+ try {
1562
+ if (iminPrintUtils == null) {
1563
+ PrinterHelper.getInstance().setTextBitmapStrikeThru(strikeThru);
1564
+ }
1565
+ promise.resolve(null);
1566
+ } catch (Exception e) {
1567
+ promise.reject("setTextBitmapStrikeThru_failed", e.getMessage());
1568
+ }
1569
+ }
1570
+
1571
+ @ReactMethod
1572
+ public void setTextBitmapUnderline(boolean haveUnderline, final Promise promise) {
1573
+ try {
1574
+ if (iminPrintUtils == null) {
1575
+ PrinterHelper.getInstance().setTextBitmapUnderline(haveUnderline);
1576
+ }
1577
+ promise.resolve(null);
1578
+ } catch (Exception e) {
1579
+ promise.reject("setTextBitmapUnderline_failed", e.getMessage());
1580
+ }
1581
+ }
1582
+
1583
+ @ReactMethod
1584
+ public void setTextBitmapLineSpacing(float lineHeight, final Promise promise) {
1585
+ try {
1586
+ if (iminPrintUtils == null) {
1587
+ PrinterHelper.getInstance().setTextBitmapLineSpacing(lineHeight);
1588
+ }
1589
+ promise.resolve(null);
1590
+ } catch (Exception e) {
1591
+ promise.reject("setTextBitmapLineSpacing_failed", e.getMessage());
1592
+ }
1593
+ }
1594
+
1595
+ @ReactMethod
1596
+ public void setTextBitmapLetterSpacing(float space, final Promise promise) {
1597
+ try {
1598
+ if (iminPrintUtils == null) {
1599
+ PrinterHelper.getInstance().setTextBitmapLetterSpacing(space);
1600
+ }
1601
+ promise.resolve(null);
1602
+ } catch (Exception e) {
1603
+ promise.reject("setTextBitmapLetterSpacing_failed", e.getMessage());
1604
+ }
1605
+ }
1606
+
1607
+ @ReactMethod
1608
+ public void setTextBitmapAntiWhite(boolean antiWhite, final Promise promise) {
1609
+ try {
1610
+ if (iminPrintUtils == null) {
1611
+ PrinterHelper.getInstance().setTextBitmapAntiWhite(antiWhite);
1612
+ }
1613
+ promise.resolve(null);
1614
+ } catch (Exception e) {
1615
+ promise.reject("setTextBitmapAntiWhite_failed", e.getMessage());
1616
+ }
1617
+ }
1618
+
1619
+ @ReactMethod
1620
+ public void printTextBitmap(ReadableMap config, final Promise promise) {
1621
+ try {
1622
+ if (iminPrintUtils == null) {
1623
+ String text = config.getString("text");
1624
+ if (config.hasKey("align")) {
1625
+ int align = config.getInt("align");
1626
+ PrinterHelper.getInstance().printTextBitmapWithAli(text + "\n", align, null);
1627
+ } else {
1628
+ PrinterHelper.getInstance().printTextBitmap(text + "\n", null);
1629
+ }
1630
+ }
1631
+ promise.resolve(null);
1632
+ } catch (Exception e) {
1633
+ promise.reject("printTextBitmap_failed", e.getMessage());
1634
+ }
1635
+ }
1636
+
1637
+ @ReactMethod
1638
+ public void enterPrinterBuffer(boolean isClean, final Promise promise) {
1639
+ try {
1640
+ if (iminPrintUtils == null) {
1641
+ PrinterHelper.getInstance().enterPrinterBuffer(isClean);
1642
+ }
1643
+ promise.resolve(null);
1644
+ } catch (Exception e) {
1645
+ promise.reject("enterPrinterBuffer_failed", e.getMessage());
1646
+ }
1647
+ }
1648
+
1649
+ @ReactMethod
1650
+ public void commitPrinterBuffer(final Promise promise) {
1651
+ try {
1652
+ if (iminPrintUtils == null) {
1653
+ PrinterHelper.getInstance().commitPrinterBuffer(null);
1654
+ }
1655
+ promise.resolve(null);
1656
+ } catch (Exception e) {
1657
+ promise.reject("commitPrinterBuffer_failed", e.getMessage());
1658
+ }
1659
+ }
1660
+
1661
+ @ReactMethod
1662
+ public void exitPrinterBuffer(boolean isCommit, final Promise promise) {
1663
+ try {
1664
+ if (iminPrintUtils == null) {
1665
+ PrinterHelper.getInstance().exitPrinterBuffer(isCommit);
1666
+ }
1667
+ promise.resolve(null);
1668
+ } catch (Exception e) {
1669
+ promise.reject("exitPrinterBuffer_failed", e.getMessage());
1670
+ }
1671
+ }
1672
+
1673
+ //标签相关的API
1674
+ //标签初始化
1675
+ @ReactMethod
1676
+ public void labelInitCanvas(ReadableMap config, final Promise promise) {
1677
+ if (iminPrintUtils == null) {
1678
+ Integer width = (Integer) config.getInt("width");
1679
+ Integer height1 = (Integer) config.getInt("height");
1680
+ Integer posX = (Integer) config.getInt("posX");
1681
+ Integer posY = (Integer) config.getInt("posY");
1682
+
1683
+ // 使用解析后的数据
1684
+ Log.e("IminPrinter", "labelInitCanvas:" + width + " " + height1 + " " + posX + " ,y= " + posY);
1685
+ PrinterHelper.getInstance().labelInitCanvas(LabelCanvasStyle.getCanvasStyle()
1686
+ .setWidth(width)
1687
+ .setHeight(height1)
1688
+ .setPosX(posX)
1689
+ .setPosY(posY)
1690
+ );
1691
+
1692
+ promise.resolve(true);
1693
+ }
1694
+ }
1695
+
1696
+ //绘制文本内容
1697
+ @ReactMethod
1698
+ public void labelAddText(ReadableMap config, final Promise promise) {
1699
+ Log.e("IminPrinter", "labelAddText: 点击绘制文本");
1700
+ if (iminPrintUtils == null) {
1701
+ String textLabel = config.getString("text");
1702
+ // 手动解析每个参数
1703
+ Integer posX = (Integer) config.getInt("posX");
1704
+ Integer posY = (Integer) config.getInt("posY");
1705
+ Integer textSize = (Integer) config.getInt("textSize");
1706
+ Integer textWidthRatio = (Integer) config.getInt("textWidthRatio");
1707
+ Integer textHeightRatio = (Integer) config.getInt("textHeightRatio");
1708
+ Integer width = (Integer) config.getInt("width");
1709
+ Integer height1 = (Integer) config.getInt("height");
1710
+ Integer alignStr = config.getInt("align");
1711
+ Integer rotateStr = config.getInt("rotate");
1712
+ Integer textSpace = (Integer) config.getInt("textSpace");
1713
+ Boolean enableBold = (Boolean) config.getBoolean("enableBold");
1714
+ Boolean enableUnderline = (Boolean) config.getBoolean("enableUnderline");
1715
+ Boolean enableStrikethrough = (Boolean) config.getBoolean("enableStrikethrough");
1716
+ Boolean enableItalics = (Boolean) config.getBoolean("enableItalics");
1717
+ Boolean enAntiColor = (Boolean) config.getBoolean("enAntiColor");
1718
+
1719
+ // 将字符串转换为枚举类型
1720
+ Align align = Align.values()[alignStr];
1721
+ Rotate rotate = Rotate.values()[rotateStr];
1722
+
1723
+
1724
+ Log.e("IminPrinter", "labelAddText: 点击绘制文本 " + textLabel + ",width=" +
1725
+ width + " ,height1= " + height1 + " ,posX= " + posX + " ,posY= " + posY +
1726
+ " ,rotateStr= " + rotateStr + " ,enableBold= " + enableBold + " " + align + " " + rotate);
1727
+
1728
+
1729
+ PrinterHelper.getInstance().labelAddText(textLabel, LabelTextStyle.getTextStyle()
1730
+ .setPosX(posX)
1731
+ .setPosY(posY)
1732
+ .setTextSize(textSize)
1733
+ .setTextWidthRatio(textWidthRatio)
1734
+ .setTextHeightRatio(textHeightRatio)
1735
+ .setWidth(width)
1736
+ .setHeight(height1)
1737
+ .setAlign(align)
1738
+ .setRotate(rotate)
1739
+ .setTextSpace(textSpace)
1740
+ .setEnableBold(enableBold)
1741
+ .setEnableUnderline(enableUnderline)
1742
+ .setEnableStrikethrough(enableStrikethrough)
1743
+ .setEnableItalics(enableItalics)
1744
+ .setEnAntiColor(enAntiColor)
1745
+
1746
+ );
1747
+ promise.resolve(true);
1748
+ }
1749
+ }
1750
+
1751
+ //绘制条形码内容
1752
+ @ReactMethod
1753
+ public void labelAddBarCode(ReadableMap config, final Promise promise) {
1754
+ if (iminPrintUtils == null) {
1755
+ String barCode = config.getString("barCode");
1756
+ // 直接从 map 获取各个属性
1757
+ Integer posX = (Integer) config.getInt("posX");
1758
+ Integer posY = (Integer) config.getInt("posY");
1759
+ Integer dotWidth = (Integer) config.getInt("dotWidth");
1760
+ Integer barHeight = (Integer) config.getInt("barHeight");
1761
+ Integer readable = config.getInt("readable");
1762
+ Integer symbology = config.getInt("symbology");
1763
+ Integer alignStr = config.getInt("align");
1764
+ Integer rotateStr = config.getInt("rotate");
1765
+ Integer width = (Integer) config.getInt("width");
1766
+ Integer height1 = (Integer) config.getInt("height");
1767
+
1768
+ Rotate rotate = Rotate.values()[rotateStr];
1769
+ Align align = Align.values()[alignStr];
1770
+ HumanReadable readable1 = HumanReadable.values()[readable];
1771
+ Symbology symbology1 = Symbology.values()[symbology];
1772
+
1773
+ Log.e("IminPrinter", "labelAddBarCode: 绘制条形码内容 " + barCode + ",width=" +
1774
+ width + " ,height1= " + height1 + " ,posX= " + posX + " ,posY= " + posY + " ,readable= " + readable + " ,symbology= " + symbology + " ,HumanReadable=> " + readable1);
1775
+
1776
+ PrinterHelper.getInstance().labelAddBarCode(barCode, LabelBarCodeStyle.getBarCodeStyle()
1777
+ .setPosX(posX)
1778
+ .setPosY(posY)
1779
+ .setSymbology(symbology1)
1780
+ .setDotWidth(dotWidth)
1781
+ .setBarHeight(barHeight)
1782
+ .setReadable(readable1)
1783
+ .setAlign(align)
1784
+ .setRotate(rotate)
1785
+ .setWidth(width)
1786
+ .setHeight(height1)
1787
+ );
1788
+
1789
+ promise.resolve(true);
1790
+ }
1791
+
1792
+ Log.e("IminPrinter", "labelAddBarCode: 绘制条形码内容");
1793
+ }
1794
+
1795
+ //绘制二维码内容
1796
+ @ReactMethod
1797
+ public void labelAddQrCode(ReadableMap config, final Promise promise) {
1798
+ Log.e("IminPrinter", "labelAddQrCode: 绘制二维码内容");
1799
+ if (iminPrintUtils == null) {
1800
+
1801
+ if (config != null) {
1802
+ String qrCode = config.getString("qrCode");
1803
+
1804
+ // 直接从 map 获取各个属性
1805
+ Integer posX = (Integer) config.getInt("posX");
1806
+ Integer posY = (Integer) config.getInt("posY");
1807
+ Integer size1 = (Integer) config.getInt("size");
1808
+ Integer errorLevelStr = config.getInt("errorLevel");
1809
+ Integer rotateStr = config.getInt("rotate");
1810
+ Integer width = (Integer) config.getInt("width");
1811
+ Integer height1 = (Integer) config.getInt("height");
1812
+ Log.e("IminPrinter", "labelAddQrCode: 绘制二维码内容 " + qrCode + ",width=" +
1813
+ width + " ,height1= " + height1 + " ,posX= " + posX + " ,posY= " + posY + " ,errorLevelStr= " + errorLevelStr + " ,size= " + size1);
1814
+
1815
+ Rotate rotate = Rotate.values()[rotateStr];
1816
+ ErrorLevel errorLevel = ErrorLevel.values()[errorLevelStr];
1817
+
1818
+
1819
+ PrinterHelper.getInstance().labelAddQrCode(qrCode, LabelQrCodeStyle.getQrCodeStyle()
1820
+ .setPosX(posX)
1821
+ .setPosY(posY)
1822
+ .setSize(size1)
1823
+ .setErrorLevel(errorLevel)
1824
+ .setRotate(rotate)
1825
+ .setWidth(width)
1826
+ .setHeight(height1)
1827
+ );
1828
+
1829
+ }
1830
+ promise.resolve(true);
1831
+ }
1832
+ }
1833
+
1834
+ //绘制图像
1835
+ @ReactMethod
1836
+ public void labelAddBitmap(ReadableMap config, final Promise promise) {
1837
+ Log.e("IminPrinter", "labelAddBitmap: 绘制图像");
1838
+ if (iminPrintUtils == null) {
1839
+ try {
1840
+ String bitmapUrl = config.getString("bitmapUrl");
1841
+ Integer posX = (Integer) config.getInt("posX");
1842
+ Integer posY = (Integer) config.getInt("posY");
1843
+ Integer algorithm = config.getInt("algorithm");
1844
+ Integer value = (Integer) config.getInt("value");
1845
+ Integer width = (Integer) config.getInt("width");
1846
+ Integer height1 = (Integer) config.getInt("height");
1847
+
1848
+ ImageAlgorithm imageAlgorithm = ImageAlgorithm.values()[algorithm];
1849
+
1850
+ Log.e("IminPrinter", "labelAddBitmap: 绘制图像 " + bitmapUrl + " ,width=" + width + " ,height1= " + height1 + " ,posX= " + posX + " ,posY= " + posY + " ,algorithm= " + algorithm + " ,value= " + value);
1851
+
1852
+ Bitmap bitmap = null;
1853
+ if (bitmapUrl == null || bitmapUrl.isEmpty()) {
1854
+ promise.resolve(false);
1855
+ } else {
1856
+ bitmap = Glide.with(reactContext).asBitmap().load(bitmapUrl).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit(width, height1).get();
1857
+ }
1858
+
1859
+ PrinterHelper.getInstance().labelAddBitmap(bitmap, LabelBitmapStyle.getBitmapStyle()
1860
+ .setPosX(posX)
1861
+ .setPosY(posY)
1862
+ .setAlgorithm(imageAlgorithm)
1863
+ .setValue(value)
1864
+ .setWidth(width)
1865
+ .setHeight(height1)
1866
+ );
1867
+ promise.resolve(true);
1868
+ }catch (Exception e){
1869
+ Log.e("IminPrinter", "labelAddBitmap: "+e.getMessage());
1870
+ promise.resolve(false);
1871
+ }
1872
+
1873
+ }
1874
+ }
1875
+
1876
+ //打印标签样式的图片
1877
+ @ReactMethod
1878
+ public void printLabelBitmap(ReadableMap config, final Promise promise) {
1879
+ Log.e("IminPrinter", "printLabelBitmap: 打印标签样式的图片");
1880
+ if (iminPrintUtils == null) {
1881
+
1882
+ try {
1883
+ String bitmapUrl = config.getString("bitmapUrl");
1884
+ Integer width = (Integer) config.getInt("width");
1885
+ Integer height1 = (Integer) config.getInt("height");
1886
+
1887
+ Log.e("IminPrinter", "labelAddArea: 绘制特殊图形 " + ",width=" +
1888
+ width + " ,height1= " + height1 + " ,bitmapUrl= " + bitmapUrl );
1889
+
1890
+ Bitmap bitmap = null;
1891
+ if (bitmapUrl == null || bitmapUrl.isEmpty()) {
1892
+ promise.resolve(false);
1893
+ } else {
1894
+ bitmap = Glide.with(reactContext).asBitmap().load(bitmapUrl).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit(width, height1).get();
1895
+ }
1896
+
1897
+ PrinterHelper.getInstance().labelAddBitmap(bitmap, LabelBitmapStyle.getBitmapStyle()
1898
+ .setWidth(width)
1899
+ .setHeight(height1)
1900
+ );
1901
+
1902
+ promise.resolve(true);
1903
+
1904
+ } catch (Exception e) {
1905
+ Log.e("IminPrinter", "printLabelBitmap: 打印标签样式的图片 e " + e.getMessage());
1906
+ promise.resolve(false);
1907
+ }
1908
+
1909
+ }
1910
+ }
1911
+
1912
+ //绘制特殊图形
1913
+ @ReactMethod
1914
+ public void labelAddArea(ReadableMap config, final Promise promise) {
1915
+ if (iminPrintUtils == null) {
1916
+ Log.e("IminPrinter", "labelAddArea: 绘制特殊图形 " + config.getInt("style"));
1917
+ // 获取具体的参数
1918
+ if (config != null) {
1919
+ Integer style = config.getInt("style");
1920
+ Integer width = (Integer) config.getInt("width");
1921
+ Integer height1 = (Integer) config.getInt("height");
1922
+ Integer posX = (Integer) config.getInt("posX");
1923
+ Integer posY = (Integer) config.getInt("posY");
1924
+ Integer endX = (Integer) config.getInt("endX");
1925
+ Integer endY = (Integer) config.getInt("endY");
1926
+ Integer thick = (Integer) config.getInt("thick");
1927
+ Log.e("IminPrinter", "labelAddArea: 绘制特殊图形 " + style + ",width=" +
1928
+ width + " ,height1= " + height1 + " ,posX= " + posX + " ,posY= " + posY + " ,endX= " + endX + " ,endY= " + endY + " ,thick= " + thick);
1929
+ // 转换字符串到枚举
1930
+ Shape styleShape = Shape.values()[style];
1931
+ Log.e("IminPrinter", "labelAddArea: 绘制特殊图形 " + styleShape);
1932
+ PrinterHelper.getInstance().labelAddArea(LabelAreaStyle.getAreaStyle()
1933
+ .setStyle(styleShape)
1934
+ .setWidth(width)
1935
+ .setHeight(height1)
1936
+ .setPosX(posX)
1937
+ .setPosY(posY)
1938
+ .setEndX(endX)
1939
+ .setEndY(endY)
1940
+ .setThick(thick));
1941
+ promise.resolve(true);
1942
+ }
1943
+ }
1944
+ }
1945
+
1946
+ //打印绘制的内容
1947
+ @ReactMethod
1948
+ public void labelPrintCanvas(int printCount, final Promise promise) {
1949
+ if (iminPrintUtils == null) {
1950
+ Log.e("IminPrinter", "labelPrintCanvas: 打印绘制的内容 =>" + printCount);
1951
+ WritableMap payload = Arguments.createMap();
1952
+ PrinterHelper.getInstance().labelPrintCanvas(printCount, new ILabelPrintResult() {
1953
+ @Override
1954
+ public void onResult(int resultCode, String message) throws RemoteException {
1955
+ payload.putString("result", message);
1956
+ payload.putInt("resultCode",resultCode);
1957
+ promise.resolve(payload);
1958
+ }
1959
+
1960
+ @Override
1961
+ public IBinder asBinder() {
1962
+ return null;
1963
+ }
1964
+ });
1965
+ }
1966
+ }
1967
+
1968
+ //标签学习
1969
+ @ReactMethod
1970
+ public void labelLearning(final Promise promise) {
1971
+ if (iminPrintUtils == null) {
1972
+ WritableMap payload = Arguments.createMap();
1973
+ PrinterHelper.getInstance().labelPaperLearning(new INeoPrinterCallback() {
1974
+ @Override
1975
+ public void onRunResult(boolean isSuccess) throws RemoteException {
1976
+
1977
+ }
1978
+
1979
+ @Override
1980
+ public void onReturnString(String s) throws RemoteException {
1981
+ Log.e("IminPrinter", "labelLearning: 标签学习" + s);
1982
+ payload.putString("result", s);
1983
+ promise.resolve(payload);
1984
+ }
1985
+
1986
+ @Override
1987
+ public void onRaiseException(int code, String msg) throws RemoteException {
1988
+
1989
+ }
1990
+
1991
+ @Override
1992
+ public void onPrintResult(int code, String msg) throws RemoteException {
1993
+
1994
+ }
1995
+ });
1996
+ }
1997
+ }
1998
+
1999
+ //设置打印模式
2000
+ @ReactMethod
2001
+ public void setPrintModel(int printModel, final Promise promise) {
2002
+ if (iminPrintUtils == null) {
2003
+ Log.e("IminPrinter", "setPrintModel: 设置打印模式" + printModel);
2004
+ PrinterHelper.getInstance().setPrinterMode(printModel);
2005
+ promise.resolve(true);
2006
+ }
2007
+ }
2008
+
2009
+ private BroadcastReceiver createChargingStateBroadcastReceiver() {
2010
+ return new BroadcastReceiver() {
2011
+ @Override
2012
+ public void onReceive(Context context, Intent intent) {
2013
+ int status = intent.getIntExtra(ACTION_PRITER_STATUS, -1);
2014
+ Log.d(TAG, "打印机状态:" + intent.getAction());
2015
+ // 发送事件到React Native
2016
+ if (intent.getAction().equals(ACTION_PRITER_STATUS_CHANGE)) {
2017
+ WritableMap result = Arguments.createMap();
2018
+ WritableMap payload = Arguments.createMap();
2019
+ payload.putString("message", Utils.getInstance().getPrinterStatusText(status));
2020
+ payload.putString("code", String.format("%d", status));
2021
+ result.putMap("eventData", payload);
2022
+ result.putString("eventName", "printer_status");
2023
+ sendEvent(reactContext, "eventBroadcast", result);
2024
+ }
2025
+ }
2026
+ };
2027
+ }
2028
+
2029
+ private void initializeBroadcastReceiver() {
2030
+ IntentFilter intentFilter = new IntentFilter();
2031
+ mBroadcastReceiver = createChargingStateBroadcastReceiver();
2032
+ intentFilter.addAction(ACTION_PRITER_STATUS_CHANGE);
2033
+ intentFilter.addAction(ACTION_POGOPIN_STATUS_CHANGE);
2034
+ getReactApplicationContext().registerReceiver(mBroadcastReceiver, intentFilter);
2035
+ }
2036
+
2037
+ @ReactMethod
2038
+ public void addListener(String eventName) {
2039
+ }
2040
+
2041
+ @ReactMethod
2042
+ public void removeListeners(Integer count) {
2043
+ }
2044
+
2045
+ @Override
2046
+ public Map<String, Object> getConstants() {
2047
+ final Map<String, Object> constants = new HashMap<>();
2048
+ constants.put("SDK_VERSION_IMIN", sdkVersion);
2049
+ return constants;
2050
+ }
2051
+
2052
+ }