Android连接USB打印机
发布日期:2021-11-09 22:50:29 浏览次数:5 分类:技术文章

本文共 4259 字,大约阅读时间需要 14 分钟。

项目中需要用到android设备通过USB连接热敏打印机打印图文混排的小条,总结一下。

外部主要是用这个方法来打印:

/**     * 打印核验小条     * 子线程中运行     */    private static int times = 0;    public void printVerifyData(String tmpName,                                String tmpIdNo,                                String curName,                                String idcardNo,                                String headerPic) {        while(!isConnect){            isConnect = doConnect();            times ++ ;            Log.d("lixm","isConnect = " + isConnect + ", times = " + times);            if(times >= TRY_COUNT){                // todo send error                sendPrintError();                return;            }            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }        }        ArrayList
data = new ArrayList<>(); // 用于装载打印数据 try { PrinterWriter printer = new PrinterWriter58mm(); // printer.init(); // 初始化打印机 printer.setAlignCenter(); data.add(printer.getDataAndReset()); // 打印标题 printer.setEmphasizedOn(); printer.setFontSize(1); printer.print(Const.VALUE.TITLE); printer.printLineFeed(); printer.setFontSize(0); printer.setEmphasizedOff(); // 打印虚线 printer.setAlignCenter(); printer.printDottedLine(); printer.printLineFeed(); data.add(printer.getDataAndReset()); // 打印了一个图片 printer.setAlignCenter(); ArrayList
image1 = printer.getImageByte(headerPic); if(image1 == null || image1.size() == 0){ ToastUtils.showShort("照片转换异常,请重新打印!"); sendPrintError(); return; } data.addAll(image1); // 打印虚线 printer.printDottedLine(); printer.printLineFeed(); // 输出并换行 printer.setLineHeight(80); // 设置行间距 printer.setEmphasizedOn(); printer.printInOneLine("姓名:", tmpName, 0); printer.printLineFeed(); printer.printInOneLine("身份证号码:", tmpIdNo, 0); printer.printLineFeed(); printer.printInOneLine("凭证有效期:", TimeUtils.date2String(new Date(),new SimpleDateFormat("yyyy年MM月dd日")), 0); printer.printLineFeed(); printer.setEmphasizedOff(); // 打印虚线 printer.setAlignCenter(); printer.printDottedLine(); printer.printLineFeed(); // 输出并换行 data.add(printer.getDataAndReset()); // todo 这里打印二维码 String oriString = curName+"|" +idcardNo; String qrString = Base64.encodeToString(oriString.getBytes("UTF-8"),Base64.NO_WRAP); Log.d("lixm","qrString = " + qrString); Bitmap qrCodeImage = QRCodeUtil.createQRCodeBitmap(qrString, 300, "0"); //byte[] qrData = PrinterUtils.decodeBitmap(qrCodeImage); ArrayList
qrData = printer.getImageByte(qrCodeImage); data.addAll(qrData); // 打印虚线 printer.setAlignCenter(); printer.printDottedLine(); printer.printLineFeed(); // 输出并换行 printer.setAlignLeft(); printer.setEmphasizedOn(); String tips1 = " 提示:凭二维码扫码进站,仅限本人使用,"+ "\n"; String tips2 = " 如遇遗失请重新补打凭证"+"\n"; printer.print(tips1); printer.print(tips2); printer.setEmphasizedOff(); printer.printLineFeed(); printer.printLineFeed(); printer.feedPaperCutPartial(); // 切纸 data.add(printer.getDataAndClose()); boolean isPrintResult = printTest(data); // 打印数据 //printer.close(); //printer = null; if(isPrintResult){ BusEvent busEvent = new BusEvent(); busEvent.action = "printer_completed"; busEvent.data = "ok"; EventBus.getDefault().post(busEvent); }else{ sendPrintError(); } } catch (IOException e) { LogUtils.file(e.getCause() + "--" + e.getMessage()); sendPrintError(); } }

完整代码请点击

转载地址:https://blog.csdn.net/dami_lixm/article/details/98470628 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:android自定义圆角按钮,点击实现背景和文字颜色同时变化
下一篇:android通过USB读取身份证信息

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月04日 20时50分15秒