| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- package io.flutter.plugins.utils;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.util.Log;
- import androidx.annotation.NonNull;
- import com.snbc.sdk.LabelPrinter;
- import java.io.ByteArrayOutputStream;
- import java.nio.ByteBuffer;
- import java.util.ArrayList;
- import java.util.List;
- import io.flutter.plugin.common.MethodChannel;
- public class BTPPrintUtil {
- /**
- * 单例实例,使用 volatile 保证多线程可见性和有序性
- */
- private static volatile LabelPrinter labelPrinter;
- private static int nLang = 2;
- /**
- * 获取 LabelPrinter 单例实例
- *
- * @return LabelPrinter 实例
- */
- public static LabelPrinter getInstance() {
- // 双重检查锁定以确保只初始化一次实例
- if (labelPrinter == null) {
- synchronized (BTPPrintUtil.class) {
- if (labelPrinter == null) {
- labelPrinter = new LabelPrinter();
- }
- }
- }
- return labelPrinter;
- }
- /**
- * 通过打印机mac地址进行蓝牙连接开启打印机(同步)
- *
- * @param address 打印机地址
- * @return 成功与否
- */
- public static int connectBluetoothPrinter(String address) {
- // return 1;
- // 获取单例实例以确保线程安全
- return getInstance().ConnectPrinter(7, address, 4);
- }
- public static void printBitMap(Bitmap bitmap, String deviceName, @NonNull MethodChannel.Result result) {
- int x = 0;
- if(deviceName.contains("BTP-UP321")) {
- x = 0;
- } else if(deviceName.contains("BTP-P398PLUS")) {
- x = 40;
- }
- int bytes = bitmap.getByteCount();
- ByteBuffer buf = ByteBuffer.allocate(bytes);
- bitmap.copyPixelsToBuffer(buf);
- ByteArrayOutputStream LionData=new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, LionData);
- getInstance().SetLabelSize(560,640);
- int code = getInstance().PrintImageData(x, 0, LionData.toByteArray());
- getInstance().PrintLabel(1,1);
- result.success(code == 0 ? true : false);
- }
- public static void printText(ArrayList<String> textList,@NonNull MethodChannel.Result result) {
- int width = 560;
- int height = 640;
- getInstance().SetLabelSize(width,height);
- int marginX = 2;
- int marginY = 40;
- //矩形框类型
- int rectangleWidth = width - marginX * 2;
- int rectangleHeight = height - marginY * 2;
- int lineWidth = 1;
- int lineHeight = rectangleHeight / 6;
- int titleWidth = rectangleWidth / 2;
- float fontSize = 3;
- // 设置初始偏移量
- int offsetY = 0;
- int offsetX = 0;
- // 计算绘制线条的 y 坐标
- int secondLineY = marginY + lineHeight * 2 - lineWidth + offsetY;
- int thirdLineY = marginY + lineHeight * 3 - lineWidth + offsetY;
- int fourthLineY = marginY + lineHeight * 4 - lineWidth + offsetY;
- int fiveLineY = marginY + lineHeight * 5 - lineWidth + offsetY;
- int textAlignHorizontal = 0; // 左对齐
- labelPrinter.PrintText(marginX * 2 + offsetX, marginY + offsetY, "3", textList.get(0), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, marginY + offsetY, "3", textList.get(1), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + offsetX, marginY + lineHeight - lineWidth + offsetY, "3", textList.get(2), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + offsetX, secondLineY, "3", textList.get(3), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + offsetX, thirdLineY, "3", textList.get(4), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, thirdLineY, "3", textList.get(5), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + offsetX, fourthLineY, "3", textList.get(6), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, fourthLineY, "3", textList.get(7), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + offsetX, fiveLineY, "3", textList.get(8), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, fiveLineY, "3", textList.get(9), 0, 7, 0, 0);
- int code = getInstance().PrintLabel(1,1);
- result.success(code == 0 ? true : false);
- }
- public static void printBarCodeWithText(String barCode, String text, ArrayList<String> textList,@NonNull MethodChannel.Result result) {
- getInstance().SetLabelSize(560,640*2);
- // 二维码
- labelPrinter.PrintBarcodeQR(90, 40, 0, barCode, 'M', 8, 2);
- //质量 品质 食安选项框
- labelPrinter.PrintRectangle(100, 440, 20, 20, 1);
- labelPrinter.PrintText(124, 440, "3", "质量", 0, 7, 0, 0);
- labelPrinter.PrintRectangle(220, 440, 20, 20, 1);
- labelPrinter.PrintText(244, 440, "3", "品质", 0, 7, 0, 0);
- labelPrinter.PrintRectangle(340, 440, 20, 20, 1);
- labelPrinter.PrintText(364, 440, "3", "食安", 0, 7, 0, 0);
- // 编号 labelPrinter.PrintText(100, 500, "8", "SHXM2024007404", 0, 7, 3, 0);
- labelPrinter.PrintText(120, 500, "5", text, 0, 0, 0, 0);
- //
- int width = 560;
- int height = 640;
- int marginX = 2;
- int marginY = 80;
- //矩形框类型
- int rectangleWidth = width - marginX * 2;
- int rectangleHeight = height - marginY * 2;
- int lineWidth = 1;
- int lineHeight = rectangleHeight / 6;
- int titleWidth = rectangleWidth / 2;
- float fontSize = 3;
- // 设置初始偏移量
- int offsetY = 680;
- int offsetX = 0;
- // 计算绘制线条的 y 坐标
- int secondLineY = marginY + lineHeight * 2 - lineWidth + offsetY;
- int thirdLineY = marginY + lineHeight * 3 - lineWidth + offsetY;
- int fourthLineY = marginY + lineHeight * 4 - lineWidth + offsetY;
- int fiveLineY = marginY + lineHeight * 5 - lineWidth + offsetY;
- int textAlignHorizontal = 0; // 左对齐
- labelPrinter.PrintText(marginX * 2 + offsetX, marginY + offsetY, "3", textList.get(0), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, marginY + offsetY, "3", textList.get(1), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + offsetX, marginY + lineHeight - lineWidth + offsetY, "3", textList.get(2), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + offsetX, secondLineY, "3", textList.get(3), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + offsetX, thirdLineY, "3", textList.get(4), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, thirdLineY, "3", textList.get(5), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + offsetX, fourthLineY, "3", textList.get(6), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, fourthLineY, "3", textList.get(7), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + offsetX, fiveLineY, "3", textList.get(8), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, fiveLineY, "3", textList.get(9), 0, 7, 0, 0);
- //
- int code = getInstance().PrintLabel(1,1);
- result.success(code == 0 ? true : false);
- }
- // BTP打印机同时打印图标和标签只能调这个,依次printBitMap再printText会导致偶发printText不走。
- public static void PrintBitMapAndText(Bitmap bitmap, String deviceName, ArrayList<String> textList,@NonNull MethodChannel.Result result) {
- int x = 0;
- if(deviceName.contains("BTP-UP321")) {
- x = 0;
- } else if(deviceName.contains("BTP-P398PLUS")) {
- x = 40;
- }
- int bytes = bitmap.getByteCount();
- ByteBuffer buf = ByteBuffer.allocate(bytes);
- bitmap.copyPixelsToBuffer(buf);
- ByteArrayOutputStream LionData=new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, LionData);
- getInstance().SetLabelSize(560,640*2);
- int code = getInstance().PrintImageData(x, 0, LionData.toByteArray());
- int width = 560;
- int height = 640;
- int marginX = 2;
- int marginY = 80;
- //矩形框类型
- int rectangleWidth = width - marginX * 2;
- int rectangleHeight = height - marginY * 2;
- int lineWidth = 1;
- int lineHeight = rectangleHeight / 6;
- int titleWidth = rectangleWidth / 2;
- float fontSize = 3;
- // 设置初始偏移量
- int offsetY = 680;
- int offsetX = 0;
- // 计算绘制线条的 y 坐标
- int secondLineY = marginY + lineHeight * 2 - lineWidth + offsetY;
- int thirdLineY = marginY + lineHeight * 3 - lineWidth + offsetY;
- int fourthLineY = marginY + lineHeight * 4 - lineWidth + offsetY;
- int fiveLineY = marginY + lineHeight * 5 - lineWidth + offsetY;
- int textAlignHorizontal = 0; // 左对齐
- labelPrinter.PrintText(marginX * 2 + offsetX, marginY + offsetY, "3", textList.get(0), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, marginY + offsetY, "3", textList.get(1), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + offsetX, marginY + lineHeight - lineWidth + offsetY, "3", textList.get(2), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + offsetX, secondLineY, "3", textList.get(3), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + offsetX, thirdLineY, "3", textList.get(4), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, thirdLineY, "3", textList.get(5), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + offsetX, fourthLineY, "3", textList.get(6), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, fourthLineY, "3", textList.get(7), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + offsetX, fiveLineY, "3", textList.get(8), 0, 7, 0, 0);
- labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, fiveLineY, "3", textList.get(9), 0, 7, 0, 0);
- getInstance().PrintLabel(1,1);
- result.success(code == 0 ? true : false);
- }
- }
|