How to Use the Java API for Printer Operations: Difference between revisions
(Created page with "* Get PrinterDevice <syntaxhighlight lang="java" line='line'> device = (PrinterDevice) POSTerminal.getInstance(mContext).getDevice(POSTerminal.DEVICE_NAME_PRINTER); </syntax...") |
No edit summary |
||
Line 1: | Line 1: | ||
* | == Java API Guide for Printer Operations == | ||
1. '''Get PrinterDevice:''' | |||
* Initiate the process by obtaining an instance of the PrinterDevice. | |||
<syntaxhighlight lang="java" line='line'> | <syntaxhighlight lang="java" line='line'> | ||
device = (PrinterDevice) POSTerminal.getInstance(mContext).getDevice(POSTerminal.DEVICE_NAME_PRINTER); | device = (PrinterDevice) POSTerminal.getInstance(mContext).getDevice(POSTerminal.DEVICE_NAME_PRINTER); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
* Open device | 2. '''Open Device:''' | ||
* Open the printer device to establish communication. | |||
<syntaxhighlight lang="java" line='line'> | <syntaxhighlight lang="java" line='line'> | ||
device.open(); | device.open(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
* Print Text | 3. '''Print Operations:''' | ||
* '''Print Text:''' | |||
** Use the relevant Java API methods to print text documents. | |||
<syntaxhighlight lang="java" line='line'> | <syntaxhighlight lang="java" line='line'> | ||
try { | try { | ||
Line 26: | Line 31: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
* Print Bitmap | * '''Print Bitmap:''' | ||
** Implement the API functionalities to print bitmap images. | |||
<syntaxhighlight lang="java" line='line'> | <syntaxhighlight lang="java" line='line'> | ||
Bitmap bitmap = null; | Bitmap bitmap = null; | ||
Line 45: | Line 51: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
* Print HTML | * '''Print HTML:''' | ||
** Utilize the API for printing HTML content. | |||
<syntaxhighlight lang="java" line='line'> | <syntaxhighlight lang="java" line='line'> | ||
try { | try { | ||
Line 100: | Line 107: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
* | 4. '''Close Device:''' | ||
* Properly close the printer device after operations to ensure system integrity and resource management. | |||
<syntaxhighlight lang="java" line='line'> | <syntaxhighlight lang="java" line='line'> | ||
device.close(); | device.close(); | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 17:36, 23 December 2023
Java API Guide for Printer Operations
1. Get PrinterDevice:
- Initiate the process by obtaining an instance of the PrinterDevice.
device = (PrinterDevice) POSTerminal.getInstance(mContext).getDevice(POSTerminal.DEVICE_NAME_PRINTER);
2. Open Device:
- Open the printer device to establish communication.
device.open();
3. Print Operations:
- Print Text:
- Use the relevant Java API methods to print text documents.
try {
device.printText(
"Demo receipts" +
"MERCHANT COPY" +
"" +
"MERCHANT NAME" +
"SHXXXXXXCo.,LTD." +
"530310041315039" +
"TERMINAL NO" +
"50000045" +
"OPERATOR" +
"50000045" +
"");
} catch (DeviceException e) {
e.printStackTrace();
}
- Print Bitmap:
- Implement the API functionalities to print bitmap images.
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:
- Utilize the API for printing HTML content.
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();
}
4. Close Device:
- Properly close the printer device after operations to ensure system integrity and resource management.
device.close();