您的位置:首页 > 其它

百富POS机A920安卓系统打印功能的实现

2018-01-22 10:36 2967 查看
由于打印demo中只有一些参数设置和基础打印,也无法实现排版功能,后采用com.pax.gl.imgprocessing中的接口


IImgProcessing.IPage



生成bitmap实现打印,可支持打印排版,打印图片等。

1、重要的引入jar包,一开始只引入了第一个,然后是痛苦的bug排查。。。



2、根据API,添加打印功能,自己封装了一个打印工具类
package com.hisun.pos.utils.printer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.hisun.pos.MyApplication;
import com.hisun.pos.R;
import com.hisun.pos.utils.LogUtil;
import com.pax.gl.imgprocessing.IImgProcessing;
import com.pax.ipp.service.aidl.dal.printer.EFontTypeAscii;
import com.pax.ipp.service.aidl.dal.printer.EFontTypeExtCode;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
* Created by ylh on 2018/1/18 0018.
*/

public class PrinterUtil {

private static final int FONT_BIG = 24;
private static final int FONT_NORMAL = 20;
private static Context mContext;

public PrinterUtil(Context context) {
mContext = context;
}

public void print(String str, Boolean containBitmap) {
//初始化
PrinterTester.getInstance().init();
//字体设置(ASCII,拓展码)
PrinterTester.getInstance().fontSet(EFontTypeAscii.FONT_8_16, EFontTypeExtCode.FONT_16_16);
//间距设置(字间距,行间距)
PrinterTester.getInstance().spaceSet(Byte.parseByte("0"),
Byte.parseByte("0"));

//字符打印左边界
PrinterTester.getInstance().leftIndents(Short.parseShort("0"));
//打印黑度
PrinterTester.getInstance().setGray(1);
//双倍宽度<->正常宽度
if (true) {
PrinterTester.getInstance().setDoubleWidth(true, true);
}
//双倍高度<->正常高度
if (true) {
PrinterTester.getInstance().setDoubleHeight(true, true);
}
//正常打印<->反显打印
PrinterTester.getInstance().setInvert(false);
//打印图片
if (containBitmap) {
PrinterTester.getInstance().printBitmap(
BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_qrcode));
}
//打印状态
String status = PrinterTester.getInstance().getStatus();

//打印文本
PrinterTester.getInstance().printStr(str, null);
//走纸
PrinterTester.getInstance().step(150);

//开始打印
PrinterTester.getInstance().start();
}

public void print(String orderNo, String orderTime, String orderAmt) {
Bitmap bitmap = generate(orderNo, orderTime, orderAmt);
//初始化
PrinterTester.getInstance().init();
String statues = PrinterTester.getInstance().getStatus();
LogUtil.debug(statues);
if (statues.contains("Success")){
PrinterTester.getInstance().printBitmap(bitmap);
PrinterTester.getInstance().start();
}
}

public Bitmap generate(String orderNo, String orderTime, String orderAmt) {
IImgProcessing.IPage page = GetObj.getGL().getImgProcessing().createPage();
//设置字体
//        page.setTypeFace(TYPE_FACE);
//往页面里添加新的一行
//        page.addLine();
//创建一个元素,可用IImgProcessing.IPage.ILine.addUnit(IUnit)来生成行
page.adjustLineSpace(10);

//logo
page.addLine().addUnit(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_mpay));

// 凭单抬头
page.addLine().addUnit("
e50c
Payment Voucher", FONT_BIG, IImgProcessing.IPage.EAlign.CENTER, IImgProcessing.IPage.ILine.IUnit.TEXT_STYLE_BOLD);

page.addLine().adjustTopSpace(20);

// 门店名称
page.addLine().addUnit("Store:" + MyApplication.getLoginResp().getUserBasicInfo().getMercName(),
FONT_NORMAL);

// 商户id
page.addLine().addUnit("Merchant ID:" + MyApplication.getLoginResp().getUserBasicInfo().getUserId(), FONT_NORMAL);

// 操作人员
page.addLine().addUnit("Operator:" + MyApplication.getLoginResp().getUserBasicInfo().getDisplayNm(), FONT_NORMAL);

// Order:
page.addLine().addUnit("Order:" + orderNo, FONT_NORMAL);

// Payment_time:
page.addLine().addUnit("Payment_time:" + orderTime, FONT_NORMAL);

// Amount:
page.addLine().addUnit("Amount:$" + orderAmt, FONT_NORMAL);

// ===:
page.addLine().addUnit("==================================", FONT_NORMAL, IImgProcessing.IPage.EAlign.CENTER);

// Payment Successful:
page.addLine().addUnit("Payment Successful", FONT_BIG, IImgProcessing.IPage.EAlign.CENTER, IImgProcessing.IPage.ILine.IUnit.TEXT_STYLE_BOLD);

page.addLine().adjustTopSpace(20);

// Print Time:
page.addLine().addUnit("Print Time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), FONT_NORMAL);

// Terminal:
page.addLine().addUnit("Terminal:" + SysTester.getInstance().getTerminfoModel() + " " + SysTester.getInstance().getDevInterfaceVer(), FONT_NORMAL);

page.addLine().adjustTopSpace(20);

//二维码
page.addLine().addUnit(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_qrcode), IImgProcessing.IPage.EAlign.CENTER);
page.addLine().adjustTopSpace(2);
page.addLine().addUnit("Scan to download Mpay", FONT_NORMAL, IImgProcessing.IPage.EAlign.CENTER);

page.addLine().addUnit("\n\n\n\n\n\n", FONT_NORMAL);

IImgProcessing imgProcessing = GetObj.getGL().getImgProcessing();
return imgProcessing.pageToBitmap(page, 384);
}
}
调用打印方法
new Thread(new Runnable() {
@Override
public void run() {
PrinterUtil printer = new PrinterUtil(CashierChoosePaymentActivity.this);
printer.print("2018011800000000000345", "2018-01-19  11:27:52", "10.00");
}
}).start();
重要的获取 IPrinter printer
package com.hisun.pos.utils.printer;

import android.graphics.Bitmap;

import com.pax.ipp.service.aidl.Exceptions;
import com.pax.ipp.service.aidl.dal.printer.EFontTypeAscii;
import com.pax.ipp.service.aidl.dal.printer.EFontTypeExtCode;
import com.pax.ipp.service.aidl.dal.printer.Pic;
import com.pax.ippi.dal.interfaces.IPrinter;

public class PrinterTester extends TestLog {

private static PrinterTester printerTester;
private IPrinter printer;

private PrinterTester() {
printer = GetObj.getDal().getPrinter();
}

public static PrinterTester getInstance() {
if (printerTester == null) {
printerTester = new PrinterTester();
}
return printerTester;
}

public void init() {
try {
printer.init();
logTrue("init");
} catch (Exceptions e) {
e.printStackTrace();
logErr("init", e.toString());
}
}

public String getStatus() {
try {
int status = printer.getStatus();
logTrue("getStatus");
return statusCode2Str(status);
} catch (Exceptions e) {
e.printStackTrace();
logErr("getStatus", e.toString());
return "";
}

}

public void fontSet(EFontTypeAscii asciiFontType, EFontTypeExtCode cFontType) {
try {
printer.fontSet(asciiFontType, cFontType);
logTrue("fontSet");
} catch (Exceptions e) {
e.printStackTrace();
logErr("fontSet", e.toString());
}

}

public void spaceSet(byte wordSpace, byte lineSpace) {
try {
printer.spaceSet(wordSpace, lineSpace);
logTrue("spaceSet");
} catch (Exceptions e) {
e.printStackTrace();
logErr("spaceSet", e.toString());
}
}

public void printStr(String str, String charset) {
try {
printer.printStr(str, charset);
logTrue("printStr");
} catch (Exceptions e) {
e.printStackTrace();
logErr("printStr", e.toString());
}

}

public void step(int b) {
try {
printer.step(b);
logTrue("setStep");
} catch (Exceptions e) {
e.printStackTrace();
logErr("setStep", e.toString());
}
}

public void printBitmap(Bitmap bitmap) {
try {
Pic pic = new Pic(bitmap);
printer.printBitmap(pic);
logTrue("printBitmap");
} catch (Exceptions e) {
e.printStackTrace();
logErr("printBitmap", e.toString());
}
}

public String start() {
try {
int res = printer.start();
logTrue("start");
return statusCode2Str(res);
} catch (Exceptions e) {
e.printStackTrace();
logErr("start", e.toString());
return "";
}

}

public void leftIndents(short indent) {
try {
printer.leftIndent(indent);
logTrue("leftIndent");
} catch (Exceptions e) {
e.printStackTrace();
logErr("leftIndent", e.toString());
}
}

public int getDotLine() {
try {
int dotLine = printer.getDotLine();
logTrue("getDotLine");
return dotLine;
} catch (Exceptions e) {
e.printStackTrace();
logErr("getDotLine", e.toString());
return -2;
}
}

public void setGray(int level) {
try {
printer.setGray(level);
logTrue("setGray");
} catch (Exceptions e) {
e.printStackTrace();
logErr("setGray", e.toString());
}

}

public void setDoubleWidth(boolean isAscDouble, boolean isLocalDouble) {
try {
printer.doubleWidth(isAscDouble, isLocalDouble);
logTrue("doubleWidth");
} catch (Exceptions e) {
e.printStackTrace();
logErr("doubleWidth", e.toString());
}
}

public void setDoubleHeight(boolean isAscDouble, boolean isLocalDouble) {
try {
printer.doubleHeight(isAscDouble, isLocalDouble);
logTrue("doubleHeight");
} catch (Exceptions e) {
e.printStackTrace();
logErr("doubleHeight", e.toString());
}

}

public void setInvert(boolean isInvert) {
try {
printer.invert(isInvert);
logTrue("setInvert");
} catch (Exceptions e) {
e.printStackTrace();
logErr("setInvert", e.toString());
}

}

public String statusCode2Str(int status) {
String res = "";
switch (status) {
case 0:
res = "Success ";
break;
case 1:
res = "Printer is busy ";
break;
case 2:
res = "Out of paper ";
break;
case 3:
res = "The format of print data packet error ";
break;
case 4:
res = "Printer malfunctions ";
break;
case 8:
res = "Printer over heats ";
break;
case 9:
res = "Printer voltage is too low";
break;
case 240:
res = "Printing is unfinished ";
break;
case 252:
res = " The printer has not installed font library ";
break;
case 254:
res = "Data package is too long ";
break;
default:
break;
}
return res;
}
}
首先需要获取IDal dal对象,再通过getPrinter()方法拿到printer
package com.hisun.pos.utils.printer;

import android.util.Log;

import com.hisun.pos.activity.CashierChoosePaymentActivity;
import com.pax.gl.IGL;
import com.pax.ippi.dal.interfaces.IDal;
import com.pax.ippi.emv.interfaces.IEmv;

public class GetObj {

private static IDal dal;
private static IGL gl;
private static IEmv emv;

// 获取IDal dal对象
public static IDal getDal() {
dal = CashierChoosePaymentActivity.dal;
if (dal == null) {
Log.e("IPPITest", "dal is null");
}
return dal;
}

// 获取IGL gl对象
public static IGL getGL() {

gl = CashierChoosePaymentActivity.gl;
if (gl == null) {
Log.e("IPPITest", "gl is null");
}
return gl;

}

// 获取IEmv emv对象
public static IEmv getEmv() {

emv = CashierChoosePaymentActivity.emv;
if (emv == null) {
Log.e("IPPITest", "emv is null");
}
return emv;

}

}
IDal dal对象可在MainActivity中注册NeptuneUser对象拿到,dal = neptuneUser.getService().getDal();
public static NeptuneUser neptuneUser;
public static IDal dal;
public static IGL gl;
public static IEmv emv;

@Override
protected void setLayout() {
setContentView(R.layout.activity_cashier_choose_payment);

neptuneUser = NeptuneUser.getInstance(this);
dal = neptuneUser.getService().getDal();
gl = neptuneUser.getService().getGl();
emv = neptuneUser.getService().getEmv();
}

@Override
protected void onResume() {
super.onResume();
neptuneUser.register();
}

@Override
protected void onPause() {
super.onPause();
neptuneUser.unRegister();
}

补充:获取pos机设备信息的API,源自demo
package com.hisun.pos.utils.printer;

import com.hisun.pos.activity.CashierChoosePaymentActivity;
import com.pax.ipp.service.aidl.Exceptions;
import com.pax.ipp.service.aidl.dal.sys.EBeepMode;
import com.pax.ipp.service.aidl.dal.sys.ENavigationKey;
import com.pax.ipp.service.aidl.dal.sys.ETermInfoKey;
import com.pax.ipp.service.aidl.dal.sys.ETouchMode;
import com.pax.ippi.dal.interfaces.ISys;

import java.util.Map;

public class SysTester extends TestLog {

private static SysTester sysTester;
private ISys iSys = null;

private SysTester() {
iSys = GetObj.getDal().getSys();
}

public static SysTester getInstance() {
if (sysTester == null) {
sysTester = new SysTester();
}
return sysTester;
}

public void beep(final EBeepMode beepMode, final int delayTime) {
try {
iSys.beep(beepMode, delayTime);
logTrue("beep");
} catch (Exceptions e) {
e.printStackTrace();
logErr("beep", e.toString());
}

}

public String getTerminfo() {
try {
Map<ETermInfoKey, String> termInfo = iSys.getTermInfo();
logTrue("getTerminfo");
StringBuilder termInfoStr = new StringBuilder();
for (ETermInfoKey key : ETermInfoKey.values()) {
termInfoStr.append(key.name() + ":" + termInfo.get(key) + "\n");
}
return termInfoStr.toString();
} catch (Exceptions e) {
e.printStackTrace();
logErr("getTerminfo", e.toString());
return "";
}

}

public String getTerminfoModel() {
try {
Map<ETermInfoKey, String> termInfo = iSys.getTermInfo();
logTrue("getTerminfoModel");
return termInfo.get(ETermInfoKey.MODEL);
} catch (Exceptions e) {
e.printStackTrace();
logErr("getTerminfoModel", e.toString());
return "";
}
}

public String getRadom(int len) {
try {
byte[] random = iSys.getRandom(len);
if (random != null) {
logTrue("getRadom");
return CashierChoosePaymentActivity.gl.getConvert().bcdToStr(random);
} else {
logErr("getRadom", "return null");
return "null";
}
} catch (Exceptions e) {
e.printStackTrace();
logErr("getRadom", e.toString());
return "";
}

}

public String getDevInterfaceVer() {
try {
String verString = iSys.getDevInterfaceVer();
logTrue("getDevInterfaceVer");
//            return "version of device interface:" + verString;
return verString;
} catch (Exceptions e) {
e.printStackTrace();
logErr("getDevInterfaceVer", e.toString());
return "";
}
}

public void showNavigationBar(boolean flag) {
try {
iSys.showNavigationBar(flag);
logTrue("showNavigationBar");
} catch (Exceptions e) {
e.printStackTrace();
logErr("showNavigationBar", e.toString());
}
}

public void enableNavigationBar(boolean flag) {
try {
iSys.enableNavigationBar(flag);
logTrue("enableNavigationBar");
} catch (Exceptions e) {
e.printStackTrace();
logErr("enableNavigationBar", e.toString());
}
}

public void enableNavigationKey(ENavigationKey navigationKey, boolean flag) {
try {
iSys.enableNavigationKey(navigationKey, flag);
logTrue("enableNavigationKey");
} catch (Exceptions e) {
e.printStackTrace();
logErr("enableNavigationKey", e.toString());
}
}

public boolean isNavigationBarVisible() {
boolean res = true;
try {
res = iSys.isNavigationBarVisible();
logTrue("isNavigationBarVisible");
return res;
} catch (Exceptions e) {
e.printStackTrace();
logErr("isNavigationBarVisible", e.toString());
}
return res;
}

public boolean isNavigationBarEnabled() {
boolean res = true;
try {
res = iSys.isNavigationBarEnabled();
logTrue("isNavigationBarEnabled");
return res;
} catch (Exceptions e) {
e.printStackTrace();
logErr("isNavigationBarEnabled", e.toString());
}
return res;
}

public boolean isNavigationKeyEnabled(ENavigationKey navigationKey) {
boolean res = true;
try {
res = iSys.isNavigationKeyEnabled(navigationKey);
logTrue("isNavigationKeyEnabled");
return res;
} catch (Exceptions e) {
e.printStackTrace();
logErr("isNavigationKeyEnabled", e.toString());
}
return res;
}

public void showStatusBar(boolean flag) {
try {
iSys.showStatusBar(flag);
logTrue("showStatusBar");
} catch (Exceptions e) {
e.printStackTrace();
logErr("showStatusBar", e.toString());
}
}

public void enableStatusBar(boolean flag) {
try {
iSys.enableStatusBar(flag);
logTrue("enableStatusBar");
} catch (Exceptions e) {
e.printStackTrace();
logErr("enableStatusBar", e.toString());
}
}

public boolean isStatusBarEnabled() {
boolean res = true;
try {
res = iSys.isStatusBarEnabled();
logTrue("isStatusBarEnabled");
return res;
} catch (Exceptions e) {
e.printStackTrace();
logErr("isStatusBarEnabled", e.toString());
}
return res;
}

public boolean isStatusBarVisible() {
boolean res = true;
try {
res = iSys.isStatusBarVisible();
logTrue("isStatusBarVisible");
return res;
} catch (Exceptions e) {
e.printStackTrace();
logErr("isStatusBarVisible", e.toString());
}
return res;
}

public void resetStatusBar() {
try {
iSys.resetStatusBar();
logTrue("resetStatusBar");
} catch (Exceptions e) {
e.printStackTrace();
logErr("resetStatusBar", e.toString());
}
}

public void enablePowerKey(boolean flag) {
try {
iSys.enablePowerKey(flag);
logTrue("enablePowerKey");
} catch (Exceptions e) {
e.printStackTrace();
logErr("enablePowerKey", e.toString());
}
}

public boolean isPowerKeyEnabled() {
boolean res = true;
try {
res = iSys.isPowerKeyEnabled();
logTrue("isPowerKeyEnabled");
return res;
} catch (Exceptions e) {
e.printStackTrace();
logErr("isPowerKeyEnabled", e.toString());
}
return res;
}

public void setSettingsNeedPassword(boolean flag) {
try {
iSys.setSettingsNeedPassword(flag);
logTrue("setSettingsNeedPassword");
} catch (Exceptions e) {
e.printStackTrace();
logErr("setSettingsNeedPassword", e.toString());
}
}

public void reboot() {
try {
iSys.reboot();
logTrue("reboot");
} catch (Exceptions e) {
e.printStackTrace();
logErr("reboot", e.toString());
}
}

public void shutdown() {
try {
iSys.shutdown();
logTrue("shutdown");
} catch (Exceptions e) {
e.printStackTrace();
logErr("shutdown", e.toString());
}
}

public void switchTouchMode(ETouchMode touchMode) {
try {
iSys.switchTouchMode(touchMode);
logTrue("switchTouchMode");
} catch (Exceptions e) {
e.printStackTrace();
logErr("switchTouchMode", e.toString());
}
}

}


写了个倒叙的步骤。。。

实际打印出的凭条如图:



补充:源码就不上传了,上传下打印的demo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐