Android蓝牙打印格式排版

来源:互联网 发布:mysql qps 和 tps 编辑:程序博客网 时间:2024/05/16 19:20
最近由于项目需求,在做蓝牙打印的功能。Android连接蓝牙打印机网上满天飞,但是内容的排版却很少。

我也是研究了下,自己封装了一个工具类希望可以方便开发。

这个类叫 BluetoothPrintFormatUtil ,无需考虑敏感字符及E文

例如:要排版如下格式












在Android代码中写入如下代码

StringBuffer sb = new StringBuffer();        //排版标题sb.append(BluetoothPrintFormatUtil.printTitle("订餐单\n"))  .append("日期:2013-06-25\n");//排版People信息LinkedHashMap<String, String> leftMsgMap = new LinkedHashMap<String, String>();LinkedHashMap<String, String> rightMsgMap = new LinkedHashMap<String, String>();leftMsgMap.put("姓名", "Aaron_lin");leftMsgMap.put("病区", "11脑外科");leftMsgMap.put("床号", "123");leftMsgMap.put("性别", "女");rightMsgMap.put("住院号", "2112\n");rightMsgMap.put("年龄", "22\n");rightMsgMap.put("身高", "175cm\n");rightMsgMap.put("BMI", "22.0\n");sb.append(BluetoothPrintFormatUtil.printSymmetryMSG(leftMsgMap, rightMsgMap));//排版点餐菜单信息必须以 '$' 分隔菜单格式是     菜谱名$数量$单价 LinkedHashMap<String, LinkedList<String>> menuMsgMap = new LinkedHashMap<String, LinkedList<String>>();LinkedList<String> menuList = new LinkedList<String>();menuList.add(" 红烧鸭块$2$22.0");menuList.add(" 水煮鱼片$1$25.0");menuMsgMap.put("早餐", menuList);menuList.clear();menuList.add(" 农家小炒肉$1$18.0");menuList.add(" 鱼香肉丝1$2$18.0");menuList.add(" 炒饭$1$18.0");menuMsgMap.put("午餐", menuList);menuList.clear();menuList.add(" 红烧鸡块$1$10.0");menuMsgMap.put("晚餐", menuList);sb.append(BluetoothPrintFormatUtil.printMenuMSG(menuMsgMap))  .append("总计:111.0\n")  .append("打印日期:2013-06-20\n\n\n");
System.out.println(sb.toString());

注意:单独运行在Java平台下会不对齐,此处主要运行在Android下

目前只封装了这种排版格式,若有其他格式可以信息我。

BluetoothPrintFormatUtil.java 下载地址:http://download.csdn.net/detail/qq331710168/5647587



原创粉丝点击