|
@@ -297,4 +297,196 @@ public class PrintUtil {
|
297
|
297
|
}
|
298
|
298
|
|
299
|
299
|
|
|
300
|
+ public static void printText(ArrayList<String> textList, @NonNull MethodChannel.Result result) {
|
|
301
|
+ // 检查是否连接了打印机
|
|
302
|
+ if (PrintUtil.isConnection() != 0) {
|
|
303
|
+ result.success(false);
|
|
304
|
+ return;
|
|
305
|
+ }
|
|
306
|
+
|
|
307
|
+ // 重置错误和取消打印状态
|
|
308
|
+ //重置错误状态变量
|
|
309
|
+ final boolean[] isError = {false};
|
|
310
|
+ //重置取消打印状态变量
|
|
311
|
+ final boolean[] isCancel = {false};
|
|
312
|
+ // 初始化打印数据
|
|
313
|
+ ArrayList<String> jsonList = new ArrayList<>();
|
|
314
|
+ ArrayList<String> infoList = new ArrayList<>();
|
|
315
|
+
|
|
316
|
+ // 在每次打印任务前初始化生成的打印数据页数
|
|
317
|
+ int generatedPrintDataPageCount = 0;
|
|
318
|
+ // 设置打印的总页数和份数
|
|
319
|
+ int pageCount = 1;
|
|
320
|
+ int quantity = 1;
|
|
321
|
+ int totalQuantity = pageCount * quantity;
|
|
322
|
+ //setTotalQuantityOfPrints已废弃,使用方法含义更明确的setTotalPrintQuantity
|
|
323
|
+ PrintUtil.getInstance().setTotalPrintQuantity(totalQuantity);
|
|
324
|
+ // 打印参数设置
|
|
325
|
+ /*
|
|
326
|
+ * 参数1:打印浓度 ,参数2:纸张类型 参数3:打印模式
|
|
327
|
+ * 打印浓度 B50/B50W/T6/T7/T8 建议设置6或8,Z401/B32建议设置8,B3S/B21/B203/B1建议设置3
|
|
328
|
+ */
|
|
329
|
+ PrintUtil.getInstance().startPrintJob(3, 3, 1, new PrintCallback() {
|
|
330
|
+ @Override
|
|
331
|
+ public void onProgress(int pageIndex, int quantityIndex, HashMap<String, Object> hashMap) {
|
|
332
|
+ // 更新打印进度
|
|
333
|
+ String progressMessage = "打印进度:已打印到第" + pageIndex + "页,第" + quantityIndex + "份";
|
|
334
|
+ Log.d("ble", "测试:" + progressMessage);
|
|
335
|
+
|
|
336
|
+ // 处理打印完成情况
|
|
337
|
+ if (pageIndex == pageCount && quantityIndex == quantity) {
|
|
338
|
+ Log.d("ble", "测试:onProgress: 结束打印");
|
|
339
|
+ //endJob,使用方法含义更明确的endPrintJob
|
|
340
|
+ if (PrintUtil.getInstance().endPrintJob()) {
|
|
341
|
+ Log.d("ble", "结束打印成功");
|
|
342
|
+ } else {
|
|
343
|
+ Log.d("ble", "结束打印失败");
|
|
344
|
+ }
|
|
345
|
+ result.success(true);
|
|
346
|
+ }
|
|
347
|
+ }
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+ @Override
|
|
351
|
+ public void onError(int i) {
|
|
352
|
+
|
|
353
|
+ }
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+ @Override
|
|
357
|
+ public void onError(int errorCode, int printState) {
|
|
358
|
+ Log.d("ble", "测试:onError");
|
|
359
|
+ isError[0] = true;
|
|
360
|
+ result.success(false);
|
|
361
|
+ }
|
|
362
|
+
|
|
363
|
+ @Override
|
|
364
|
+ public void onCancelJob(boolean isSuccess) {
|
|
365
|
+ //取消打印成功回调
|
|
366
|
+ isCancel[0] = true;
|
|
367
|
+ }
|
|
368
|
+
|
|
369
|
+ /**
|
|
370
|
+ * SDK缓存空闲回调,可以在此处传入打印数据
|
|
371
|
+ *
|
|
372
|
+ * @param pageIndex 当前回调函数处理下一页的打印索引
|
|
373
|
+ * @param bufferSize 缓存空间的大小
|
|
374
|
+ */
|
|
375
|
+ @Override
|
|
376
|
+ public void onBufferFree(int pageIndex, int bufferSize) {
|
|
377
|
+ // 如果出现错误、已取消打印,或 pageIndex 超过总页数,则返回
|
|
378
|
+ if (isError[0] || isCancel[0] || pageIndex > pageCount) {
|
|
379
|
+ return;
|
|
380
|
+ }
|
|
381
|
+
|
|
382
|
+ Log.d(TAG, "测试-空闲数据回调-数据生成判断-总页数 " + pageCount + ",已生成页数:" + generatedPrintDataPageCount + ",空闲回调数据长度:" + bufferSize);
|
|
383
|
+ // 生成打印数据
|
|
384
|
+ generatePrintDataIfNeeded(textList, bufferSize, generatedPrintDataPageCount, pageCount, jsonList, infoList, quantity);
|
|
385
|
+ }
|
|
386
|
+ });
|
|
387
|
+ }
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+ /**
|
|
391
|
+ * 根据需要生成打印数据,确保不超过总页数限制。
|
|
392
|
+ *
|
|
393
|
+ * @param bufferSize 当前缓存空间的大小。
|
|
394
|
+ */
|
|
395
|
+ private static void generatePrintDataIfNeeded(ArrayList<String> textList,int bufferSize, int generatedPrintDataPageCount, int pageCount, ArrayList<String> jsonList, ArrayList<String> infoList, int quantity) {
|
|
396
|
+ // 如果已生成的打印数据页数小于总页数,则继续生成
|
|
397
|
+ if (generatedPrintDataPageCount < pageCount) {
|
|
398
|
+ // 计算本次要生成的数据长度,以免超过总页数
|
|
399
|
+ int commitDataLength = Math.min((pageCount - generatedPrintDataPageCount), bufferSize);
|
|
400
|
+ // 生成数据
|
|
401
|
+ generateMultiPagePrintData(textList,generatedPrintDataPageCount, generatedPrintDataPageCount + commitDataLength, jsonList, infoList, quantity);
|
|
402
|
+ // 提交打印数据
|
|
403
|
+ PrintUtil.getInstance().commitData(jsonList.subList(generatedPrintDataPageCount, generatedPrintDataPageCount + commitDataLength), infoList.subList(generatedPrintDataPageCount, generatedPrintDataPageCount + commitDataLength));
|
|
404
|
+ // 更新已生成的打印数据页数
|
|
405
|
+ generatedPrintDataPageCount += commitDataLength;
|
|
406
|
+ }
|
|
407
|
+ }
|
|
408
|
+
|
|
409
|
+ /**
|
|
410
|
+ * 生成多页的打印数据。
|
|
411
|
+ *
|
|
412
|
+ * @param index 起始索引,生成数据的起始页。
|
|
413
|
+ * @param cycleIndex 结束索引,生成数据的结束页。
|
|
414
|
+ */
|
|
415
|
+ private static void generateMultiPagePrintData(ArrayList<String> textList, int index, int cycleIndex, ArrayList<String> jsonList, ArrayList<String> infoList, int quantity) {
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+ while (index < cycleIndex) {
|
|
419
|
+
|
|
420
|
+ // 设置打印参数
|
|
421
|
+ float width = 70;
|
|
422
|
+ float height = 72.5F;
|
|
423
|
+ int orientation = 0;
|
|
424
|
+ float marginX = 2.0F;
|
|
425
|
+ float marginY = 10.0F;
|
|
426
|
+ //矩形框类型
|
|
427
|
+ float rectangleWidth = width - marginX * 2;
|
|
428
|
+ float rectangleHeight = height - marginY * 2;
|
|
429
|
+ float lineWidth = 0.5F;
|
|
430
|
+ //1.圆 2.椭圆 3.矩形 4.圆角矩形
|
|
431
|
+ int graphType = 3;
|
|
432
|
+ float lineHeight = rectangleHeight / 6.0F;
|
|
433
|
+// float titleWidth = rectangleWidth * 2 / 5.0F;
|
|
434
|
+// float contentWidth = rectangleWidth * 3 / 5.0F;
|
|
435
|
+
|
|
436
|
+ float titleWidth = rectangleWidth / 2.0F;
|
|
437
|
+ float contentWidth = rectangleWidth / 2.0F;
|
|
438
|
+
|
|
439
|
+ float fontSize = 3.0F;
|
|
440
|
+ // 设置初始偏移量
|
|
441
|
+ float offsetY = 0F;
|
|
442
|
+ float offsetX = 0F;
|
|
443
|
+ // 计算绘制线条的 y 坐标
|
|
444
|
+ float secondLineY = marginY + lineHeight * 2 - lineWidth + offsetY;
|
|
445
|
+ float thirdLineY = marginY + lineHeight * 3 - lineWidth + offsetY;
|
|
446
|
+ float fourthLineY = marginY + lineHeight * 4 - lineWidth + offsetY;
|
|
447
|
+ float fiveLineY = marginY + lineHeight * 5 - lineWidth + offsetY;
|
|
448
|
+ List<String> fonts = new ArrayList<>();
|
|
449
|
+ fonts.add("ZT008.ttf");
|
|
450
|
+ // 设置画布大小
|
|
451
|
+ PrintUtil.getInstance().drawEmptyLabel(width, height, orientation, fonts);
|
|
452
|
+
|
|
453
|
+ int textAlignHorizontal = 0; // 左对齐
|
|
454
|
+ // 绘制小标题
|
|
455
|
+ PrintUtil.getInstance().drawLabelText(marginX * 2.5f + offsetX, marginY + offsetY, titleWidth - marginX * 3, lineHeight, textList.get(0), "宋体", fontSize, 0, textAlignHorizontal, 1, 6, 0, 1, new boolean[]{false, false, false, false});
|
|
456
|
+ PrintUtil.getInstance().drawLabelText(marginX * 2.5f + titleWidth + offsetX, marginY + offsetY, contentWidth - marginX * 3, lineHeight, textList.get(1), "宋体", fontSize, 0, 0, 1, 6, 0, 1, new boolean[]{false, false, false, false});
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+ PrintUtil.getInstance().drawLabelText(marginX * 2.5f + offsetX, marginY + lineHeight - lineWidth + offsetY, rectangleWidth - marginX * 3, lineHeight, textList.get(2), "宋体", fontSize, 0, textAlignHorizontal, 1, 6, 0, 1, new boolean[]{false, false, false, false});
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+ PrintUtil.getInstance().drawLabelText(marginX * 2.5f + offsetX, secondLineY, rectangleWidth - marginX * 3, lineHeight, textList.get(3), "宋体", fontSize, 0, textAlignHorizontal, 1, 6, 0, 1, new boolean[]{false, false, false, false});
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+ PrintUtil.getInstance().drawLabelText(marginX * 2.5f + offsetX, thirdLineY, titleWidth - marginX * 3, lineHeight, textList.get(4), "宋体", fontSize, 0, textAlignHorizontal, 1, 6, 0, 1, new boolean[]{false, false, false, false});
|
|
466
|
+ PrintUtil.getInstance().drawLabelText(marginX * 2.5f + titleWidth + offsetX, thirdLineY, contentWidth - marginX * 3, lineHeight, textList.get(5), "宋体", fontSize, 0, 0, 1, 6, 0, 1, new boolean[]{false, false, false, false});
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+ PrintUtil.getInstance().drawLabelText(marginX * 2.5f + offsetX, fourthLineY, titleWidth - marginX * 3, lineHeight, textList.get(6), "宋体", fontSize, 0, textAlignHorizontal, 1, 6, 0, 1, new boolean[]{false, false, false, false});
|
|
470
|
+ PrintUtil.getInstance().drawLabelText(marginX * 2.5f + titleWidth + offsetX, fourthLineY, contentWidth - marginX * 3, lineHeight, textList.get(7), "宋体", fontSize, 0, 0, 1, 6, 0, 1, new boolean[]{false, false, false, false});
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+ PrintUtil.getInstance().drawLabelText(marginX * 2.5f + offsetX, fiveLineY, titleWidth - marginX * 3, lineHeight, textList.get(8), "宋体", fontSize, 0, textAlignHorizontal, 1, 6, 0, 1, new boolean[]{false, false, false, false});
|
|
474
|
+ PrintUtil.getInstance().drawLabelText(marginX * 2.5f + titleWidth + offsetX, fiveLineY, contentWidth - marginX * 3, lineHeight, textList.get(9), "宋体", fontSize, 0, 0, 1, 6, 0, 1, new boolean[]{false, false, false, false});
|
|
475
|
+
|
|
476
|
+ //生成打印数据
|
|
477
|
+ byte[] jsonByte = PrintUtil.getInstance().generateLabelJson();
|
|
478
|
+
|
|
479
|
+ //转换为jsonStr
|
|
480
|
+ String jsonStr = new String(jsonByte);
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+ jsonList.add(jsonStr);
|
|
484
|
+ //除B32/Z401/T8的printMultiple为11.81,其他的为8
|
|
485
|
+ String jsonInfo = "{ " + "\"printerImageProcessingInfo\": " + "{ " + "\"orientation\":" + orientation + "," + " \"margin\": [ 0, 0, 0, 0 ], " + " \"printQuantity\": " + quantity + ", " + " \"horizontalOffset\": 0, " + " \"verticalOffset\": 0, " + " \"width\":" + width + "," + " \"height\":" + height + "," + "\"printMultiple\":" + 8 + "," + " \"epc\": \"\" }}";
|
|
486
|
+ infoList.add(jsonInfo);
|
|
487
|
+
|
|
488
|
+ index++;
|
|
489
|
+ }
|
|
490
|
+ }
|
|
491
|
+
|
300
|
492
|
}
|