How to Use the Java API for Printer Operations

From wizarPOS
Revision as of 17:36, 23 December 2023 by Simon (talk | contribs)

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();