文章目录
- 【Android】Android 集成佳博80打印机打印票据
- 1、集成佳博80打印机依赖
- 2、规范调用接口
- 3、使用到的相关对象以及工具类
- 4、MainActivity初始化接口
- 5、Uniapp调用方法
- 技术分享区
【Android】Android 集成佳博80打印机打印票据
1、集成佳博80打印机依赖
相关依赖在佳博的技术支持下载,然后导入到项目即可
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.7'compile files('libs/gprintersdkv1.jar')
2、规范调用接口
个人习惯,先写接口,再写功能实现类
import java.util.List;/*** 佳博网口打印机** @author Tellsea* @date 2021-8-11*/
public interface NetworkPrintingService {/*** 制作单** @param param*/void makeFoodOrderPrinting(String param);}
实现接口
import android.webkit.JavascriptInterface;
import android.widget.Toast;import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gprinter.command.EscCommand;
import com.gprinter.io.EthernetPort;
import com.zhhy.spotorder.app.cashier.MainActivity;
import com.zhhy.spotorder.app.cashier.entity.BizStoreNetwork;
import com.zhhy.spotorder.app.cashier.entity.OrderPrinting;
import com.zhhy.spotorder.app.cashier.entity.OrderPrintingDetail;
import com.zhhy.spotorder.app.cashier.service.NetworkPrintingService;
import com.zhhy.spotorder.app.cashier.utils.IntegerUtils;import org.apache.commons.lang3.StringUtils;import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;/*** 佳博网口打印机** @author Tellsea* @date 2021-8-11*/
public class NetworkPrintingServiceImpl implements NetworkPrintingService {private static Map<String, EthernetPort> ethernetPortMap = new HashMap<>();/*** 制作单** @param param*/@Override@JavascriptInterfacepublic void makeFoodOrderPrinting(String param) {System.out.println("制作单参数:" + param);if (param.isEmpty()) {Toast.makeText(MainActivity.CONTEXT, "打印参数不能为空", Toast.LENGTH_LONG).show();return;}Map<String, String> paramMap = new Gson().fromJson(param, new TypeToken<Map<String, String>>() {}.getType());for (Map.Entry<String, String> entry : paramMap.entrySet()) {BizStoreNetwork storeNetwork = new Gson().fromJson(entry.getKey(), BizStoreNetwork.class);OrderPrinting entity = new Gson().fromJson(entry.getValue(), OrderPrinting.class);if (entity.getOrderDetailList().isEmpty()) {Toast.makeText(MainActivity.CONTEXT, "佳博打印机:商品为空", Toast.LENGTH_LONG).show();continue;}EthernetPort ethernetPort = ethernetPortConn(storeNetwork.getPrinterIp(), storeNetwork.getPrinterPort());if (ethernetPort == null) {continue;}EscCommand esc = new EscCommand();esc.addInitializePrinter();// 大文字esc.addSetKanjiFontMode(EscCommand.ENABLE.ON, EscCommand.ENABLE.ON, EscCommand.ENABLE.OFF);// 大数字esc.addSelectPrintModes(EscCommand.FONT.FONTA, EscCommand.ENABLE.OFF, EscCommand.ENABLE.ON, EscCommand.ENABLE.ON, EscCommand.ENABLE.OFF);esc.addSelectJustification(EscCommand.JUSTIFICATION.CENTER);esc.addText("制作单 " + entity.getTakeOrderNumber() + "\n");esc.addText(entity.getStoreName() + "\n");esc.addPrintAndLineFeed();esc.addSelectJustification(EscCommand.JUSTIFICATION.LEFT);esc.addText("------------------------\n");esc.addText("桌台号:" + entity.getDeskName() + "\n");esc.addText("桌台类型:" + entity.getDeskType() + "\n");esc.addText("订单号:\n");esc.addText(entity.getOrderNumber() + "\n");esc.addText("------------------------\n");esc.addPrintAndLineFeed();esc.addText("菜名");esc.addSetHorAndVerMotionUnits((byte) 7, (byte) 0);esc.addSetAbsolutePrintPosition((short) 13);esc.addText("数量");esc.addSetHorAndVerMotionUnits((byte) 10, (byte) 0);esc.addSetAbsolutePrintPosition((short) 26);esc.addText("单价");esc.addPrintAndLineFeed();esc.addText("------------------------\n");for (OrderPrintingDetail item : entity.getOrderDetailList()) {esc.addText(item.getSpuTitle());esc.addSetHorAndVerMotionUnits((byte) 7, (byte) 0);esc.addSetAbsolutePrintPosition((short) 13);esc.addText(String.valueOf(item.getNumber()));esc.addSetHorAndVerMotionUnits((byte) 10, (byte) 0);esc.addSetAbsolutePrintPosition((short) 26);esc.addText(String.valueOf(item.getSpuSellPrice()));esc.addPrintAndLineFeed();String content = "";if (IntegerUtils.eq(item.getSpuAttrType(), 2) && StringUtils.isNotEmpty(item.getSpuDescription())) {content += item.getSpuDescription();}if (StringUtils.isNotEmpty(item.getSpuRemark())) {if (StringUtils.isEmpty(content)) {content += ",";}content += item.getSpuRemark();}if (StringUtils.isNotEmpty(content)) {esc.addText(content);esc.addPrintAndLineFeed();}}esc.addText("------------------------\n");esc.addText("总计:" + entity.getPayMoney() + "\n");esc.addText("------------------------\n");esc.addPrintAndFeedLines((byte) 6);esc.addCutPaper();Vector<Byte> data = esc.getCommand();try {ethernetPort.writeDataImmediately(data);} catch (IOException e) {e.printStackTrace();}}}/*** 以太网连接** @param ip 连接 wifi 的 ip 地址* @param port 连接 wifi 的端口号*/public EthernetPort ethernetPortConn(String ip, int port) {if (ethernetPortMap.get(ip) == null) {// 第一次连接EthernetPort ethernetPort = new EthernetPort(ip, port);if (ethernetPort.openPort()) {Toast.makeText(MainActivity.CONTEXT, "以太网连接打印机成功", Toast.LENGTH_SHORT).show();ethernetPortMap.put(ip, ethernetPort);return ethernetPort;}Toast.makeText(MainActivity.CONTEXT, "以太网连接打印机失败:IP:" + ip + ",端口:" + port, Toast.LENGTH_SHORT).show();return null;}// 已经连接成功if (ethernetPortMap.containsKey(ip)) {return ethernetPortMap.get(ip);}ethernetPortMap.remove(ip);Toast.makeText(MainActivity.CONTEXT, "以太网连接打印机失败:IP:" + ip + ",端口:" + port, Toast.LENGTH_SHORT).show();return null;}
}
3、使用到的相关对象以及工具类
订单对象
/*** 预结账单** @author Tellsea* @date 2021/07/22*/
@Data
@Accessors(chain = true)
public class OrderPrinting {private Long id;private String storeName;private String takeOrderNumber;private String deskName;private String deskType;private String orderNumber;private String serialNumber;private List<OrderPrintingDetail> orderDetailList;private BigDecimal originPrice;private BigDecimal reducePrice;private BigDecimal integerPrice;private String payWayName;private BigDecimal payMoney;private String placeOrderTime;private String payTime;private String orderRemark;
}
订单商品对象
/*** 商品信息** @author Tellsea* @date 2021/07/22*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class OrderPrintingDetail {private Long spuAttrType;private String spuTitle;private String spuDescription;private BigDecimal number;private BigDecimal spuSellPrice;private String spuRemark;}
打印机参数
import lombok.Data;
import lombok.experimental.Accessors;/*** 网口打印机** @author Tellsea* @date 2021-8-11*/
@Data
@Accessors(chain = true)
public class BizStoreNetwork {/*** 打印机IP*/private String printerIp;/*** 打印机端口*/private int printerPort;/*** 连接允许超时时间*/private int timeout = 1000;
}
使用到的比较工具类
/*** Integer 类型工具类* 原符号 < <= > >= <>* 对应函数 lt() le() gt() ge() ne()** @author Tellsea* @date 2021/04/12*/
public class IntegerUtils {/*** 等于** @param val1* @param val2* @return*/public static boolean eq(Integer val1, Integer val2) {if (val1 != null && val2 != null) {return val1.intValue() == val2.intValue();}return false;}public static boolean eq(Long val1, Integer val2) {if (val1 != null && val2 != null) {return val1.intValue() == val2.intValue();}return false;}/*** 大于** @param val1* @param val2* @return*/public static boolean gt(Integer val1, Integer val2) {if (val1 != null && val2 != null) {return val1.intValue() > val2.intValue();}return false;}public static boolean gt(Long val1, Integer val2) {if (val1 != null && val2 != null) {return val1.intValue() > val2.intValue();}return false;}/*** 小于** @param val1* @param val2* @return*/public static boolean lt(Integer val1, Integer val2) {if (val1 != null && val2 != null) {return val1.intValue() < val2.intValue();}return false;}/*** 大于等于** @param val1* @param val2* @return*/public static boolean ge(Integer val1, Integer val2) {if (val1 != null && val2 != null) {return (val1.intValue() > val2.intValue()) || (val1.intValue() == val2.intValue());}return false;}/*** 小于等于** @param val1* @param val2* @return*/public static boolean le(Integer val1, Integer val2) {if (val1 != null && val2 != null) {return (val1.intValue() < val2.intValue()) || (val1.intValue() == val2.intValue());}return false;}/*** 不等于** @param val1* @param val2* @return*/public static boolean ne(Integer val1, Integer val2) {if (val1 != null && val2 != null) {return val1.intValue() != val2.intValue();}return false;}
}
4、MainActivity初始化接口
// 佳博打印机初始化
NetworkPrintingService networkPrintingService = new NetworkPrintingServiceImpl();
5、Uniapp调用方法
在接口方法已经使用了@JavascriptInterface注解,可以使用JavaScript直接调用
/*** 制作单*/
function makeFoodNetwork(param) {window.networkPrinting.makeFoodOrderPrinting(param);
}