Browse Source

打印机适配BTP

周素华 10 months ago
parent
commit
bc9a22c5df

+ 24 - 4
android/app/src/main/java/io/flutter/plugins/BluetoothPlugin.java

@@ -33,6 +33,7 @@ import java.security.Permission;
33 33
 import java.util.ArrayList;
34 34
 import java.util.HashMap;
35 35
 import java.util.List;
36
+import java.util.Map;
36 37
 import java.util.concurrent.ExecutorService;
37 38
 import java.util.concurrent.LinkedBlockingDeque;
38 39
 import java.util.concurrent.ThreadFactory;
@@ -178,6 +179,9 @@ public class BluetoothPlugin implements FlutterPlugin, MethodChannel.MethodCallH
178 179
                 Log.d("ble", "startBluetoothPrintBarCode :" + methodCall.arguments);
179 180
                 startBluetoothPrintBarCode(methodCall, result);
180 181
                 break;
182
+            case "startBluetoothPrintBitMapAndText":
183
+                Log.d("ble", "PrintBitMapAndText :" + methodCall.arguments);
184
+                printBitMapAndText(methodCall, result);
181 185
 
182 186
 
183 187
             case "autoLogin":
@@ -340,9 +344,6 @@ public class BluetoothPlugin implements FlutterPlugin, MethodChannel.MethodCallH
340 344
     void startBluetoothPrintText(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result) {
341 345
 
342 346
         ArrayList<String> textList = (ArrayList<String>) methodCall.arguments;
343
-//
344
-//        Log.d("ble", "imageByte:" + imageByte);
345
-//        Bitmap bitmap = Bytes2Bimap(imageByte);
346 347
 
347 348
         BluetoothDevice bluetoothDevice = mBluetoothAdapter.getRemoteDevice(itemPosition.getDeviceHardwareAddress());
348 349
         if (ActivityCompat.checkSelfPermission(_applicationContext, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
@@ -350,12 +351,31 @@ public class BluetoothPlugin implements FlutterPlugin, MethodChannel.MethodCallH
350 351
         }
351 352
         BlueDeviceInfo blueDeviceInfo = new BlueDeviceInfo(bluetoothDevice.getName(), bluetoothDevice.getAddress(), itemPosition.getConnectState());
352 353
         if(blueDeviceInfo.getDeviceName().contains("BTP")) {
353
-//            BTPPrintUtil.printBitMap(bitmap, blueDeviceInfo.getDeviceName(), result);
354
+            BTPPrintUtil.printText(textList, result);
354 355
         } else {
355 356
             PrintUtil.printText(textList, result);
356 357
         }
357 358
     }
358 359
 
360
+    void printBitMapAndText(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result) {
361
+
362
+        HashMap map = (HashMap)methodCall.arguments;
363
+        byte[] imageByte = (byte[]) map.get("bytes");
364
+        ArrayList<String> textList = (ArrayList<String>) map.get("textList");
365
+
366
+        Bitmap bitmap = Bytes2Bimap(imageByte);
367
+        BluetoothDevice bluetoothDevice = mBluetoothAdapter.getRemoteDevice(itemPosition.getDeviceHardwareAddress());
368
+        if (ActivityCompat.checkSelfPermission(_applicationContext, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
369
+            return;
370
+        }
371
+        BlueDeviceInfo blueDeviceInfo = new BlueDeviceInfo(bluetoothDevice.getName(), bluetoothDevice.getAddress(), itemPosition.getConnectState());
372
+        if(blueDeviceInfo.getDeviceName().contains("BTP")) {
373
+            BTPPrintUtil.PrintBitMapAndText(bitmap,  blueDeviceInfo.getDeviceName(), textList, result);
374
+        }
375
+
376
+    }
377
+
378
+
359 379
     void startBluetoothPrintBarCode(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result) {
360 380
 
361 381
         byte[] imageByte = (byte[]) methodCall.arguments;

+ 123 - 4
android/app/src/main/java/io/flutter/plugins/utils/BTPPrintUtil.java

@@ -10,6 +10,8 @@ import com.snbc.sdk.LabelPrinter;
10 10
 
11 11
 import java.io.ByteArrayOutputStream;
12 12
 import java.nio.ByteBuffer;
13
+import java.util.ArrayList;
14
+import java.util.List;
13 15
 
14 16
 import io.flutter.plugin.common.MethodChannel;
15 17
 
@@ -73,13 +75,130 @@ public class BTPPrintUtil {
73 75
         result.success(code == 0 ? true : false);
74 76
     }
75 77
 
76
-    public  static void printBarCodeWithText(String barCode, String text, @NonNull MethodChannel.Result result) {
78
+    public  static void printText(ArrayList<String> textList,@NonNull MethodChannel.Result result) {
79
+
80
+        int width = 560;
81
+        int height = 640;
82
+        getInstance().SetLabelSize(width,height);
83
+
84
+        int marginX = 2;
85
+        int marginY = 40;
86
+        //矩形框类型
87
+        int rectangleWidth = width - marginX * 2;
88
+        int rectangleHeight = height - marginY * 2;
89
+        int lineWidth = 1;
90
+
91
+        int lineHeight = rectangleHeight / 6;
92
+
93
+        int titleWidth = rectangleWidth / 2;
94
+
95
+        float fontSize = 3;
96
+        // 设置初始偏移量
97
+        int offsetY = 0;
98
+        int offsetX = 0;
99
+        // 计算绘制线条的 y 坐标
100
+        int secondLineY = marginY + lineHeight * 2 - lineWidth + offsetY;
101
+        int thirdLineY = marginY + lineHeight * 3 - lineWidth + offsetY;
102
+        int fourthLineY = marginY + lineHeight * 4 - lineWidth + offsetY;
103
+        int fiveLineY = marginY + lineHeight * 5 - lineWidth + offsetY;
104
+
105
+        int textAlignHorizontal = 0; // 左对齐
106
+
107
+        labelPrinter.PrintText(marginX * 2 + offsetX, marginY + offsetY, "3", textList.get(0), 0, 7, 0, 0);
108
+        labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, marginY + offsetY, "3", textList.get(1), 0, 7, 0, 0);
109
+
110
+
111
+        labelPrinter.PrintText(marginX * 2 + offsetX, marginY + lineHeight - lineWidth + offsetY, "3", textList.get(2), 0, 7, 0, 0);
112
+
113
+
114
+        labelPrinter.PrintText(marginX * 2 + offsetX, secondLineY, "3", textList.get(3), 0, 7, 0, 0);
115
+
116
+
117
+        labelPrinter.PrintText(marginX * 2 + offsetX, thirdLineY, "3", textList.get(4), 0, 7, 0, 0);
118
+        labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, thirdLineY, "3", textList.get(5), 0, 7, 0, 0);
119
+
120
+
121
+        labelPrinter.PrintText(marginX * 2 + offsetX, fourthLineY, "3", textList.get(6), 0, 7, 0, 0);
122
+        labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, fourthLineY, "3", textList.get(7), 0, 7, 0, 0);
123
+
124
+
125
+        labelPrinter.PrintText(marginX * 2 + offsetX, fiveLineY, "3", textList.get(8), 0, 7, 0, 0);
126
+        labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, fiveLineY, "3", textList.get(9), 0, 7, 0, 0);
127
+
77 128
 
78
-        getInstance().SetLabelSize(560,640);
79
-        labelPrinter.PrintBarcodeQR(80, 20,  0, "SHXM2024007404", 'H', 18, 2);
80
-        labelPrinter.PrintText(100, 500, "8", "SHXM2024007404", 0, 7, 3, 0);
81 129
         int code = getInstance().PrintLabel(1,1);
82 130
         result.success(code == 0 ? true : false);
83 131
     }
84 132
 
133
+    // BTP打印机同时打印图标和标签只能调这个,依次printBitMap再printText会导致偶发printText不走。
134
+    public  static  void PrintBitMapAndText(Bitmap bitmap, String deviceName, ArrayList<String> textList,@NonNull MethodChannel.Result result) {
135
+        int x = 0;
136
+        if(deviceName.contains("BTP-UP321")) {
137
+            x = 0;
138
+        } else if(deviceName.contains("BTP-P398PLUS")) {
139
+            x = 40;
140
+        }
141
+        int bytes = bitmap.getByteCount();
142
+        ByteBuffer buf = ByteBuffer.allocate(bytes);
143
+        bitmap.copyPixelsToBuffer(buf);
144
+        ByteArrayOutputStream LionData=new ByteArrayOutputStream();
145
+        bitmap.compress(Bitmap.CompressFormat.PNG, 100, LionData);
146
+
147
+        getInstance().SetLabelSize(560,640*2);
148
+        int code = getInstance().PrintImageData(x, 0, LionData.toByteArray());
149
+
150
+
151
+        int width = 560;
152
+        int height = 640;
153
+
154
+        int marginX = 2;
155
+        int marginY = 80;
156
+        //矩形框类型
157
+        int rectangleWidth = width - marginX * 2;
158
+        int rectangleHeight = height - marginY * 2;
159
+        int lineWidth = 1;
160
+
161
+        int lineHeight = rectangleHeight / 6;
162
+
163
+        int titleWidth = rectangleWidth / 2;
164
+
165
+        float fontSize = 3;
166
+        // 设置初始偏移量
167
+        int offsetY = 680;
168
+        int offsetX = 0;
169
+        // 计算绘制线条的 y 坐标
170
+        int secondLineY = marginY + lineHeight * 2 - lineWidth + offsetY;
171
+        int thirdLineY = marginY + lineHeight * 3 - lineWidth + offsetY;
172
+        int fourthLineY = marginY + lineHeight * 4 - lineWidth + offsetY;
173
+        int fiveLineY = marginY + lineHeight * 5 - lineWidth + offsetY;
174
+
175
+        int textAlignHorizontal = 0; // 左对齐
176
+
177
+        labelPrinter.PrintText(marginX * 2 + offsetX, marginY + offsetY, "3", textList.get(0), 0, 7, 0, 0);
178
+        labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, marginY + offsetY, "3", textList.get(1), 0, 7, 0, 0);
179
+
180
+
181
+        labelPrinter.PrintText(marginX * 2 + offsetX, marginY + lineHeight - lineWidth + offsetY, "3", textList.get(2), 0, 7, 0, 0);
182
+
183
+
184
+        labelPrinter.PrintText(marginX * 2 + offsetX, secondLineY, "3", textList.get(3), 0, 7, 0, 0);
185
+
186
+
187
+        labelPrinter.PrintText(marginX * 2 + offsetX, thirdLineY, "3", textList.get(4), 0, 7, 0, 0);
188
+        labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, thirdLineY, "3", textList.get(5), 0, 7, 0, 0);
189
+
190
+
191
+        labelPrinter.PrintText(marginX * 2 + offsetX, fourthLineY, "3", textList.get(6), 0, 7, 0, 0);
192
+        labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, fourthLineY, "3", textList.get(7), 0, 7, 0, 0);
193
+
194
+
195
+        labelPrinter.PrintText(marginX * 2 + offsetX, fiveLineY, "3", textList.get(8), 0, 7, 0, 0);
196
+        labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, fiveLineY, "3", textList.get(9), 0, 7, 0, 0);
197
+
198
+
199
+
200
+
201
+        getInstance().PrintLabel(1,1);
202
+        result.success(code == 0 ? true : false);
203
+    }
85 204
 }

+ 6 - 2
lib/page/print/print_page.dart

@@ -99,8 +99,12 @@ class _PrintPageState extends BaseLifecycleState<PrintPage> {
99 99
 
100 100
 
101 101
     MyNavigator.showLoading(msg: '打印中...');
102
-    await PrintService.startBluetoothPrintBitMap(smallUint8List!);
103
-    await PrintService.startBluetoothPrintText(widget.args.textList);
102
+    if(PrintService.connectedDeviceList.last.deviceName.contains("BTP")) {
103
+      await PrintService.startBluetoothPrintBitMapAndText(smallUint8List!, widget.args.textList);
104
+    } else {
105
+      await PrintService.startBluetoothPrintBitMap(smallUint8List!);
106
+      await PrintService.startBluetoothPrintText(widget.args.textList);
107
+    }
104 108
     MyNavigator.dismiss();
105 109
     MyNavigator.showToast('打印成功');
106 110
   }

+ 10 - 0
lib/plugin/bluetooth_plugin.dart

@@ -100,6 +100,16 @@ class BluetoothPlugin {
100 100
   }
101 101
 
102 102
   //开始打印
103
+  Future<bool> startBluetoothPrintBitMapAndText(Uint8List bytes,List<String> textList) async {
104
+
105
+    if (Platform.isIOS) {
106
+      return false;
107
+    }
108
+    return _channel.invokeMethod('startBluetoothPrintBitMapAndText', {"bytes": bytes, "textList": textList}).then<bool>((d) => d);
109
+  }
110
+
111
+
112
+  //开始打印
103 113
   Future<bool> startBluetoothPrintBarCodeWithText(String barCode, String text) async {
104 114
 
105 115
     if (Platform.isIOS) {

+ 10 - 0
lib/service/print_service.dart

@@ -139,6 +139,16 @@ class PrintService {
139 139
     }
140 140
   }
141 141
 
142
+  // 打印打印图片和文字只给BTP设备使用
143
+  static Future<bool> startBluetoothPrintBitMapAndText(Uint8List bytes,List<String> textList) async {
144
+
145
+    if(await canExecAction()) {
146
+      return await BluetoothPlugin.instance.startBluetoothPrintBitMapAndText(bytes, textList);
147
+    } else {
148
+      return false;
149
+    }
150
+  }
151
+
142 152
   //开始打印图片
143 153
   static Future<bool> startBluetoothPrintBitMap(Uint8List bytes) async {
144 154