在收銀系統中經常使用到打印小票的功能。本文將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();
}
}