Java实现安卓连接商米POS收银机打印小票功能

在收银系统中经常使用到打印小票的功能。本文将Java如何实现商米POS收银机打印小票的功能。包括“”定义管理打印相关方法的类,封装好方法供外部调用”、“调用打印功能示例”。

1、定义管理打印相关方法的类,封装好方法供外部调用。

AidlUtil类封装了打印的方法。

创建打印服务的对象:

private ServiceConnection connService = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        woyouService = IWoyouService.Stub.asInterface(service);
    }

    @Override
    public 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==53
        if (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();
        }
    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值