Java实现安卓连接商米POS收银机打印小票功能
在收银系统中经常使用到打印小票的功能。本文将Java如何实现商米POS收银机打印小票的功能。包括“”定义管理打印相关方法的类,封装好方法供外部调用”、“调用打印功能示例”。
1、定义管理打印相关方法的类,封装好方法供外部调用。
AidlUtil类封装了打印的方法。
创建打印服务的对象:
private ServiceConnection connService = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {woyouService = IWoyouService.Stub.asInterface(service);}@Overridepublic void onServiceDisconnected(ComponentName name) {woyouService = null;}
};
连接打印服务:
/*** 连接服务*/
@TargetApi(Build.VERSION_CODES.DONUT)
public void connectPrinterService(Context context) {this.context = context.getApplicationContext();Intent intent = new Intent();intent.setPackage(SERVICE_PACKAGE);intent.setAction(SERVICE_ACTION);context.getApplicationContext().startService(intent);context.getApplicationContext().bindService(intent, connService, Context.BIND_AUTO_CREATE);
}
断开打印服务:
/*
断开服务*/
public void disconnectPrinterService(Context context) {if (woyouService != null) {context.getApplicationContext().unbindService(connService);woyouService = null;}
}
设置打印浓度:
/*** 设置打印浓度*/
private int[] darkness = new int[]{0x0600, 0x0500, 0x0400, 0x0300, 0x0200, 0x0100, 0, 0xffff, 0xfeff, 0xfdff, 0xfcff, 0xfbff, 0xfaff};public void setDarkness(int index) {if (woyouService == null) {Toast.makeText(context, "The service has been disconnected!", Toast.LENGTH_SHORT).show();return;}int k = darkness[index];try {woyouService.sendRAWData(ESCUtil.setPrinterDarkness(k), null);woyouService.printerSelfChecking(null);} catch (RemoteException e) {e.printStackTrace();}
}
初始化打印机:
/*** 初始化打印机*/
public void initPrinter() {if (woyouService == null) {Toast.makeText(context, "The service has been disconnected!", Toast.LENGTH_SHORT).show();return;}try {woyouService.printerInit(null);} catch (RemoteException e) {e.printStackTrace();}
}
打印文字:
/*** 打印文字,,,,设置打印内容,字体大小,是否加粗,居中(靠左靠右),下划线*/public void printText(String content, float size, boolean isBold, int gravity, boolean isUnderLine) {//Gravity.LEFT==51,,Gravity.CENTER==17,,Gravity.RIGHT==53if (woyouService == null) {Toast.makeText(context, "The service has been disconnected!", Toast.LENGTH_SHORT).show();return;}try {if (isBold) {woyouService.sendRAWData(ESCUtil.boldOn(), null);} else {woyouService.sendRAWData(ESCUtil.boldOff(), null);}if (gravity == 51) {woyouService.setAlignment(0, null);} else if (gravity == 17) {woyouService.setAlignment(1, null);} else if (gravity == 53) {woyouService.setAlignment(2, null);}if (isUnderLine) {woyouService.sendRAWData(ESCUtil.underlineWithOneDotWidthOn(), null);} else {woyouService.sendRAWData(ESCUtil.underlineOff(), null);}woyouService.printTextWithFont(content, null, size, null);woyouService.lineWrap(1, null);
// woyouService.cutPaper(null);//切纸动作,如果打印机没有切刀,不会执行该动作} catch (RemoteException e) {e.printStackTrace();}}
打印图片:
/*** 打印图片*/
public void printBitmap(Bitmap bitmap) {if (woyouService == null) {Toast.makeText(context, "The service has been disconnected", Toast.LENGTH_SHORT).show();return;}try {woyouService.setAlignment(1, null);woyouService.printBitmap(bitmap, null);woyouService.lineWrap(1, null);} catch (RemoteException e) {e.printStackTrace();}
}
打印分割线:
//打印分割线public void printDivider(String delimiterToRepeat) {float fontSize = 20;int fontCenter = 17;delimiterToRepeat = delimiterToRepeat.equals("") ? " " : delimiterToRepeat;int delimiterToRepeatLength = GeneralUtil.string_length(delimiterToRepeat); // 打印时,中文是2字节,英文是1字节int aLineBytes; // 一行的字节if (getPrinterInfo().get(1).contains("T1mini") || getPrinterInfo().get(1).contains("T2mini")) {aLineBytes = 770;} else if (getPrinterInfo().get(1).contains("T1") || getPrinterInfo().get(1).contains("T2")) {//这是T2机用中文测出来的,15(fontSize)*38(个)*2=1140,不能大于1150(可等于),中文占2个字节。aLineBytes = 1150;} else {aLineBytes = 0; // ... 暂时不知道还有其他什么型号}// 字符串拼接String printText = "";for (int j = 1; j <= aLineBytes / (delimiterToRepeatLength * fontSize); j++) {printText = printText.concat(delimiterToRepeat);}printText(printText, fontSize, true, fontCenter, false);
// woyouService.lineWrap(1, null); // 太浪费纸了}
进行切刀:
//切刀
public void cutPaper() throws RemoteException {woyouService.cutPaper(null);//切纸动作,如果打印机没有切刀,不会执行该动作
}
走纸:
//走纸
public void linewrap(int num) {try {woyouService.lineWrap(num, null);} catch (RemoteException e) {e.printStackTrace();}
}
打印表格:
public void printTable(String[] colsTextArr, int[] colsWidthArr, int[] colsAlign) {try {woyouService.printColumnsString(colsTextArr, colsWidthArr, colsAlign, null);
// linewrap(1); // 太浪费纸了} catch (RemoteException e) {e.printStackTrace();}}
2、调用打印功能示例。
private void printRetailTradeAggregation() {//打印跨天上班汇总小票try {AidlUtil.getInstance().printText("交班确认表", 35, false, 17, false);AidlUtil.getInstance().printText("注意:此交班小票为跨日期上班系统自动生成,请妥善保管以便对账。", 20, false, 17, false);AidlUtil.getInstance().printDivider("-");AidlUtil.getInstance().printText("上班时间:" + new SimpleDateFormat(Constants.DATE_FORMAT_Default).format(BaseActivity.retailTradeAggregation.getWorkTimeStart()), 25, false, 51, false);AidlUtil.getInstance().printText("下班时间:" + new SimpleDateFormat(Constants.DATE_FORMAT_Default).format(BaseActivity.retailTradeAggregation.getWorkTimeEnd()), 25, false, 51, false);AidlUtil.getInstance().printText("交班人:" + Constants.getCurrentStaff().getName(), 30, false, 51, false);AidlUtil.getInstance().linewrap(1);AidlUtil.getInstance().printText("------收银汇总------", 35, false, 17, false);AidlUtil.getInstance().printText("交易单数:" + BaseActivity.retailTradeAggregation.getTradeNO(), 30, false, 51, false);AidlUtil.getInstance().printText("营业额:" + BaseActivity.retailTradeAggregation.getAmount(), 30, false, 51, false);AidlUtil.getInstance().printText("准备金:" + BaseActivity.retailTradeAggregation.getReserveAmount(), 30, false, 51, false);AidlUtil.getInstance().printText("现金收入:" + BaseActivity.retailTradeAggregation.getCashAmount(), 30, false, 51, false);AidlUtil.getInstance().printText("微信收入:" + BaseActivity.retailTradeAggregation.getWechatAmount(), 30, false, 51, false);
// AidlUtil.getInstance().printText("支付宝收入:" + showAlipayAmount.getText().toString(), 30, false, 51, false);AidlUtil.getInstance().printDivider("-");AidlUtil.getInstance().printText("交班人签名:", 30, false, 51, false);AidlUtil.getInstance().linewrap(8);AidlUtil.getInstance().cutPaper();} catch (RemoteException e) {e.printStackTrace();}}
Java实现安卓连接商米POS收银机打印小票功能相关推荐
- android 连接商米POSV1内置打印机
最近接触连接打印机的比较多,就写下来吧 连接商米POSV1的打印机: 商米官网上有开发文档,具体可以看下,我只写下步骤: (1)因为我用的AIDL的方法,所以先把这3个文件放入到项目中 (2)创建线程 ...
- 收银机打印数据截取_商米智能化收银机,教你轻松把店开起来
现在的很多年轻人,厌倦了每天上下班的生活,总想着有一天可以开一家属于自己的店铺,在家也能轻轻松松赚大钱,但是一家店铺顺利开起来,除了店铺装修.设备上费点力气,一些开店利器的选择上也不能马虎.就拿收银机 ...
- 重复造轮子系列——基于FastReport设计打印模板实现桌面端WPF套打和商超POS高度自适应小票打印...
重复造轮子系列--基于FastReport设计打印模板实现桌面端WPF套打和商超POS高度自适应小票打印 一.引言 桌面端系统经常需要对接各种硬件设备,比如扫描器.读卡器.打印机等. 这里介绍下桌面端 ...
- 商米设备开发之-打印(适配大部分商米设备)
今天这里讲的是商米内置打印设备,不涉及商米云打印.,内置需要根据机型进行区分,大致分两种,这里给出区分不同商米不同机型的代码: 获取品牌代码:(商米的品牌为SUNMI) // public stati ...
- iOS 连接打印机 ESC/POS 指令打印 打印图片二维码
最近公司给商户做的App 允许App把卖出的商品信息通过打印机 打印标签 所以了解了一下iOS 和 打印机 之间的交互 (Ps:用的不是UIPrinter 那个扫面打印机 发送信息打印的那个框架) 主 ...
- java 小票打印_java 调收银机打印小票
public class MyPrint implementsPrintable{static Logger log = Logger.getLogger(MyPrint.class);//业务类 U ...
- java 开发安卓im_Android端IM应用中的@人功能实现:仿微博、QQ、微信,零入侵、高可扩展...
本文由"猫爸iYao"原创分享,感谢作者. 1.引言 最近有个需求:评论@人(没错,就是IM聊天或者微博APP里的@人功能),就像下图这样: ▲ 微信群聊界面里的@人功能 ▲ QQ ...
- java 打印 小票_java 调收银机打印小票
public class MyPrint implementsPrintable{static Logger log = Logger.getLogger(MyPrint.class);//业务类 U ...
- java web 打印pos小票_JS+调用word打印功能实现在Webfrom客户端pos机打印小票(58x210mm)...
本文主要解决在web网页上通过点击某个按钮现实打印小票的功能.修改于2015.8.15. 页面html代码: 调用Word打印机打印 function doPrint() { viewToWord(& ...
最新文章
- 使用pinchzoom实现头像剪裁
- PHP之factory
- QT操作sqlite数据库汇总
- DBA_Oracle Table Partition表分区概念汇总(概念)
- java自动获取ip_java自动获取电脑ip和MAC地址
- 04_mysql增删改操作
- Python椭圆加密算法实现区块链信息认证
- kafka 不同分区文件存储_Kafka 系列(二)文件存储机制与Producer架构原理怎样保证数据可靠性??...
- MySQL索引原理及慢查询优化,了解一下?
- 蓝桥杯新增web应用开发科目—送给想要参赛的小伙伴们一份备赛指南
- 树莓派Python3 使用定时器
- 白名单模板_亚马逊白名单申请流程全解析
- 【2】输入俩个数m,n,字符串st1为1-m组成,输出字符串的倒数第n个字符
- 双路服务器单路运行,什么叫双路服务器?与PC机、单路机有什么区别?
- Java常用工具类-根据物流单号,从快递100中获取物流详细信息,包含发货,签收等
- 大量数据导出Excel 之 多重影分身之术
- 一起来找:程序员必去的社区与网站
- 跨境电商支付方式之如何玩转跨境支付
- MS office二级错题记录【3】
- 复旦大学和中科大 计算机,强基计划遇冷?!复旦大学和中科大都没招满...
热门文章
- raid5数据丢失后应该怎么做才能提高数据恢复成功率?
- 解读下一代网络:算力网络正从理想照进现实
- 【lssvm预测】基于飞蛾扑火算法改进的最小二乘支持向量机lssvm预测
- (he)的平方等于she
- 如何用php下载文件?
- 2021年G1工业锅炉司炉多少分及格及G1工业锅炉司炉模拟考试题
- 视频去水印软件哪个好用?
- 有道云笔记桌面挂件android,有道云笔记网页版全面更新!更有Android,pc新版享不停!...
- 封闭类(Sealed Classes)
- java 封闭实例_java – 每个内部类都需要一个封闭的实例是真的吗?