@simplymobile/capacitor-bixolon-printer 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,725 @@
1
+ package pl.simplymobile.plugins.bixolonprinter;
2
+
3
+ import android.bluetooth.BluetoothAdapter;
4
+ import android.bluetooth.BluetoothDevice;
5
+ import android.bluetooth.BluetoothSocket;
6
+ import android.graphics.Bitmap;
7
+ import android.graphics.BitmapFactory;
8
+ import android.graphics.Canvas;
9
+ import android.graphics.Matrix;
10
+ import android.graphics.Paint;
11
+ import android.os.Build;
12
+ import android.util.Base64;
13
+ import android.util.Log;
14
+
15
+ import com.getcapacitor.JSArray;
16
+ import com.getcapacitor.JSObject;
17
+ import com.getcapacitor.PermissionState;
18
+ import com.getcapacitor.Plugin;
19
+ import com.getcapacitor.PluginCall;
20
+ import com.getcapacitor.PluginMethod;
21
+ import com.getcapacitor.annotation.CapacitorPlugin;
22
+ import com.getcapacitor.annotation.Permission;
23
+ import com.getcapacitor.annotation.PermissionCallback;
24
+
25
+ import java.io.IOException;
26
+ import java.io.InputStream;
27
+ import java.io.OutputStream;
28
+ import java.util.ArrayList;
29
+ import java.util.List;
30
+ import java.util.Set;
31
+ import java.util.UUID;
32
+
33
+ @CapacitorPlugin(
34
+ name = "BixolonPrinter",
35
+ permissions = {
36
+ @Permission(
37
+ alias = BixolonPrinterPlugin.BLUETOOTH_ALIAS,
38
+ strings = { "android.permission.BLUETOOTH_CONNECT", "android.permission.BLUETOOTH_SCAN" }
39
+ )
40
+ }
41
+ )
42
+ public class BixolonPrinterPlugin extends Plugin {
43
+
44
+ public static final String BLUETOOTH_ALIAS = "bluetooth";
45
+
46
+ private static final String LOG_TAG = "BixolonPrinter";
47
+
48
+ private BluetoothAdapter mBluetoothAdapter;
49
+ private BluetoothSocket mmSocket;
50
+ private BluetoothDevice mmDevice;
51
+
52
+ private OutputStream mmOutputStream;
53
+ private InputStream mmInputStream;
54
+
55
+ private Thread workerThread;
56
+ private byte[] readBuffer;
57
+ private int readBufferPosition;
58
+ private volatile boolean stopWorker;
59
+
60
+ private static final byte[] ESC_ALIGN_LEFT = { 0x1B, 0x61, 0x00 };
61
+ private static final byte[] ESC_ALIGN_RIGHT = { 0x1B, 0x61, 0x02 };
62
+ private static final byte[] ESC_ALIGN_CENTER = { 0x1B, 0x61, 0x01 };
63
+
64
+ private static final byte[] CUT_PAPER = { 0x1b, 0x69 };
65
+
66
+ private static String CURRENT_BRAND = "generic";
67
+
68
+ // region Permission handling
69
+
70
+ private boolean ensureBluetoothPermission(PluginCall call) {
71
+ // Legacy BLUETOOTH permission is an install-time permission on Android <= 11.
72
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
73
+ return true;
74
+ }
75
+ if (getPermissionState(BLUETOOTH_ALIAS) == PermissionState.GRANTED) {
76
+ return true;
77
+ }
78
+ requestPermissionForAlias(BLUETOOTH_ALIAS, call, "bluetoothPermsCallback");
79
+ return false;
80
+ }
81
+
82
+ @PermissionCallback
83
+ private void bluetoothPermsCallback(PluginCall call) {
84
+ if (getPermissionState(BLUETOOTH_ALIAS) != PermissionState.GRANTED) {
85
+ call.reject("Bluetooth permission denied");
86
+ return;
87
+ }
88
+ switch (call.getMethodName()) {
89
+ case "list":
90
+ list(call);
91
+ break;
92
+ case "connect":
93
+ connect(call);
94
+ break;
95
+ case "getStatus":
96
+ getStatus(call);
97
+ break;
98
+ default:
99
+ call.reject("Unknown method after permission grant: " + call.getMethodName());
100
+ break;
101
+ }
102
+ }
103
+
104
+ // endregion
105
+
106
+ // region Plugin methods
107
+
108
+ @PluginMethod
109
+ public void list(PluginCall call) {
110
+ if (!ensureBluetoothPermission(call)) {
111
+ return;
112
+ }
113
+ try {
114
+ mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
115
+ if (mBluetoothAdapter == null) {
116
+ call.reject("NO BLUETOOTH ADAPTER AVAILABLE");
117
+ return;
118
+ }
119
+ if (!mBluetoothAdapter.isEnabled()) {
120
+ call.reject("BLUETOOTH IS DISABLED");
121
+ return;
122
+ }
123
+
124
+ Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
125
+ JSArray devices = new JSArray();
126
+ if (pairedDevices != null) {
127
+ for (BluetoothDevice device : pairedDevices) {
128
+ JSObject jsDevice = new JSObject();
129
+ jsDevice.put("name", device.getName());
130
+ jsDevice.put("address", device.getAddress());
131
+ jsDevice.put("type", device.getType());
132
+ devices.put(jsDevice);
133
+ }
134
+ }
135
+
136
+ JSObject ret = new JSObject();
137
+ ret.put("devices", devices);
138
+ call.resolve(ret);
139
+ } catch (Exception e) {
140
+ Log.e(LOG_TAG, "list failed", e);
141
+ call.reject(e.getMessage(), e);
142
+ }
143
+ }
144
+
145
+ @PluginMethod
146
+ public void getStatus(PluginCall call) {
147
+ if (!ensureBluetoothPermission(call)) {
148
+ return;
149
+ }
150
+ try {
151
+ JSObject ret = new JSObject();
152
+ BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
153
+ boolean enabled = adapter != null && adapter.isEnabled();
154
+ ret.put("enabled", enabled);
155
+
156
+ boolean connected = mmSocket != null && mmSocket.isConnected();
157
+ ret.put("connected", connected);
158
+ if (connected) {
159
+ BluetoothDevice connectedDevice = mmSocket.getRemoteDevice();
160
+ ret.put("name", connectedDevice.getName());
161
+ ret.put("address", connectedDevice.getAddress());
162
+ }
163
+ call.resolve(ret);
164
+ } catch (Exception e) {
165
+ Log.e(LOG_TAG, "getStatus failed", e);
166
+ call.reject(e.getMessage(), e);
167
+ }
168
+ }
169
+
170
+ @PluginMethod
171
+ public void connect(PluginCall call) {
172
+ if (!ensureBluetoothPermission(call)) {
173
+ return;
174
+ }
175
+ String name = call.getString("name");
176
+ if (name == null || name.isEmpty()) {
177
+ call.reject("Missing required parameter: name");
178
+ return;
179
+ }
180
+ try {
181
+ if (!findBT(name)) {
182
+ call.reject("BLUETOOTH DEVICE NOT FOUND: " + name);
183
+ return;
184
+ }
185
+
186
+ // Standard SerialPortService ID
187
+ UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
188
+ mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
189
+ mmSocket.connect();
190
+ mmOutputStream = mmSocket.getOutputStream();
191
+ mmInputStream = mmSocket.getInputStream();
192
+ beginListenForData();
193
+
194
+ Log.d(LOG_TAG, "BLUETOOTH OPENED: " + mmDevice.getName());
195
+ JSObject ret = new JSObject();
196
+ ret.put("message", "BLUETOOTH OPENED: " + mmDevice.getName());
197
+ call.resolve(ret);
198
+ } catch (Exception e) {
199
+ Log.e(LOG_TAG, "connect failed", e);
200
+ call.reject(e.getMessage(), e);
201
+ }
202
+ }
203
+
204
+ @PluginMethod
205
+ public void disconnect(PluginCall call) {
206
+ try {
207
+ stopWorker = true;
208
+ if (mmOutputStream != null) {
209
+ mmOutputStream.close();
210
+ }
211
+ if (mmInputStream != null) {
212
+ mmInputStream.close();
213
+ }
214
+ if (mmSocket != null) {
215
+ mmSocket.close();
216
+ }
217
+ mmOutputStream = null;
218
+ mmInputStream = null;
219
+ mmSocket = null;
220
+ call.resolve();
221
+ } catch (Exception e) {
222
+ Log.e(LOG_TAG, "disconnect failed", e);
223
+ call.reject(e.getMessage(), e);
224
+ }
225
+ }
226
+
227
+ @PluginMethod
228
+ public void printText(PluginCall call) {
229
+ String text = call.getString("text");
230
+ if (text == null) {
231
+ call.reject("Missing required parameter: text");
232
+ return;
233
+ }
234
+ if (!isConnected(call)) {
235
+ return;
236
+ }
237
+ try {
238
+ mmOutputStream.write(text.getBytes());
239
+ Log.d(LOG_TAG, "PRINT TEXT SEND -> " + text);
240
+ call.resolve();
241
+ } catch (Exception e) {
242
+ Log.e(LOG_TAG, "printText failed", e);
243
+ call.reject(e.getMessage(), e);
244
+ }
245
+ }
246
+
247
+ @PluginMethod
248
+ public void printBase64(PluginCall call) {
249
+ String data = call.getString("data");
250
+ if (data == null) {
251
+ call.reject("Missing required parameter: data");
252
+ return;
253
+ }
254
+ if (!isConnected(call)) {
255
+ return;
256
+ }
257
+ int align = call.getInt("align", 0);
258
+ try {
259
+ Bitmap image = convertBase64ToBitmap(data);
260
+ int maxWidth = 512;
261
+ Bitmap scaled = scaleBitmap(image, maxWidth, 512);
262
+
263
+ // Set alignment - only on non-Bixolon printers
264
+ if (!CURRENT_BRAND.equals("bixolon")) {
265
+ mmOutputStream.write(selAlignTitle(align));
266
+ }
267
+
268
+ mmOutputStream.write(decodeBitmapBase64(scaled));
269
+
270
+ Log.d(LOG_TAG, "PRINT BASE64 SEND");
271
+ call.resolve();
272
+ } catch (Exception e) {
273
+ Log.e(LOG_TAG, "printBase64 failed", e);
274
+ call.reject(e.getMessage(), e);
275
+ }
276
+ }
277
+
278
+ @PluginMethod
279
+ public void printImageUrl(PluginCall call) {
280
+ String url = call.getString("url");
281
+ if (url == null) {
282
+ call.reject("Missing required parameter: url");
283
+ return;
284
+ }
285
+ if (!isConnected(call)) {
286
+ return;
287
+ }
288
+ int align = call.getInt("align", 0);
289
+ try {
290
+ Bitmap bmp = BitmapFactory.decodeFile(url);
291
+ if (bmp == null) {
292
+ call.reject("PRINT PHOTO ERROR - THE FILE DOESN'T EXIST: " + url);
293
+ return;
294
+ }
295
+ byte[] command = decodeBitmapUrl(bmp);
296
+ switch (align) {
297
+ case 0:
298
+ printLeftImage(command);
299
+ break;
300
+ case 1:
301
+ printCenterImage(command);
302
+ break;
303
+ case 2:
304
+ printRightImage(command);
305
+ break;
306
+ default:
307
+ printLeftImage(command);
308
+ break;
309
+ }
310
+ call.resolve();
311
+ } catch (Exception e) {
312
+ Log.e(LOG_TAG, "printImageUrl failed", e);
313
+ call.reject(e.getMessage(), e);
314
+ }
315
+ }
316
+
317
+ @PluginMethod
318
+ public void printPOSCommand(PluginCall call) {
319
+ String command = call.getString("command");
320
+ if (command == null) {
321
+ call.reject("Missing required parameter: command");
322
+ return;
323
+ }
324
+ if (!isConnected(call)) {
325
+ return;
326
+ }
327
+ try {
328
+ mmOutputStream.write(hexStringToBytes(command));
329
+ Log.d(LOG_TAG, "POS COMMAND SENT");
330
+ call.resolve();
331
+ } catch (Exception e) {
332
+ Log.e(LOG_TAG, "printPOSCommand failed", e);
333
+ call.reject(e.getMessage(), e);
334
+ }
335
+ }
336
+
337
+ @PluginMethod
338
+ public void printQRCode(PluginCall call) {
339
+ String text = call.getString("text");
340
+ if (text == null) {
341
+ call.reject("Missing required parameter: text");
342
+ return;
343
+ }
344
+ if (!isConnected(call)) {
345
+ return;
346
+ }
347
+ try {
348
+ // In development - currently sends the raw string to the printer.
349
+ mmOutputStream.write(text.getBytes());
350
+ Log.d(LOG_TAG, "PRINT QRCODE SENT");
351
+ call.resolve();
352
+ } catch (Exception e) {
353
+ Log.e(LOG_TAG, "printQRCode failed", e);
354
+ call.reject(e.getMessage(), e);
355
+ }
356
+ }
357
+
358
+ @PluginMethod
359
+ public void cutPaper(PluginCall call) {
360
+ if (!isConnected(call)) {
361
+ return;
362
+ }
363
+ try {
364
+ mmOutputStream.write(CUT_PAPER);
365
+ Log.d(LOG_TAG, "CUT PAPER SENT");
366
+ call.resolve();
367
+ } catch (Exception e) {
368
+ Log.e(LOG_TAG, "cutPaper failed", e);
369
+ call.reject(e.getMessage(), e);
370
+ }
371
+ }
372
+
373
+ @PluginMethod
374
+ public void setBrand(PluginCall call) {
375
+ String brand = call.getString("brand");
376
+ if (brand == null || brand.isEmpty()) {
377
+ call.reject("Invalid brand supplied. Current brand remains " + CURRENT_BRAND);
378
+ return;
379
+ }
380
+ CURRENT_BRAND = brand;
381
+ call.resolve();
382
+ }
383
+
384
+ // endregion
385
+
386
+ // region Helpers
387
+
388
+ private boolean isConnected(PluginCall call) {
389
+ if (mmOutputStream == null || mmSocket == null || !mmSocket.isConnected()) {
390
+ call.reject("PRINTER NOT CONNECTED");
391
+ return false;
392
+ }
393
+ return true;
394
+ }
395
+
396
+ /**
397
+ * Looks for the specified bluetooth device among the bonded devices.
398
+ * If found, sets mmDevice to the device instance.
399
+ */
400
+ private boolean findBT(String name) {
401
+ mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
402
+ if (mBluetoothAdapter == null) {
403
+ Log.e(LOG_TAG, "NO BLUETOOTH ADAPTER AVAILABLE");
404
+ return false;
405
+ }
406
+ Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
407
+ if (pairedDevices != null) {
408
+ for (BluetoothDevice device : pairedDevices) {
409
+ if (device.getName() != null && device.getName().equalsIgnoreCase(name)) {
410
+ mmDevice = device;
411
+ return true;
412
+ }
413
+ }
414
+ }
415
+ return false;
416
+ }
417
+
418
+ // After opening a connection to the bluetooth printer device,
419
+ // we have to listen and check if data was sent back.
420
+ private void beginListenForData() {
421
+ try {
422
+ final byte delimiter = 10; // ASCII newline
423
+ stopWorker = false;
424
+ readBufferPosition = 0;
425
+ readBuffer = new byte[1024];
426
+ workerThread = new Thread(() -> {
427
+ while (!Thread.currentThread().isInterrupted() && !stopWorker) {
428
+ try {
429
+ int bytesAvailable = mmInputStream.available();
430
+ if (bytesAvailable > 0) {
431
+ byte[] packetBytes = new byte[bytesAvailable];
432
+ mmInputStream.read(packetBytes);
433
+ for (int i = 0; i < bytesAvailable; i++) {
434
+ byte b = packetBytes[i];
435
+ if (b == delimiter) {
436
+ byte[] encodedBytes = new byte[readBufferPosition];
437
+ System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
438
+ readBufferPosition = 0;
439
+ } else {
440
+ readBuffer[readBufferPosition++] = b;
441
+ }
442
+ }
443
+ }
444
+ } catch (IOException ex) {
445
+ stopWorker = true;
446
+ }
447
+ }
448
+ });
449
+ workerThread.start();
450
+ } catch (Exception e) {
451
+ Log.e(LOG_TAG, "beginListenForData failed", e);
452
+ }
453
+ }
454
+
455
+ // align text
456
+ private static byte[] selAlignTitle(int align) {
457
+ switch (align) {
458
+ case 0:
459
+ return ESC_ALIGN_LEFT;
460
+ case 1:
461
+ return ESC_ALIGN_CENTER;
462
+ case 2:
463
+ return ESC_ALIGN_RIGHT;
464
+ default:
465
+ return ESC_ALIGN_LEFT;
466
+ }
467
+ }
468
+
469
+ private void printLeftImage(byte[] msg) {
470
+ try {
471
+ mmOutputStream.write(ESC_ALIGN_LEFT);
472
+ mmOutputStream.write(msg);
473
+ } catch (IOException e) {
474
+ Log.e(LOG_TAG, "ERROR PRINT LEFT IMAGE", e);
475
+ }
476
+ }
477
+
478
+ private void printCenterImage(byte[] msg) {
479
+ try {
480
+ mmOutputStream.write(ESC_ALIGN_CENTER);
481
+ mmOutputStream.write(msg);
482
+ mmOutputStream.write(ESC_ALIGN_LEFT);
483
+ } catch (IOException e) {
484
+ Log.e(LOG_TAG, "ERROR PRINT CENTER IMAGE", e);
485
+ }
486
+ }
487
+
488
+ private void printRightImage(byte[] msg) {
489
+ try {
490
+ mmOutputStream.write(ESC_ALIGN_RIGHT);
491
+ mmOutputStream.write(msg);
492
+ mmOutputStream.write(ESC_ALIGN_LEFT);
493
+ } catch (IOException e) {
494
+ Log.e(LOG_TAG, "ERROR PRINT RIGHT IMAGE", e);
495
+ }
496
+ }
497
+
498
+ private static byte[] decodeBitmapBase64(Bitmap bmp) {
499
+ int bmpWidth = bmp.getWidth();
500
+ int bmpHeight = bmp.getHeight();
501
+
502
+ List<String> bpmBinaryList = bitmapToBinaryList(bmp);
503
+ List<String> bmpHexList = binaryListToHexStringList(bpmBinaryList);
504
+ String commandHexString = "1D763000";
505
+
506
+ // there are 8 pixels per byte. In case of modulo: add 1 to compensate.
507
+ bmpWidth = bmpWidth % 8 == 0 ? bmpWidth / 8 : (bmpWidth / 8 + 1);
508
+ int xL = bmpWidth % 256;
509
+ int xH = (bmpWidth - xL) / 256;
510
+
511
+ String xLHex = Integer.toHexString(xL);
512
+ String xHHex = Integer.toHexString(xH);
513
+ if (xLHex.length() == 1) {
514
+ xLHex = "0" + xLHex;
515
+ }
516
+ if (xHHex.length() == 1) {
517
+ xHHex = "0" + xHHex;
518
+ }
519
+ String widthHexString = xLHex + xHHex;
520
+
521
+ int yL = bmpHeight % 256;
522
+ int yH = (bmpHeight - yL) / 256;
523
+
524
+ String yLHex = Integer.toHexString(yL);
525
+ String yHHex = Integer.toHexString(yH);
526
+ if (yLHex.length() == 1) {
527
+ yLHex = "0" + yLHex;
528
+ }
529
+ if (yHHex.length() == 1) {
530
+ yHHex = "0" + yHHex;
531
+ }
532
+ String heightHexString = yLHex + yHHex;
533
+
534
+ List<String> commandList = new ArrayList<>();
535
+ commandList.add(commandHexString + widthHexString + heightHexString);
536
+ commandList.addAll(bmpHexList);
537
+
538
+ return hexList2Byte(commandList);
539
+ }
540
+
541
+ private static byte[] decodeBitmapUrl(Bitmap bmp) {
542
+ int bmpWidth = bmp.getWidth();
543
+ int bmpHeight = bmp.getHeight();
544
+
545
+ List<String> bpmBinaryList = bitmapToBinaryList(bmp);
546
+ List<String> bmpHexList = binaryListToHexStringList(bpmBinaryList);
547
+ String commandHexString = "1D763000";
548
+ String widthHexString = Integer.toHexString(bmpWidth % 8 == 0 ? bmpWidth / 8 : (bmpWidth / 8 + 1));
549
+ if (widthHexString.length() > 2) {
550
+ Log.e(LOG_TAG, "decodeBitmap error: width is too large");
551
+ return null;
552
+ } else if (widthHexString.length() == 1) {
553
+ widthHexString = "0" + widthHexString;
554
+ }
555
+ widthHexString = widthHexString + "00";
556
+
557
+ String heightHexString = Integer.toHexString(bmpHeight);
558
+ if (heightHexString.length() > 2) {
559
+ Log.e(LOG_TAG, "decodeBitmap error: height is too large");
560
+ return null;
561
+ } else if (heightHexString.length() == 1) {
562
+ heightHexString = "0" + heightHexString;
563
+ }
564
+ heightHexString = heightHexString + "00";
565
+
566
+ List<String> commandList = new ArrayList<>();
567
+ commandList.add(commandHexString + widthHexString + heightHexString);
568
+ commandList.addAll(bmpHexList);
569
+
570
+ return hexList2Byte(commandList);
571
+ }
572
+
573
+ private static List<String> bitmapToBinaryList(Bitmap bmp) {
574
+ List<String> list = new ArrayList<>();
575
+ StringBuffer sb;
576
+
577
+ int zeroCount = bmp.getWidth() % 8;
578
+
579
+ StringBuilder zeroStr = new StringBuilder();
580
+ if (zeroCount > 0) {
581
+ for (int i = 0; i < (8 - zeroCount); i++) {
582
+ zeroStr.append("0");
583
+ }
584
+ }
585
+
586
+ for (int i = 0; i < bmp.getHeight(); i++) {
587
+ sb = new StringBuffer();
588
+ for (int j = 0; j < bmp.getWidth(); j++) {
589
+ int color = bmp.getPixel(j, i);
590
+
591
+ int r = (color >> 16) & 0xff;
592
+ int g = (color >> 8) & 0xff;
593
+ int b = color & 0xff;
594
+ // if color close to white, bit='0', else bit='1'
595
+ if (r > 160 && g > 160 && b > 160) {
596
+ sb.append("0");
597
+ } else {
598
+ sb.append("1");
599
+ }
600
+ }
601
+ if (zeroCount > 0) {
602
+ sb.append(zeroStr.toString());
603
+ }
604
+ list.add(sb.toString());
605
+ }
606
+ return list;
607
+ }
608
+
609
+ private static List<String> binaryListToHexStringList(List<String> list) {
610
+ List<String> hexList = new ArrayList<>();
611
+ for (String binaryStr : list) {
612
+ StringBuilder sb = new StringBuilder();
613
+ for (int i = 0; i < binaryStr.length(); i += 8) {
614
+ String str = binaryStr.substring(i, i + 8);
615
+ String hexString = myBinaryStrToHexString(str);
616
+ sb.append(hexString);
617
+ }
618
+ hexList.add(sb.toString());
619
+ }
620
+ return hexList;
621
+ }
622
+
623
+ private static final String[] binaryArray = {
624
+ "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
625
+ "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
626
+ };
627
+
628
+ private static String myBinaryStrToHexString(String binaryStr) {
629
+ StringBuilder hex = new StringBuilder();
630
+ String hexStr = "0123456789ABCDEF";
631
+ String f4 = binaryStr.substring(0, 4);
632
+ String b4 = binaryStr.substring(4, 8);
633
+ for (int i = 0; i < binaryArray.length; i++) {
634
+ if (f4.equals(binaryArray[i])) {
635
+ hex.append(hexStr.substring(i, i + 1));
636
+ }
637
+ }
638
+ for (int i = 0; i < binaryArray.length; i++) {
639
+ if (b4.equals(binaryArray[i])) {
640
+ hex.append(hexStr.substring(i, i + 1));
641
+ }
642
+ }
643
+ return hex.toString();
644
+ }
645
+
646
+ private static byte[] hexList2Byte(List<String> list) {
647
+ List<byte[]> commandList = new ArrayList<>();
648
+ for (String hexStr : list) {
649
+ commandList.add(hexStringToBytes(hexStr));
650
+ }
651
+ return sysCopy(commandList);
652
+ }
653
+
654
+ private static byte[] hexStringToBytes(String hexString) {
655
+ if (hexString == null || hexString.equals("")) {
656
+ return null;
657
+ }
658
+ hexString = hexString.toUpperCase();
659
+ int length = hexString.length() / 2;
660
+ char[] hexChars = hexString.toCharArray();
661
+ byte[] d = new byte[length];
662
+ for (int i = 0; i < length; i++) {
663
+ int pos = i * 2;
664
+ d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
665
+ }
666
+ return d;
667
+ }
668
+
669
+ private static byte charToByte(char c) {
670
+ return (byte) "0123456789ABCDEF".indexOf(c);
671
+ }
672
+
673
+ private static byte[] sysCopy(List<byte[]> srcArrays) {
674
+ int len = 0;
675
+ for (byte[] srcArray : srcArrays) {
676
+ len += srcArray.length;
677
+ }
678
+ byte[] destArray = new byte[len];
679
+ int destLen = 0;
680
+ for (byte[] srcArray : srcArrays) {
681
+ System.arraycopy(srcArray, 0, destArray, destLen, srcArray.length);
682
+ destLen += srcArray.length;
683
+ }
684
+ return destArray;
685
+ }
686
+
687
+ private Bitmap convertBase64ToBitmap(String b64) {
688
+ byte[] imageAsBytes = Base64.decode(b64.getBytes(), Base64.DEFAULT);
689
+ return BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
690
+ }
691
+
692
+ private Bitmap scaleBitmap(Bitmap bitmap, int maxWidth, int maxHeight) {
693
+ int width;
694
+ int height;
695
+ float widthRatio = (float) bitmap.getWidth() / maxWidth;
696
+ float heightRatio = (float) bitmap.getHeight() / maxHeight;
697
+ if (widthRatio >= heightRatio) {
698
+ width = maxWidth;
699
+ height = (int) (((float) width / bitmap.getWidth()) * bitmap.getHeight());
700
+ } else {
701
+ height = maxHeight;
702
+ width = (int) (((float) height / bitmap.getHeight()) * bitmap.getWidth());
703
+ }
704
+ Bitmap scaledBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
705
+
706
+ float ratioX = (float) width / bitmap.getWidth();
707
+ float ratioY = (float) height / bitmap.getHeight();
708
+ float middleX = width / 2.0f;
709
+ float middleY = height / 2.0f;
710
+ Matrix scaleMatrix = new Matrix();
711
+ scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
712
+
713
+ Canvas canvas = new Canvas(scaledBitmap);
714
+ canvas.setMatrix(scaleMatrix);
715
+ canvas.drawBitmap(
716
+ bitmap,
717
+ middleX - bitmap.getWidth() / 2,
718
+ middleY - bitmap.getHeight() / 2,
719
+ new Paint(Paint.FILTER_BITMAP_FLAG)
720
+ );
721
+ return scaledBitmap;
722
+ }
723
+
724
+ // endregion
725
+ }