How to Use the Java API for Printer Operations
- Get PrinterDevice
device = (PrinterDevice) POSTerminal.getInstance(mContext).getDevice(POSTerminal.DEVICE_NAME_PRINTER);
- Open device
device.open();
- Print Text
try {
device.printText(
"Demo receipts" +
"MERCHANT COPY" +
"" +
"MERCHANT NAME" +
"SHXXXXXXCo.,LTD." +
"530310041315039" +
"TERMINAL NO" +
"50000045" +
"OPERATOR" +
"50000045" +
"");
} catch (DeviceException e) {
e.printStackTrace();
}
- Print Bitmap
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(mContext.getResources().getAssets()
.open("printer_barcode_low.png"));
} catch (Exception e1) {
e1.printStackTrace();
}
try {
Format format = new Format();
format.setParameter(Format.FORMAT_ALIGN, Format.FORMAT_ALIGN_RIGHT);
device.printBitmap(format, bitmap);
} catch (DeviceException e) {
e.printStackTrace();
}
- Print HTML
try {
final String htmlContent = "<!DOCTYPE html>" +
"<html>" +
"<head>" +
" <style type=\"text/css\">" +
" * {" +
" margin:0;" +
" padding:0;" +
" }" +
" </style>" +
"</head>" +
"<body>" +
"Demo receipts<br />" +
"MERCHANT COPY<br />" +
"<hr/>" +
"MERCHANT NAME<br />" +
"SHXXXXXXCo.,LTD.<br />" +
"530310041315039<br />" +
"TERMINAL NO<br />" +
"50000045<br />" +
"OPERATOR<br />" +
"50000045<br />" +
"<hr />" +
"CARD NO<br />" +
"623020xxxxxx3994 I<br />" +
"ISSUER ACQUIRER<br />" +
"<br />" +
"TRANS TYPE<br />" +
"PAY SALE<br />" +
"PAY SALE<br />" +
"<hr/>" +
"<br />" +
"Demo receipts,do not sign!<br />" +
"<br />" +
"<br />" +
"<br />" +
"</body>" +
"</html>";
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
try {
device.printHTML(mContext, htmlContent, null);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
- Close device
device.close();