react-native-printer-imin 0.9.0 → 0.10.2

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,1664 +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 (Build.MODEL.contains("I23D") || Build.MODEL.contains("I23M") || Build.MODEL.contains("I24D") || Build.MODEL.contains("I24T") || Build.MODEL.contains("I24M")) {
74
- //初始化 2.0 SDK。
75
- PrinterHelper.getInstance().initPrinterService(reactContext);
76
- sdkVersion = "2.0.0";
77
- }
78
- // if (modelList.contains(Build.MODEL)) {
79
- // //初始化 2.0 的 SDK。
80
- // PrinterHelper.getInstance().initPrinterService(reactContext);
81
- // sdkVersion = "2.0.0";
82
- // }
83
- else {
84
- //初始化 1.0 SDK
85
-
86
- iminPrintUtils = IminPrintUtils.getInstance(reactContext);
87
- String deviceModel = Utils.getInstance().getModel();
88
- if (deviceModel.contains("M2-203") || deviceModel.contains("M2-202") || deviceModel.contains("M2-Pro")) {
89
- connectType = IminPrintUtils.PrintConnectType.SPI;
90
- } else {
91
- connectType = IminPrintUtils.PrintConnectType.USB;
92
- }
93
- iminPrintUtils.setIsOpenLog(IminPrintUtils.isOpenLog == 1 ? 0 : 1);
94
- iminPrintUtils.resetDevice();
95
- sdkVersion = "1.0.0";
96
- }
97
- initializeBroadcastReceiver();
98
- }
99
-
100
- @Override
101
- @NonNull
102
- public String getName() {
103
- return NAME;
104
- }
105
-
106
- private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
107
-
108
- try {
109
- reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
110
- } catch (RuntimeException e) {
111
- Log.e(TAG, "java.lang.RuntimeException: Trying to invoke JS before CatalystInstance has been set!", e);
112
- }
113
- }
114
-
115
- @ReactMethod
116
- public void initPrinter(final Promise promise) {
117
- try {
118
- if (iminPrintUtils != null) {
119
- iminPrintUtils.initPrinter(connectType);
120
- } else {
121
- PrinterHelper.getInstance().initPrinter(reactContext.getPackageName(), null);
122
- }
123
- promise.resolve(true);
124
- } catch (Exception e) {
125
- promise.reject("ININTPRINTER_FAILED", e.getMessage());
126
- }
127
- }
128
-
129
- @ReactMethod
130
- public void getPrinterStatus(final Promise promise) {
131
- try {
132
- WritableMap payload = Arguments.createMap();
133
- if (iminPrintUtils != null) {
134
- if (connectType.equals(IminPrintUtils.PrintConnectType.SPI)) {
135
- iminPrintUtils.getPrinterStatus(connectType, new Callback() {
136
- @Override
137
- public void callback(int status) {
138
- payload.putString("message", Utils.getInstance().getPrinterStatusText(status));
139
- payload.putString("code", String.format("%d", status));
140
- promise.resolve(payload);
141
- }
142
- });
143
- } else {
144
- int status = iminPrintUtils.getPrinterStatus(connectType);
145
- payload.putString("message", Utils.getInstance().getPrinterStatusText(status));
146
- payload.putString("code", String.format("%d", status));
147
- promise.resolve(payload);
148
- }
149
- } else {
150
- int status = PrinterHelper.getInstance().getPrinterStatus();
151
- payload.putString("message", Utils.getInstance().getPrinterStatusText(status));
152
- payload.putString("code", String.format("%d", status));
153
- promise.resolve(payload);
154
- }
155
- payload.putString("message", "sdsd");
156
- payload.putString("code", "1");
157
- promise.resolve(payload);
158
- } catch (Exception e) {
159
- promise.reject("getPrinterStatus_failed", e.getMessage());
160
- }
161
-
162
- }
163
-
164
- @ReactMethod
165
- public void setTextSize(int size, final Promise promise) {
166
- try {
167
- if (iminPrintUtils != null) {
168
- iminPrintUtils.setTextSize(size);
169
- }
170
- promise.resolve(null);
171
- } catch (Exception e) {
172
- promise.reject("setTextSize_failed", e.getMessage());
173
- }
174
- }
175
-
176
- @ReactMethod
177
- public void setTextTypeface(int font, final Promise promise) {
178
- try {
179
- if (iminPrintUtils != null) {
180
- switch (font) {
181
- case 1:
182
- iminPrintUtils.setTextTypeface(Typeface.MONOSPACE);
183
- break;
184
- case 2:
185
- iminPrintUtils.setTextTypeface(Typeface.DEFAULT_BOLD);
186
- break;
187
- case 3:
188
- iminPrintUtils.setTextTypeface(Typeface.SANS_SERIF);
189
- break;
190
- case 4:
191
- iminPrintUtils.setTextTypeface(Typeface.SERIF);
192
- break;
193
- default:
194
- iminPrintUtils.setTextTypeface(Typeface.DEFAULT);
195
- break;
196
- }
197
- }
198
- promise.resolve(null);
199
- } catch (Exception e) {
200
- promise.reject("setTextTypeface_failed", e.getMessage());
201
- }
202
- }
203
-
204
- @ReactMethod
205
- public void setTextStyle(int style, final Promise promise) {
206
- try {
207
- if (iminPrintUtils != null) {
208
- iminPrintUtils.setTextStyle(style);
209
- }
210
- promise.resolve(null);
211
- } catch (Exception e) {
212
- promise.reject("setTextStyle_failed", e.getMessage());
213
- }
214
- }
215
-
216
- @ReactMethod
217
- public void setAlignment(int alignment, final Promise promise) {
218
- try {
219
- if (iminPrintUtils != null) {
220
- iminPrintUtils.setAlignment(alignment);
221
- }
222
- promise.resolve(null);
223
- } catch (Exception e) {
224
- promise.reject("setAlignment_failed", e.getMessage());
225
- }
226
- }
227
-
228
- @ReactMethod
229
- public void setTextLineSpacing(float space, final Promise promise) {
230
- try {
231
- if (iminPrintUtils != null) {
232
- iminPrintUtils.setTextLineSpacing(space);
233
- }
234
- promise.resolve(null);
235
- } catch (Exception e) {
236
- promise.reject("setTextLineSpacing_failed", e.getMessage());
237
- }
238
- }
239
-
240
- @ReactMethod
241
- public void printText(String text, final Promise promise) {
242
- try {
243
- if (iminPrintUtils != null) {
244
- iminPrintUtils.printText(text + "\n");
245
- } else {
246
- PrinterHelper.getInstance().printText(text + "\n", null);
247
- }
248
- promise.resolve(null);
249
- } catch (Exception e) {
250
- promise.reject("printText_failed", e.getMessage());
251
- }
252
- }
253
-
254
- @ReactMethod
255
- public void printAntiWhiteText(String text, final Promise promise) {
256
- try {
257
- if (iminPrintUtils != null) {
258
- iminPrintUtils.printAntiWhiteText(text);
259
- }
260
- promise.resolve(null);
261
- } catch (Exception e) {
262
- promise.reject("printAntiWhiteText_failed", e.getMessage());
263
- }
264
- }
265
-
266
- @ReactMethod
267
- public void setTextWidth(int width, final Promise promise) {
268
- try {
269
- if (iminPrintUtils != null) {
270
- iminPrintUtils.setTextWidth(width);
271
- }
272
- promise.resolve(null);
273
- } catch (Exception e) {
274
- promise.reject("setTextWidth_failed", e.getMessage());
275
- }
276
- }
277
-
278
- @ReactMethod
279
- public void printAndLineFeed(final Promise promise) {
280
- try {
281
- if (iminPrintUtils != null) {
282
- iminPrintUtils.printAndLineFeed();
283
- } else {
284
- PrinterHelper.getInstance().printAndLineFeed();
285
- }
286
- promise.resolve(null);
287
- } catch (Exception e) {
288
- promise.reject("printAndLineFeed_failed", e.getMessage());
289
- }
290
- }
291
-
292
- @ReactMethod
293
- public void printAndFeedPaper(int height, final Promise promise) {
294
- try {
295
- if (iminPrintUtils != null) {
296
- iminPrintUtils.printAndFeedPaper(height);
297
- } else {
298
- PrinterHelper.getInstance().printAndFeedPaper(height);
299
- }
300
- promise.resolve(null);
301
- } catch (Exception e) {
302
- promise.reject("printAndLineFeed_failed", e.getMessage());
303
- }
304
- }
305
-
306
- @ReactMethod
307
- public void printColumnsText(ReadableArray cols, final Promise promise) {
308
- try {
309
- String[] colsText = new String[cols.size()];
310
- int[] colsWidth = new int[cols.size()];
311
- int[] colsAlign = new int[cols.size()];
312
- int[] colsFontSize = new int[cols.size()];
313
- for (int i = 0; i < cols.size(); i++) {
314
- ReadableMap col = cols.getMap(i);
315
- String textColumn = col.getString("text");
316
- int widthColumn = col.getInt("width");
317
- int alignColumn = col.getInt("align");
318
- Log.d(TAG, "printColumnsText: colsText" + textColumn);
319
- int fontSizeColumn = col.getInt("fontSize");
320
- colsText[i] = textColumn;
321
- colsWidth[i] = widthColumn;
322
- colsAlign[i] = alignColumn;
323
- colsFontSize[i] = fontSizeColumn;
324
- }
325
- Log.d(TAG, "printColumnsText: colsText" + colsText);
326
- Log.d(TAG, "printColumnsText: colsWidth" + colsWidth);
327
- Log.d(TAG, "printColumnsText: colsAlign" + colsAlign);
328
- Log.d(TAG, "printColumnsText: colsFontSize" + colsFontSize);
329
- if (iminPrintUtils != null) {
330
- iminPrintUtils.printColumnsText(colsText, colsWidth, colsAlign, colsFontSize);
331
- } else {
332
- PrinterHelper.getInstance().printColumnsText(colsText, colsWidth, colsAlign, colsFontSize, null);
333
- }
334
- promise.resolve(null);
335
- } catch (Exception e) {
336
- promise.reject("printColumnsText_failed", e.getMessage());
337
- }
338
- }
339
-
340
- @ReactMethod
341
- public void setPageFormat(int style, final Promise promise) {
342
- try {
343
- if (iminPrintUtils != null) {
344
- iminPrintUtils.setPageFormat(style);
345
- } else {
346
- PrinterHelper.getInstance().setPageFormat(style);
347
- }
348
- promise.resolve(null);
349
- } catch (Exception e) {
350
- promise.reject("setPageFormat_failed", e.getMessage());
351
- }
352
- }
353
-
354
- @ReactMethod
355
- public void partialCut(final Promise promise) {
356
- try {
357
- if (iminPrintUtils != null) {
358
- iminPrintUtils.partialCut();
359
- } else {
360
- PrinterHelper.getInstance().partialCut();
361
- }
362
- promise.resolve(null);
363
- } catch (Exception e) {
364
- promise.reject("partialCut_failed", e.getMessage());
365
- }
366
- }
367
-
368
- @ReactMethod
369
- public void fullCut(final Promise promise) {
370
- try {
371
- if (iminPrintUtils == null) {
372
- PrinterHelper.getInstance().fullCut();
373
- }
374
- promise.resolve(null);
375
- } catch (Exception e) {
376
- promise.reject("fullCut_failed", e.getMessage());
377
- }
378
- }
379
-
380
- @ReactMethod
381
- public void printSingleBitmap(ReadableMap config, final Promise promise) {
382
- try {
383
- String url = config.getString("url");
384
- if (config.hasKey("width") && config.hasKey("height")) {
385
- int width = config.getInt("width");
386
- int height = config.getInt("height");
387
- Bitmap image = Glide.with(reactContext).asBitmap().load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit(width, height).get();
388
- if (image.isRecycled()) {
389
- return;
390
- }
391
- if (iminPrintUtils != null) {
392
- if (config.hasKey("align")) {
393
- int align = config.getInt("align");
394
- iminPrintUtils.printSingleBitmap(image, align);
395
- } else {
396
- iminPrintUtils.printSingleBitmap(image);
397
- }
398
- } else {
399
- if (config.hasKey("align")) {
400
- int align = config.getInt("align");
401
- PrinterHelper.getInstance().printBitmapWithAlign(image, align, null);
402
- } else {
403
- PrinterHelper.getInstance().printBitmap(image, null);
404
- }
405
- }
406
- } else {
407
- Bitmap image = Glide.with(reactContext).asBitmap().load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit().get();
408
- if (image.isRecycled()) {
409
- return;
410
- }
411
- if (iminPrintUtils != null) {
412
- if (config.hasKey("align")) {
413
- int align = config.getInt("align");
414
- iminPrintUtils.printSingleBitmap(image, align);
415
- } else {
416
- iminPrintUtils.printSingleBitmap(image);
417
- }
418
- } else {
419
- if (config.hasKey("align")) {
420
- int align = config.getInt("align");
421
- PrinterHelper.getInstance().printBitmapWithAlign(image, align, null);
422
- } else {
423
- PrinterHelper.getInstance().printBitmap(image, null);
424
- }
425
- }
426
- }
427
- promise.resolve(null);
428
- } catch (Exception e) {
429
- promise.reject("printSingleBitmap_failed", e.getMessage());
430
- }
431
- }
432
-
433
- @ReactMethod
434
- public void printMultiBitmap(ReadableMap config, final Promise promise) {
435
- try {
436
- ReadableArray urls = config.getArray("urls");
437
- ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
438
- if (config.hasKey("width") && config.hasKey("height")) {
439
- int width = config.getInt("width");
440
- int height = config.getInt("height");
441
- for (int i = 0; i < urls.size(); i++) {
442
- Bitmap image = Glide.with(reactContext).asBitmap().load(urls.getString(i)).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit(width, height).get();
443
- if (!image.isRecycled()) {
444
- bitmaps.add(image);
445
- }
446
- }
447
- if (iminPrintUtils != null) {
448
- if (config.hasKey("align")) {
449
- int align = config.getInt("align");
450
- iminPrintUtils.printMultiBitmap(bitmaps, align);
451
- } else {
452
- iminPrintUtils.printMultiBitmap(bitmaps, 0);
453
- }
454
- } else {
455
- if (config.hasKey("align")) {
456
- int align = config.getInt("align");
457
- PrinterHelper.getInstance().printMultiBitmapWithAlign(bitmaps, align, null);
458
- } else {
459
- PrinterHelper.getInstance().printMultiBitmap(bitmaps, null);
460
- }
461
- }
462
- } else {
463
- for (int i = 0; i < urls.size(); i++) {
464
- Bitmap image = Glide.with(reactContext).asBitmap().load(urls.getString(i)).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit().get();
465
- if (!image.isRecycled()) {
466
- bitmaps.add(image);
467
- }
468
- }
469
- if (iminPrintUtils != null) {
470
- if (config.hasKey("align")) {
471
- int align = config.getInt("align");
472
- iminPrintUtils.printMultiBitmap(bitmaps, align);
473
- } else {
474
- iminPrintUtils.printMultiBitmap(bitmaps, 0);
475
- }
476
- } else {
477
- if (config.hasKey("align")) {
478
- int align = config.getInt("align");
479
- PrinterHelper.getInstance().printMultiBitmapWithAlign(bitmaps, align, null);
480
- } else {
481
- PrinterHelper.getInstance().printMultiBitmap(bitmaps, null);
482
- }
483
- }
484
- }
485
- promise.resolve(null);
486
- } catch (Exception e) {
487
- promise.reject("printMultiBitmap_failed", e.getMessage());
488
- }
489
- }
490
-
491
- @ReactMethod
492
- public void printSingleBitmapBlackWhite(ReadableMap config, final Promise promise) {
493
- try {
494
- String url = config.getString("url");
495
- if (config.hasKey("width") && config.hasKey("height")) {
496
- int width = config.getInt("width");
497
- int height = config.getInt("height");
498
- Bitmap image = Glide.with(reactContext).asBitmap().load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit(width, height).get();
499
- if (image.isRecycled()) {
500
- return;
501
- }
502
- if (iminPrintUtils != null) {
503
- iminPrintUtils.printSingleBitmapBlackWhite(image);
504
- }
505
- } else {
506
- Bitmap image = Glide.with(reactContext).asBitmap().load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit().get();
507
- if (image.isRecycled()) {
508
- return;
509
- }
510
- if (iminPrintUtils != null) {
511
- iminPrintUtils.printSingleBitmapBlackWhite(image);
512
- }
513
- }
514
- promise.resolve(null);
515
- } catch (Exception e) {
516
- promise.reject("printSingleBitmapBlackWhite_failed", e.getMessage());
517
- }
518
- }
519
-
520
- @ReactMethod
521
- public void setQrCodeSize(int qrSize, final Promise promise) {
522
- try {
523
- if (iminPrintUtils != null) {
524
- iminPrintUtils.setQrCodeSize(qrSize);
525
- } else {
526
- PrinterHelper.getInstance().setQrCodeSize(qrSize);
527
- }
528
- promise.resolve(null);
529
- } catch (Exception e) {
530
- promise.reject("setQrCodeSize_failed", e.getMessage());
531
- }
532
- }
533
-
534
- @ReactMethod
535
- public void setLeftMargin(int margin, final Promise promise) {
536
- try {
537
- if (iminPrintUtils != null) {
538
- iminPrintUtils.setLeftMargin(margin);
539
- } else {
540
- PrinterHelper.getInstance().setLeftMargin(margin);
541
- }
542
- promise.resolve(null);
543
- } catch (Exception e) {
544
- promise.reject("setLeftMargin_failed", e.getMessage());
545
- }
546
- }
547
-
548
- @ReactMethod
549
- public void setBarCodeWidth(int width, final Promise promise) {
550
- try {
551
- if (iminPrintUtils != null) {
552
- iminPrintUtils.setBarCodeWidth(width);
553
- } else {
554
- PrinterHelper.getInstance().setBarCodeWidth(width);
555
- }
556
- promise.resolve(null);
557
- } catch (Exception e) {
558
- promise.reject("setBarCodeWidth_failed", e.getMessage());
559
- }
560
- }
561
-
562
- @ReactMethod
563
- public void setBarCodeHeight(int height, final Promise promise) {
564
- try {
565
- if (iminPrintUtils != null) {
566
- iminPrintUtils.setBarCodeHeight(height);
567
- } else {
568
- PrinterHelper.getInstance().setBarCodeHeight(height);
569
- }
570
- promise.resolve(null);
571
- } catch (Exception e) {
572
- promise.reject("setBarCodeHeight_failed", e.getMessage());
573
- }
574
- }
575
-
576
- @ReactMethod
577
- public void setBarCodeContentPrintPos(int position, final Promise promise) {
578
- try {
579
- if (iminPrintUtils != null) {
580
- iminPrintUtils.setBarCodeContentPrintPos(position);
581
- } else {
582
- PrinterHelper.getInstance().setBarCodeContentPrintPos(position);
583
- }
584
- promise.resolve(null);
585
- } catch (Exception e) {
586
- promise.reject("setBarCodeContentPrintPos_failed", e.getMessage());
587
- }
588
- }
589
-
590
- @ReactMethod
591
- public void printBarCode(ReadableMap config, final Promise promise) {
592
- try {
593
- String barCodeContent = config.getString("data");
594
- int barCodeType = config.getInt("type");
595
- if (iminPrintUtils != null) {
596
- if (config.hasKey("align")) {
597
- int barCodeAlign = config.getInt("align");
598
- iminPrintUtils.printBarCode(barCodeType, barCodeContent, barCodeAlign);
599
- } else {
600
- iminPrintUtils.printBarCode(barCodeType, barCodeContent);
601
- }
602
- } else {
603
- if (config.hasKey("align")) {
604
- int barCodeAlign = config.getInt("align");
605
- if (config.hasKey("position") && config.hasKey("height") && config.hasKey("width")) {
606
- int position = config.getInt("position");
607
- int height = config.getInt("height");
608
- int width = config.getInt("width");
609
- PrinterHelper.getInstance().printBarCodeWithFull(barCodeContent, barCodeType, width, height, barCodeAlign, position, null);
610
- } else {
611
- PrinterHelper.getInstance().printBarCodeWithAlign(barCodeContent, barCodeType, barCodeAlign, null);
612
- }
613
- } else {
614
- PrinterHelper.getInstance().printBarCode(barCodeContent, barCodeType, null);
615
- }
616
- }
617
- promise.resolve(null);
618
- } catch (Exception e) {
619
- promise.reject("printBarCode_failed", e.getMessage());
620
- }
621
-
622
- }
623
-
624
- @ReactMethod
625
- public void setDoubleQRSize(int size, final Promise promise) {
626
- try {
627
- if (iminPrintUtils != null) {
628
- iminPrintUtils.setDoubleQRSize(size);
629
- } else {
630
- PrinterHelper.getInstance().setDoubleQRSize(size);
631
- }
632
- promise.resolve(null);
633
- } catch (Exception e) {
634
- promise.reject("setDoubleQRSize_failed", e.getMessage());
635
- }
636
- }
637
-
638
- @ReactMethod
639
- public void setDoubleQR1Level(int level, final Promise promise) {
640
- try {
641
- if (iminPrintUtils != null) {
642
- iminPrintUtils.setDoubleQR1Level(level);
643
- } else {
644
- PrinterHelper.getInstance().setDoubleQR1Level(level);
645
- }
646
- promise.resolve(null);
647
- } catch (Exception e) {
648
- promise.reject("setDoubleQR1Level_failed", e.getMessage());
649
- }
650
- }
651
-
652
- @ReactMethod
653
- public void setDoubleQR2Level(int level, final Promise promise) {
654
- try {
655
- if (iminPrintUtils != null) {
656
- iminPrintUtils.setDoubleQR2Level(level);
657
- } else {
658
- PrinterHelper.getInstance().setDoubleQR2Level(level);
659
- }
660
- promise.resolve(null);
661
- } catch (Exception e) {
662
- promise.reject("setDoubleQR2Level_failed", e.getMessage());
663
- }
664
- }
665
-
666
- @ReactMethod
667
- public void setDoubleQR1MarginLeft(int leftMargin, final Promise promise) {
668
- try {
669
- if (iminPrintUtils != null) {
670
- iminPrintUtils.setDoubleQR1MarginLeft(leftMargin);
671
- } else {
672
- PrinterHelper.getInstance().setDoubleQR1MarginLeft(leftMargin);
673
- }
674
- promise.resolve(null);
675
- } catch (Exception e) {
676
- promise.reject("setDoubleQR1MarginLeft_failed", e.getMessage());
677
- }
678
- }
679
-
680
- @ReactMethod
681
- public void setDoubleQR2MarginLeft(int leftMargin, final Promise promise) {
682
- try {
683
- if (iminPrintUtils != null) {
684
- iminPrintUtils.setDoubleQR2MarginLeft(leftMargin);
685
- } else {
686
- PrinterHelper.getInstance().setDoubleQR2MarginLeft(leftMargin);
687
- }
688
- promise.resolve(null);
689
- } catch (Exception e) {
690
- promise.reject("setDoubleQR2MarginLeft_failed", e.getMessage());
691
- }
692
- }
693
-
694
- @ReactMethod
695
- public void setQrCodeErrorCorrectionLev(int level, final Promise promise) {
696
- try {
697
- if (iminPrintUtils != null) {
698
- iminPrintUtils.setQrCodeErrorCorrectionLev(level);
699
- } else {
700
- PrinterHelper.getInstance().setQrCodeErrorCorrectionLev(level);
701
- }
702
- promise.resolve(null);
703
- } catch (Exception e) {
704
- promise.reject("setQrCodeErrorCorrectionLev_failed", e.getMessage());
705
- }
706
- }
707
-
708
- @ReactMethod
709
- public void setDoubleQR1Version(int version, final Promise promise) {
710
- try {
711
- if (iminPrintUtils != null) {
712
- iminPrintUtils.setDoubleQR1Version(version);
713
- } else {
714
- PrinterHelper.getInstance().setDoubleQR1Version(version);
715
- }
716
- promise.resolve(null);
717
- } catch (Exception e) {
718
- promise.reject("setDoubleQR1Version_failed", e.getMessage());
719
- }
720
- }
721
-
722
- @ReactMethod
723
- public void setDoubleQR2Version(int version, final Promise promise) {
724
- try {
725
- if (iminPrintUtils != null) {
726
- iminPrintUtils.setDoubleQR2Version(version);
727
- } else {
728
- PrinterHelper.getInstance().setDoubleQR2Version(version);
729
- }
730
- promise.resolve(null);
731
- } catch (Exception e) {
732
- promise.reject("setDoubleQR2Version_failed", e.getMessage());
733
- }
734
- }
735
-
736
- @ReactMethod
737
- public void printQrCode(ReadableMap config, final Promise promise) {
738
- try {
739
- String qrStr = config.getString("data");
740
- if (config.hasKey("align")) {
741
- int align = config.getInt("align");
742
- if (iminPrintUtils != null) {
743
- iminPrintUtils.printQrCode(qrStr, align);
744
- } else {
745
- if (config.hasKey("qrSize") && config.hasKey("level")) {
746
- int qrSize = config.getInt("qrSize");
747
- int qrLevel = config.getInt("level");
748
- PrinterHelper.getInstance().printQRCodeWithFull(qrStr, qrSize, qrLevel, align, null);
749
- } else {
750
- PrinterHelper.getInstance().printQrCodeWithAlign(qrStr, align, null);
751
- }
752
- }
753
- } else {
754
- if (iminPrintUtils != null) {
755
- iminPrintUtils.printQrCode(qrStr);
756
- } else {
757
- PrinterHelper.getInstance().printQrCode(qrStr, null);
758
- }
759
- }
760
- promise.resolve(null);
761
- } catch (Exception e) {
762
- promise.reject("printQrCode_failed", e.getMessage());
763
- }
764
- }
765
-
766
- @ReactMethod
767
- public void printDoubleQR(ReadableMap config, final Promise promise) {
768
- try {
769
- String qrCode1Text = config.getString("qrCode1Text");
770
- String qrCode2Text = config.getString("qrCode2Text");
771
- if (iminPrintUtils != null) {
772
- iminPrintUtils.printDoubleQR(qrCode1Text, qrCode2Text);
773
- } else {
774
- PrinterHelper.getInstance().printDoubleQR(qrCode1Text, qrCode2Text, null);
775
- }
776
- promise.resolve(null);
777
- } catch (Exception e) {
778
- promise.reject("printDoubleQR_failed", e.getMessage());
779
- }
780
- }
781
-
782
- @ReactMethod
783
- public void openCashBox(final Promise promise) {
784
- try {
785
- if (iminPrintUtils != null) {
786
- Utils.getInstance().opencashBox();
787
- } else {
788
- PrinterHelper.getInstance().openDrawer();
789
- }
790
- promise.resolve(null);
791
- } catch (Exception e) {
792
- promise.reject("openCashBox_failed", e.getMessage());
793
- }
794
-
795
- }
796
-
797
-
798
- @ReactMethod
799
- public void setInitIminPrinter(boolean isDefault, final Promise promise) {
800
- try {
801
- if (iminPrintUtils != null) {
802
- iminPrintUtils.setInitIminPrinter(isDefault);
803
- }
804
- promise.resolve(null);
805
- } catch (Exception e) {
806
- promise.reject("setInitIminPrinter_failed", e.getMessage());
807
- }
808
- }
809
-
810
- @ReactMethod
811
- public void resetDevice(final Promise promise) {
812
- try {
813
- if (iminPrintUtils != null) {
814
- iminPrintUtils.resetDevice();
815
- }
816
- promise.resolve(null);
817
- } catch (Exception e) {
818
- promise.reject("resetDevice_failed", e.getMessage());
819
- }
820
- }
821
-
822
- @ReactMethod
823
- public void printerSelfChecking(final Promise promise) {
824
- try {
825
- if (iminPrintUtils == null) {
826
- PrinterHelper.getInstance().printerSelfChecking(null);
827
- }
828
- promise.resolve(null);
829
- } catch (Exception e) {
830
- promise.reject("printerSelfChecking_failed", e.getMessage());
831
- }
832
- }
833
-
834
- @ReactMethod
835
- public void openLogs(int open, final Promise promise) {
836
- try {
837
- if (iminPrintUtils != null) {
838
- iminPrintUtils.setIsOpenLog(open);
839
- }
840
- promise.resolve(null);
841
- } catch (Exception e) {
842
- promise.reject("setIsOpenLog_failed", e.getMessage());
843
- }
844
- }
845
-
846
- @ReactMethod
847
- public void sendRAWDataHexStr(String byteStr, final Promise promise) {
848
- try {
849
- if (iminPrintUtils != null) {
850
- iminPrintUtils.sendRAWData(byteStr);
851
- }
852
- promise.resolve(null);
853
- } catch (Exception e) {
854
- promise.reject("sendRAWDataHexStr_failed", e.getMessage());
855
- }
856
- }
857
-
858
- @ReactMethod
859
- public void printSingleBitmapColorChart(ReadableMap config, final Promise promise) {
860
- try {
861
- String url = config.getString("url");
862
- if (config.hasKey("width") && config.hasKey("height")) {
863
- int width = config.getInt("width");
864
- int height = config.getInt("height");
865
- Bitmap image = Glide.with(reactContext).asBitmap().load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit(width, height).get();
866
- if (image.isRecycled()) {
867
- return;
868
- }
869
- if (iminPrintUtils == null) {
870
- if (config.hasKey("align")) {
871
- int align = config.getInt("align");
872
- PrinterHelper.getInstance().printBitmapColorChartWithAlign(image, align, null);
873
- } else {
874
- PrinterHelper.getInstance().printBitmapColorChart(image, null);
875
- }
876
- }
877
- } else {
878
- Bitmap image = Glide.with(reactContext).asBitmap().load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).submit().get();
879
- if (image.isRecycled()) {
880
- return;
881
- }
882
- if (iminPrintUtils == null) {
883
- if (config.hasKey("align")) {
884
- int align = config.getInt("align");
885
- PrinterHelper.getInstance().printBitmapColorChartWithAlign(image, align, null);
886
- } else {
887
- PrinterHelper.getInstance().printBitmapColorChart(image, null);
888
- }
889
- }
890
- }
891
- promise.resolve(null);
892
- } catch (Exception e) {
893
- promise.reject("printSingleBitmap_failed", e.getMessage());
894
- }
895
- }
896
-
897
-
898
- @ReactMethod
899
- public void sendRAWData(ReadableArray bytes, final Promise promise) {
900
- try {
901
- // 处理接收到的 Uint8Array 数据
902
- byte[] rawData = new byte[bytes.size()];
903
- for (int i = 0; i < bytes.size(); i++) {
904
- rawData[i] = (byte) bytes.getInt(i);
905
- }
906
- if (iminPrintUtils == null) {
907
- PrinterHelper.getInstance().sendRAWData(rawData, null);
908
- }
909
- promise.resolve(null);
910
- } catch (Exception e) {
911
- promise.reject("sendRAWData_failed", e.getMessage());
912
- }
913
- }
914
-
915
- @ReactMethod
916
- public void printColumnsString(ReadableArray cols, final Promise promise) {
917
- try {
918
- String[] colsText = new String[cols.size()];
919
- int[] colsWidth = new int[cols.size()];
920
- int[] colsAlign = new int[cols.size()];
921
- int[] colsFontSize = new int[cols.size()];
922
- for (int i = 0; i < cols.size(); i++) {
923
- ReadableMap col = cols.getMap(i);
924
- String textColumn = col.getString("text");
925
- int widthColumn = col.getInt("width");
926
- int alignColumn = col.getInt("align");
927
- int fontSizeColumn = col.getInt("fontSize");
928
- colsText[i] = textColumn;
929
- colsWidth[i] = widthColumn;
930
- colsAlign[i] = alignColumn;
931
- colsFontSize[i] = fontSizeColumn;
932
- }
933
- if (iminPrintUtils == null) {
934
- PrinterHelper.getInstance().printColumnsString(colsText, colsWidth, colsAlign, colsFontSize, null);
935
- }
936
- promise.resolve(null);
937
- } catch (Exception e) {
938
- promise.reject("printColumnsString_failed", e.getMessage());
939
- }
940
- }
941
-
942
- @ReactMethod
943
- public void unBindService(final Promise promise) {
944
- try {
945
- if (iminPrintUtils == null) {
946
- PrinterHelper.getInstance().deInitPrinterService(reactContext);
947
- }
948
- } catch (Exception e) {
949
- promise.reject("unBindService_failed", e.getMessage());
950
- }
951
- }
952
-
953
- @ReactMethod
954
- public void initPrinterParams(final Promise promise) {
955
- try {
956
- if (iminPrintUtils == null) {
957
- PrinterHelper.getInstance().initPrinterParams();
958
- }
959
- promise.resolve(null);
960
- } catch (Exception e) {
961
- promise.reject("initPrinterParams_failed", e.getMessage());
962
- }
963
- }
964
-
965
- @ReactMethod
966
- public void getFontCodepage(final Promise promise) {
967
- try {
968
- if (iminPrintUtils == null) {
969
- List<String> fontCodepage = PrinterHelper.getInstance().getFontCodepage();
970
- promise.resolve(fontCodepage);
971
- } else {
972
- promise.resolve(null);
973
- }
974
- } catch (Exception e) {
975
- promise.reject("getFontCodepage_failed", e.getMessage());
976
- }
977
- }
978
-
979
- @ReactMethod
980
- public void setFontCodepage(int codepage, final Promise promise) {
981
- try {
982
- if (iminPrintUtils == null) {
983
- PrinterHelper.getInstance().setFontCodepage(codepage);
984
- }
985
- promise.resolve(null);
986
- } catch (Exception e) {
987
- promise.reject("setFontCodepage_failed", e.getMessage());
988
- }
989
- }
990
-
991
- @ReactMethod
992
- public void getCurCodepage(final Promise promise) {
993
- try {
994
- if (iminPrintUtils == null) {
995
- String curCodepage = PrinterHelper.getInstance().getCurCodepage();
996
- promise.resolve(curCodepage);
997
- } else {
998
- promise.resolve(null);
999
- }
1000
- } catch (Exception e) {
1001
- promise.reject("getCurCodepage_failed", e.getMessage());
1002
- }
1003
- }
1004
-
1005
- @ReactMethod
1006
- public void getEncodeList(final Promise promise) {
1007
- try {
1008
- if (iminPrintUtils == null) {
1009
- List<String> encodeList = PrinterHelper.getInstance().getEncodeList();
1010
- promise.resolve(encodeList);
1011
- } else {
1012
- promise.resolve(null);
1013
- }
1014
- } catch (Exception e) {
1015
- promise.reject("getEncodeList_failed", e.getMessage());
1016
- }
1017
- }
1018
-
1019
- @ReactMethod
1020
- public void setPrinterEncode(int encode, final Promise promise) {
1021
- try {
1022
- if (iminPrintUtils == null) {
1023
- PrinterHelper.getInstance().setPrinterEncode(encode);
1024
- }
1025
- promise.resolve(null);
1026
- } catch (Exception e) {
1027
- promise.reject("setPrinterEncode_failed", e.getMessage());
1028
- }
1029
- }
1030
-
1031
- @ReactMethod
1032
- public void getCurEncode(final Promise promise) {
1033
- try {
1034
- if (iminPrintUtils == null) {
1035
- String curEncode = PrinterHelper.getInstance().getCurEncode();
1036
- promise.resolve(curEncode);
1037
- } else {
1038
- promise.resolve(null);
1039
- }
1040
- } catch (Exception e) {
1041
- promise.reject("getCurEncode_failed", e.getMessage());
1042
- }
1043
- }
1044
-
1045
- @ReactMethod
1046
- public void getPrinterDensityList(final Promise promise) {
1047
- try {
1048
- if (iminPrintUtils == null) {
1049
- List<String> printerDensityList = PrinterHelper.getInstance().getPrinterDensityList();
1050
- promise.resolve(printerDensityList);
1051
- } else {
1052
- promise.resolve(null);
1053
- }
1054
- } catch (Exception e) {
1055
- promise.reject("getPrinterDensityList_failed", e.getMessage());
1056
- }
1057
- }
1058
-
1059
- @ReactMethod
1060
- public void setPrinterDensity(int density, final Promise promise) {
1061
- try {
1062
- if (iminPrintUtils == null) {
1063
- PrinterHelper.getInstance().setPrinterDensity(density);
1064
- }
1065
- promise.resolve(null);
1066
- } catch (Exception e) {
1067
- promise.reject("setPrinterDensity_failed", e.getMessage());
1068
- }
1069
- }
1070
-
1071
- @ReactMethod
1072
- public void getPrinterDensity(final Promise promise) {
1073
- try {
1074
- if (iminPrintUtils == null) {
1075
- promise.resolve(PrinterHelper.getInstance().getPrinterDensity());
1076
- } else {
1077
- promise.resolve(null);
1078
- }
1079
-
1080
- } catch (Exception e) {
1081
- promise.reject("getPrinterDensity_failed", e.getMessage());
1082
- }
1083
- }
1084
-
1085
- @ReactMethod
1086
- public void getPrinterSpeedList(final Promise promise) {
1087
- try {
1088
- if (iminPrintUtils == null) {
1089
- List<String> printerSpeedList = PrinterHelper.getInstance().getPrinterSpeedList();
1090
- promise.resolve(printerSpeedList);
1091
- } else {
1092
- promise.resolve(null);
1093
- }
1094
-
1095
- } catch (Exception e) {
1096
- promise.reject("getPrinterSpeedList_failed", e.getMessage());
1097
- }
1098
- }
1099
-
1100
- @ReactMethod
1101
- public void setPrinterSpeed(int speed, final Promise promise) {
1102
- try {
1103
- if (iminPrintUtils == null) {
1104
- PrinterHelper.getInstance().setPrinterSpeed(speed);
1105
- }
1106
- promise.resolve(null);
1107
- } catch (Exception e) {
1108
- promise.reject("setPrinterSpeed_failed", e.getMessage());
1109
- }
1110
- }
1111
-
1112
- @ReactMethod
1113
- public void getPrinterSpeed(final Promise promise) {
1114
- try {
1115
- if (iminPrintUtils == null) {
1116
- int printerSpeed = PrinterHelper.getInstance().getPrinterSpeed();
1117
- promise.resolve(printerSpeed);
1118
- } else {
1119
- promise.resolve(null);
1120
- }
1121
- } catch (Exception e) {
1122
- promise.reject("getPrinterSpeed_failed", e.getMessage());
1123
- }
1124
- }
1125
-
1126
- @ReactMethod
1127
- public void getPrinterPaperTypeList(final Promise promise) {
1128
- try {
1129
- if (iminPrintUtils == null) {
1130
- List<String> printerPaperTypeList = PrinterHelper.getInstance().getPrinterPaperTypeList();
1131
- promise.resolve(printerPaperTypeList);
1132
- } else {
1133
- promise.resolve(null);
1134
- }
1135
- } catch (Exception e) {
1136
- promise.reject("getPrinterPaperTypeList_failed", e.getMessage());
1137
- }
1138
- }
1139
-
1140
- @ReactMethod
1141
- public void getPrinterPaperType(final Promise promise) {
1142
- try {
1143
- if (iminPrintUtils == null) {
1144
- promise.resolve(PrinterHelper.getInstance().getPrinterPaperType());
1145
- } else {
1146
- promise.resolve(null);
1147
- }
1148
- } catch (Exception e) {
1149
- promise.reject("getPrinterPaperType_failed", e.getMessage());
1150
- }
1151
- }
1152
-
1153
- @ReactMethod
1154
- public void getPrinterSerialNumber(final Promise promise) {
1155
- try {
1156
- if (iminPrintUtils == null) {
1157
- PrinterHelper.getInstance().getPrinterSerialNumber(new INeoPrinterCallback() {
1158
- @Override
1159
- public void onRunResult(boolean isSuccess) throws RemoteException {
1160
- }
1161
-
1162
- @Override
1163
- public void onReturnString(String s) throws RemoteException {
1164
- promise.resolve(s);
1165
- }
1166
-
1167
- @Override
1168
- public void onRaiseException(int code, String msg) throws RemoteException {
1169
- }
1170
-
1171
- @Override
1172
- public void onPrintResult(int code, String msg) throws RemoteException {
1173
- }
1174
- });
1175
- } else {
1176
- promise.resolve(null);
1177
- }
1178
- } catch (Exception e) {
1179
- promise.reject("getPrinterSerialNumber_failed", e.getMessage());
1180
- }
1181
- }
1182
-
1183
- @ReactMethod
1184
- public void getPrinterModelName(final Promise promise) {
1185
- try {
1186
- if (iminPrintUtils == null) {
1187
- PrinterHelper.getInstance().getPrinterModelName(new INeoPrinterCallback() {
1188
- @Override
1189
- public void onRunResult(boolean isSuccess) throws RemoteException {
1190
- }
1191
-
1192
- @Override
1193
- public void onReturnString(String s) throws RemoteException {
1194
- promise.resolve(s);
1195
- }
1196
-
1197
- @Override
1198
- public void onRaiseException(int code, String msg) throws RemoteException {
1199
- }
1200
-
1201
- @Override
1202
- public void onPrintResult(int code, String msg) throws RemoteException {
1203
- }
1204
- });
1205
- } else {
1206
- promise.resolve(null);
1207
- }
1208
- } catch (Exception e) {
1209
- promise.reject("getPrinterModelName_failed", e.getMessage());
1210
- }
1211
- }
1212
-
1213
- @ReactMethod
1214
- public void getPrinterThermalHead(final Promise promise) {
1215
- try {
1216
- if (iminPrintUtils == null) {
1217
- PrinterHelper.getInstance().getPrinterThermalHead(new INeoPrinterCallback() {
1218
- @Override
1219
- public void onRunResult(boolean isSuccess) throws RemoteException {
1220
- }
1221
-
1222
- @Override
1223
- public void onReturnString(String s) throws RemoteException {
1224
- promise.resolve(s);
1225
- }
1226
-
1227
- @Override
1228
- public void onRaiseException(int code, String msg) throws RemoteException {
1229
- }
1230
-
1231
- @Override
1232
- public void onPrintResult(int code, String msg) throws RemoteException {
1233
- }
1234
- });
1235
- } else {
1236
- promise.resolve(null);
1237
- }
1238
- } catch (Exception e) {
1239
- promise.reject("getPrinterThermalHead_failed", e.getMessage());
1240
- }
1241
- }
1242
-
1243
- @ReactMethod
1244
- public void getPrinterFirmwareVersion(final Promise promise) {
1245
- try {
1246
- if (iminPrintUtils == null) {
1247
- PrinterHelper.getInstance().getPrinterFirmwareVersion(new INeoPrinterCallback() {
1248
- @Override
1249
- public void onRunResult(boolean isSuccess) throws RemoteException {
1250
- }
1251
-
1252
- @Override
1253
- public void onReturnString(String s) throws RemoteException {
1254
- promise.resolve(s);
1255
- }
1256
-
1257
- @Override
1258
- public void onRaiseException(int code, String msg) throws RemoteException {
1259
- }
1260
-
1261
- @Override
1262
- public void onPrintResult(int code, String msg) throws RemoteException {
1263
- }
1264
- });
1265
- } else {
1266
- promise.resolve(null);
1267
- }
1268
- } catch (Exception e) {
1269
- promise.reject("getPrinterFirmwareVersion_failed", e.getMessage());
1270
- }
1271
- }
1272
-
1273
- @ReactMethod
1274
- public void getPrinterHardwareVersion(final Promise promise) {
1275
- try {
1276
- if (iminPrintUtils == null) {
1277
- PrinterHelper.getInstance().getPrinterHardwareVersion(new INeoPrinterCallback() {
1278
- @Override
1279
- public void onRunResult(boolean isSuccess) throws RemoteException {
1280
- }
1281
-
1282
- @Override
1283
- public void onReturnString(String s) throws RemoteException {
1284
- promise.resolve(s);
1285
- }
1286
-
1287
- @Override
1288
- public void onRaiseException(int code, String msg) throws RemoteException {
1289
- }
1290
-
1291
- @Override
1292
- public void onPrintResult(int code, String msg) throws RemoteException {
1293
- }
1294
- });
1295
- } else {
1296
- promise.resolve(null);
1297
- }
1298
- } catch (Exception e) {
1299
- promise.reject("getPrinterHardwareVersion_failed", e.getMessage());
1300
- }
1301
- }
1302
-
1303
- @ReactMethod
1304
- public void getPrinterPaperDistance(final Promise promise) {
1305
- try {
1306
- if (iminPrintUtils == null) {
1307
- PrinterHelper.getInstance().getPrinterPaperDistance(new INeoPrinterCallback() {
1308
- @Override
1309
- public void onRunResult(boolean isSuccess) throws RemoteException {
1310
- }
1311
-
1312
- @Override
1313
- public void onReturnString(String s) throws RemoteException {
1314
- promise.resolve(s);
1315
- }
1316
-
1317
- @Override
1318
- public void onRaiseException(int code, String msg) throws RemoteException {
1319
- }
1320
-
1321
- @Override
1322
- public void onPrintResult(int code, String msg) throws RemoteException {
1323
- }
1324
- });
1325
- } else {
1326
- promise.resolve(null);
1327
- }
1328
- } catch (Exception e) {
1329
- promise.reject("getPrinterPaperDistance_failed", e.getMessage());
1330
- }
1331
- }
1332
-
1333
- @ReactMethod
1334
- public void getPrinterCutTimes(final Promise promise) {
1335
- try {
1336
- if (iminPrintUtils == null) {
1337
- PrinterHelper.getInstance().getPrinterCutTimes(new INeoPrinterCallback() {
1338
- @Override
1339
- public void onRunResult(boolean isSuccess) throws RemoteException {
1340
- }
1341
-
1342
- @Override
1343
- public void onReturnString(String s) throws RemoteException {
1344
- promise.resolve(s);
1345
- }
1346
-
1347
- @Override
1348
- public void onRaiseException(int code, String msg) throws RemoteException {
1349
- }
1350
-
1351
- @Override
1352
- public void onPrintResult(int code, String msg) throws RemoteException {
1353
- }
1354
- });
1355
- } else {
1356
- promise.resolve(null);
1357
- }
1358
- } catch (Exception e) {
1359
- promise.reject("getPrinterCutTimes_failed", e.getMessage());
1360
- }
1361
- }
1362
-
1363
-
1364
- @ReactMethod
1365
- public void getPrinterMode(final Promise promise) {
1366
- try {
1367
- if (iminPrintUtils == null) {
1368
- promise.resolve(PrinterHelper.getInstance().getPrinterMode());
1369
- } else {
1370
- promise.resolve(null);
1371
- }
1372
- } catch (Exception e) {
1373
- promise.reject("getPrinterMode_failed", e.getMessage());
1374
- }
1375
- }
1376
-
1377
- @ReactMethod
1378
- public void getDrawerStatus(final Promise promise) {
1379
- try {
1380
- if (iminPrintUtils == null) {
1381
- promise.resolve(PrinterHelper.getInstance().getDrawerStatus());
1382
- } else {
1383
- promise.resolve(null);
1384
- }
1385
- } catch (Exception e) {
1386
- promise.reject("getDrawerStatus_failed", e.getMessage());
1387
- }
1388
- }
1389
-
1390
- @ReactMethod
1391
- public void getOpenDrawerTimes(final Promise promise) {
1392
- try {
1393
- if (iminPrintUtils == null) {
1394
- promise.resolve(PrinterHelper.getInstance().getOpenDrawerTimes());
1395
- } else {
1396
- promise.resolve(null);
1397
- }
1398
- } catch (Exception e) {
1399
- promise.reject("getOpenDrawerTimes_failed", e.getMessage());
1400
- }
1401
- }
1402
-
1403
- @ReactMethod
1404
- public void getServiceVersion(final Promise promise) {
1405
- try {
1406
- if (iminPrintUtils == null) {
1407
- promise.resolve(PrinterHelper.getInstance().getServiceVersion());
1408
- } else {
1409
- promise.resolve(null);
1410
- }
1411
- } catch (Exception e) {
1412
- promise.reject("getServiceVersion_failed", e.getMessage());
1413
- }
1414
- }
1415
-
1416
- @ReactMethod
1417
- public void getUsbPrinterVidPid(final Promise promise) {
1418
- try {
1419
- if (iminPrintUtils == null) {
1420
- promise.resolve(PrinterHelper.getInstance().getUsbPrinterVidPid());
1421
- } else {
1422
- promise.resolve(null);
1423
- }
1424
- } catch (Exception e) {
1425
- promise.reject("getUsbPrinterVidPid_failed", e.getMessage());
1426
- }
1427
- }
1428
-
1429
- @ReactMethod
1430
- public void getUsbDevicesName(final Promise promise) {
1431
- try {
1432
- if (iminPrintUtils == null) {
1433
- promise.resolve(PrinterHelper.getInstance().getUsbDevicesName());
1434
- } else {
1435
- promise.resolve(null);
1436
- }
1437
- } catch (Exception e) {
1438
- promise.reject("getUsbDevicesName_failed", e.getMessage());
1439
- }
1440
- }
1441
-
1442
-
1443
- @ReactMethod
1444
- public void setCodeAlignment(int align, final Promise promise) {
1445
- try {
1446
- if (iminPrintUtils == null) {
1447
- PrinterHelper.getInstance().setCodeAlignment(align);
1448
- }
1449
- promise.resolve(null);
1450
- } catch (Exception e) {
1451
- promise.reject("setCodeAlignment_failed", e.getMessage());
1452
- }
1453
- }
1454
-
1455
- @ReactMethod
1456
- public void setTextBitmapTypeface(int typeface, final Promise promise) {
1457
- try {
1458
- if (iminPrintUtils == null) {
1459
- switch (typeface) {
1460
- case 1:
1461
- PrinterHelper.getInstance().setTextBitmapTypeface("Typeface.MONOSPACE");
1462
- break;
1463
- case 2:
1464
- PrinterHelper.getInstance().setTextBitmapTypeface("Typeface.DEFAULT_BOLD");
1465
- break;
1466
- case 3:
1467
- PrinterHelper.getInstance().setTextBitmapTypeface("Typeface.SANS_SERIF");
1468
- break;
1469
- case 4:
1470
- PrinterHelper.getInstance().setTextBitmapTypeface("Typeface.SERIF");
1471
- break;
1472
- default:
1473
- PrinterHelper.getInstance().setTextBitmapTypeface("Typeface.DEFAULT");
1474
- break;
1475
- }
1476
- }
1477
- promise.resolve(null);
1478
- } catch (Exception e) {
1479
- promise.reject("setTextBitmapTypeface_failed", e.getMessage());
1480
- }
1481
- }
1482
-
1483
- @ReactMethod
1484
- public void setTextBitmapSize(int size, final Promise promise) {
1485
- try {
1486
- if (iminPrintUtils == null) {
1487
- PrinterHelper.getInstance().setTextBitmapSize(size);
1488
- }
1489
- promise.resolve(null);
1490
- } catch (Exception e) {
1491
- promise.reject("setTextBitmapSize_failed", e.getMessage());
1492
- }
1493
- }
1494
-
1495
- @ReactMethod
1496
- public void setTextBitmapStyle(int style, final Promise promise) {
1497
- try {
1498
- if (iminPrintUtils == null) {
1499
- PrinterHelper.getInstance().setTextBitmapStyle(style);
1500
- }
1501
- promise.resolve(null);
1502
- } catch (Exception e) {
1503
- promise.reject("setTextBitmapStyle_failed", e.getMessage());
1504
- }
1505
- }
1506
-
1507
- @ReactMethod
1508
- public void setTextBitmapStrikeThru(boolean strikeThru, final Promise promise) {
1509
- try {
1510
- if (iminPrintUtils == null) {
1511
- PrinterHelper.getInstance().setTextBitmapStrikeThru(strikeThru);
1512
- }
1513
- promise.resolve(null);
1514
- } catch (Exception e) {
1515
- promise.reject("setTextBitmapStrikeThru_failed", e.getMessage());
1516
- }
1517
- }
1518
-
1519
- @ReactMethod
1520
- public void setTextBitmapUnderline(boolean haveUnderline, final Promise promise) {
1521
- try {
1522
- if (iminPrintUtils == null) {
1523
- PrinterHelper.getInstance().setTextBitmapUnderline(haveUnderline);
1524
- }
1525
- promise.resolve(null);
1526
- } catch (Exception e) {
1527
- promise.reject("setTextBitmapUnderline_failed", e.getMessage());
1528
- }
1529
- }
1530
-
1531
- @ReactMethod
1532
- public void setTextBitmapLineSpacing(float lineHeight, final Promise promise) {
1533
- try {
1534
- if (iminPrintUtils == null) {
1535
- PrinterHelper.getInstance().setTextBitmapLineSpacing(lineHeight);
1536
- }
1537
- promise.resolve(null);
1538
- } catch (Exception e) {
1539
- promise.reject("setTextBitmapLineSpacing_failed", e.getMessage());
1540
- }
1541
- }
1542
-
1543
- @ReactMethod
1544
- public void setTextBitmapLetterSpacing(float space, final Promise promise) {
1545
- try {
1546
- if (iminPrintUtils == null) {
1547
- PrinterHelper.getInstance().setTextBitmapLetterSpacing(space);
1548
- }
1549
- promise.resolve(null);
1550
- } catch (Exception e) {
1551
- promise.reject("setTextBitmapLetterSpacing_failed", e.getMessage());
1552
- }
1553
- }
1554
-
1555
- @ReactMethod
1556
- public void setTextBitmapAntiWhite(boolean antiWhite, final Promise promise) {
1557
- try {
1558
- if (iminPrintUtils == null) {
1559
- PrinterHelper.getInstance().setTextBitmapAntiWhite(antiWhite);
1560
- }
1561
- promise.resolve(null);
1562
- } catch (Exception e) {
1563
- promise.reject("setTextBitmapAntiWhite_failed", e.getMessage());
1564
- }
1565
- }
1566
-
1567
- @ReactMethod
1568
- public void printTextBitmap(ReadableMap config, final Promise promise) {
1569
- try {
1570
- if (iminPrintUtils == null) {
1571
- String text = config.getString("text");
1572
- if (config.hasKey("align")) {
1573
- int align = config.getInt("align");
1574
- PrinterHelper.getInstance().printTextBitmapWithAli(text + "\n", align, null);
1575
- } else {
1576
- PrinterHelper.getInstance().printTextBitmap(text + "\n", null);
1577
- }
1578
- }
1579
- promise.resolve(null);
1580
- } catch (Exception e) {
1581
- promise.reject("printTextBitmap_failed", e.getMessage());
1582
- }
1583
- }
1584
-
1585
- @ReactMethod
1586
- public void enterPrinterBuffer(boolean isClean, final Promise promise) {
1587
- try {
1588
- if (iminPrintUtils == null) {
1589
- PrinterHelper.getInstance().enterPrinterBuffer(isClean);
1590
- }
1591
- promise.resolve(null);
1592
- } catch (Exception e) {
1593
- promise.reject("enterPrinterBuffer_failed", e.getMessage());
1594
- }
1595
- }
1596
-
1597
- @ReactMethod
1598
- public void commitPrinterBuffer(final Promise promise) {
1599
- try {
1600
- if (iminPrintUtils == null) {
1601
- PrinterHelper.getInstance().commitPrinterBuffer(null);
1602
- }
1603
- promise.resolve(null);
1604
- } catch (Exception e) {
1605
- promise.reject("commitPrinterBuffer_failed", e.getMessage());
1606
- }
1607
- }
1608
-
1609
- @ReactMethod
1610
- public void exitPrinterBuffer(boolean isCommit, final Promise promise) {
1611
- try {
1612
- if (iminPrintUtils == null) {
1613
- PrinterHelper.getInstance().exitPrinterBuffer(isCommit);
1614
- }
1615
- promise.resolve(null);
1616
- } catch (Exception e) {
1617
- promise.reject("exitPrinterBuffer_failed", e.getMessage());
1618
- }
1619
- }
1620
-
1621
- private BroadcastReceiver createChargingStateBroadcastReceiver() {
1622
- return new BroadcastReceiver() {
1623
- @Override
1624
- public void onReceive(Context context, Intent intent) {
1625
- int status = intent.getIntExtra(ACTION_PRITER_STATUS, -1);
1626
- Log.d(TAG, "打印机状态:" + intent.getAction());
1627
- // 发送事件到React Native
1628
- if (intent.getAction().equals(ACTION_PRITER_STATUS_CHANGE)) {
1629
- WritableMap result = Arguments.createMap();
1630
- WritableMap payload = Arguments.createMap();
1631
- payload.putString("message", Utils.getInstance().getPrinterStatusText(status));
1632
- payload.putString("code", String.format("%d", status));
1633
- result.putMap("eventData", payload);
1634
- result.putString("eventName", "printer_status");
1635
- sendEvent(reactContext, "eventBroadcast", result);
1636
- }
1637
- }
1638
- };
1639
- }
1640
-
1641
- private void initializeBroadcastReceiver() {
1642
- IntentFilter intentFilter = new IntentFilter();
1643
- mBroadcastReceiver = createChargingStateBroadcastReceiver();
1644
- intentFilter.addAction(ACTION_PRITER_STATUS_CHANGE);
1645
- intentFilter.addAction(ACTION_POGOPIN_STATUS_CHANGE);
1646
- getReactApplicationContext().registerReceiver(mBroadcastReceiver, intentFilter);
1647
- }
1648
-
1649
- @ReactMethod
1650
- public void addListener(String eventName) {
1651
- }
1652
-
1653
- @ReactMethod
1654
- public void removeListeners(Integer count) {
1655
- }
1656
-
1657
- @Override
1658
- public Map<String, Object> getConstants() {
1659
- final Map<String, Object> constants = new HashMap<>();
1660
- constants.put("SDK_VERSION_IMIN", sdkVersion);
1661
- return constants;
1662
- }
1663
-
1664
- }
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
+ }