react-native-printer-imin 0.10.4 → 0.10.6

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