PrinterManager

##
android.device
Class PrinterManager

    java.lang.Object
      android.device.PrinterManager

    public class PrinterManager

extends java.lang.Object

The android.device.PriterManager provides a functional interface for initializing and controlling the printer.

To print data with this class, according to the following steps,

1. You should import the class first.
   `import android.device.PrinterManager;`
2. You can obtain an instance of this class by calling.
   `PrinterManager printer = new PrinterManager()`;
3. After that, you can set up the paper through `setupPage`.
4. If necessary, set print content through `drawLine, drawText, drawBarcode`, etc.
5. Print out the small ticket by calling `printPage`.

   For more information about the printer, read PrinterManager sample.

Field Summary
`static int` | `PRNSTS_BUSY` The printer is currently busy.
`static int` | `PRNSTS_ERR` An unknown error has occurred.
`static int` | `PRNSTS_ERR_DRIVER` An driver error has occurred.
`static int` | `PRNSTS_OK` The printer is working OK.
`static int` | `PRNSTS_OUT_OF_PAPER` The printer is out of paper.
`static int` | `PRNSTS_OVER_HEAT` The printer is over heat.
`static int` | `PRNSTS_UNDER_VOLTAGE` Printer battery voltage too low.

Constructor Summary
`PrinterManager()` The PriterManager provides a functional interface for initializing and controlling the printer.

Method Summary
`int` | `clearPage()` Clear the current page.
`int` | `close()` Close the printer.
`int` | `drawBarcode(java.lang.String data, int x, int y, int barcodeType, int width, int height, int rotate)` Draw barcode on the current page.
`int` | `drawBitmap(Bitmap bmp, int xDest, int yDest)` Draw a bitmap on the current page.
`int` | `drawBitmapEx(byte[] pbmp, int xDest, int yDest, int widthDest, int heightDest)` Draw a mono-bitmaps on the current page.
`int` | `drawLine(int x0, int y0, int x1, int y1, int lineWidth)` Draw a line in the current page.
`int` | `drawText(java.lang.String data, int x, int y, java.lang.String fontName, int fontSize, boolean bold, boolean italic, int rotate)` Draw text on the current page.
`int` | `drawTextEx(java.lang.String data, int x, int y, int width, int height, java.lang.String fontName, int fontSize, int rotate, int style, int format)` Draw text on the current page.
`int` | `getStatus()` Get the current state of the printer. See `PRNSTS_OK` `PRNSTS_OUT_OF_PAPER` `PRNSTS_OVER_HEAT` `PRNSTS_UNDER_VOLTAGE` `PRNSTS_BUSY` `PRNSTS_ERR` `PRNSTS_ERR_DRIVER`
`int` | `open()` Open the printer.
`int` | `printPage(int rotate)` Print the current page.
`void` | `setSpeedLevel(int level)` Set print speed level.
`void` | `setGrayLevel(int level)` Set print gray level.
`void` | `paperFeed(int level)` Set paper feed function.
`int` | `setupPage(int width, int height)` Set the page size.

Methods inherited from class java.lang.Object
`equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

FIELD DETAIL

### PRNSTS_OK

    public static final int PRNSTS_OK

  The printer is working OK

  See Also:
    Constant Field Values

### PRNSTS_OUT_OF_PAPER

    public static final int PRNSTS_OUT_OF_PAPER

  The printer is out of paper

  See Also:
    Constant Field Values

### PRNSTS_OVER_HEAT

    public static final int PRNSTS_OVER_HEAT

  The printer is over heat

  See Also:
    Constant Field Values

### PRNSTS_UNDER_VOLTAGE

    public static final int PRNSTS_UNDER_VOLTAGE

  The printer is under voltage

  See Also:
    Constant Field Values

### PRNSTS_BUSY

    public static final int PRNSTS_BUSY

  The printer is currently busy

  See Also:
    Constant Field Values

### PRNSTS_ERR

    public static final int PRNSTS_ERR

  An unknown error has occurred

  See Also:
    Constant Field Values

### PRNSTS_ERR_DRIVER

    public static final int PRNSTS_ERR_DRIVER

  An driver error has occurred

  See Also:
    Constant Field Values

Constructor Detail

### PrinterManager

    public PrinterManager()

Method Detail

### open

    public int open()

  Opens the printer.

  Parameters:
    none
  Returns:
    0 if open successful, -1 if failed.
  Example:
    `PrinterManager printerManager = new PrinterManager();
    int ret = printerManager.open();
    if (ret != 0) {
    //open failed
    }`

### close

    public int close()

  Close the printer.

  Parameters:
    none
  Returns:
    0 if open successful, -1 if failed.
  Example:
    `int ret = printerManager.open();
    if(ret == 0) {
    printerManager.close();
    }`

### paperFeed

    public void paperFeed(int level)

  Set paper feed function.

  Parameters:
    `level` - Number of steps required. The range of Gray level is 0 to 100, 1mm for every 1 units.
  Returns:
    none
  Example:
    `int ret = printerManager.open();
    if(ret == 0) {
    int level = 8;
    printerManager.paperFeed(level);
    }`

### setSpeedLevel

    public void setSpeedLevel(int level)

  Set print speed level.

  Parameters:
    `level` - The range of Gray level is 0 to 9. The default value is 9.
  Returns:
    none

### setGrayLevel

    public void setGrayLevel(int level)

  Set print gray level.

  Parameters:
    `level` - The range of Gray level is 0 to 4. The default value is 0.
  Returns:
    none
  Example:
    `int ret = printerManager.open();
    if(ret == 0) {
    int grayLevel = 1;
    printerManager.setGrayLevel(grayLevel);
    }`

### setupPage

    public int setupPage(int width,
                         int height)

  Set the page size. Unit is in pixel. 8 pixels is equivalent to 1 mm.

  Parameters:
    `width` - Page width. -1 means largest possible width (width = 384).
    `height` - Page height. -1 means printer driver to manage the page height.
  Returns:
    0 if success, -1 if failed.
  Example:
    `int ret = printerManager.open();
    if(ret == 0) {
    int width = 384;
    int height = -1;
    int re = printerManager.setupPage(width, height);
    if(re < 0) {
    //setupPage failed
    }
    }`

### clearPage

    public int clearPage()

  Clear the current page.

  Parameters:
    none
  Returns:
    0 if successful, -1 if failed.
  Example:
    `int ret = printerManager.open();
    if(ret == 0) {
    int re = printerManager.clearPage();
    if (re < 0) {
    //clear failed
    }
    }`

### printPage

    public int printPage(int rotate)

  Print the current page.

  Parameters:
    `rotate` - The rotation angle, currently supports only 0 (non-rotating)
  Returns:
    print status. See
    `PRNSTS_OK`
    `PRNSTS_OUT_OF_PAPER`
    `PRNSTS_OVER_HEAT`
    `PRNSTS_UNDER_VOLTAGE`
    `PRNSTS_BUSY`
    `PRNSTS_ERR`
    `PRNSTS_ERR_DRIVER`
  Example:
    `int ret = printerManager.open();
    if(ret == 0) {
    public static final int ROTATE = 0;
    int status = printerManager.printPage(ROTATE);
    }`

### drawLine

    public int drawLine(int x0,
                        int y0,
                        int x1,
                        int y1,
                        int lineWidth)

  Draw a line in the current page. The (0,0) point axis is on the upper left corner of the screen.

  Parameters:
    `x0` - Start point X axis.
    `y0` - Start point Y axis.
    `x1` - End point X axis.
    `y1` - End point Y axis.
    `lineWidth` - In pixel. 8 pixels is equivalent to 1 mm.
  Returns:
    0 if successful, -1 if failed.
  Example:
    `int ret = printerManager.open();
    if(ret == 0) {
    int x0 = 16;
    int y0 = 0;
    int x1 = 300;
    int y1 = 100;
    int lineWidth = 4;
    int re = printerManager.drawLine(x0,y0,x1,y1,lineWidth);
    if (re < 0) {
    //drawLine failed
    }
    }`

### drawText

    public int drawText(java.lang.String data,
                        int x,
                        int y,
                        java.lang.String fontName,
                        int fontSize,
                        boolean bold,
                        boolean italic,
                        int rotate)

  Draw text on the current page.

  Parameters:
    `data` - The string to draw.
    `x` - Start point X axis.
    `y` - Start point Y axis.
    `fontName` - Font to use, otherwise, default system font will be used. Custom fonts can be specified, for example, specifying the full path /mnt/sdcard/xxx.ttf.
    `fontSize` - The font size, in pixel.
    `bold` - Set font style to bold.
    `italic` - Set font style to italic.
    `rotate` - The text direction. Value:
    0 : no rotation
    1 : rotate 90 degree
    2 : rotate 180 degree
    3 : rotate 270 degree
  Returns:
    Actual printing height if successful, -1 if failed.
  Example:
    `int ret = printerManager.open();
    if(ret == 0) {
    String data = "hello";
    int x = 200;
    int y = 200;
    String fontName = "/system/fonts/kaishu.ttf";
    int fontSize = 8;
    public static final int NO_ROTATE = 0;
    int height = printerManager.drawText(data, x, y, fontName, fontSize, false, false, NO_ROTATE);
    if (height < 0) {
    //drawText failed
    }
    }`

### drawTextEx

    public int drawTextEx(java.lang.String data,
                          int x,
                          int y,
                          int width,
                          int height,
                          java.lang.String fontName,
                          int fontSize,
                          int rotate,
                          int style,
                          int format)

  Draw text on the current page.

  Parameters:
    `data` - The string to draw.
    `x` - Start point X axis.
    `y` - Start point Y axis.
    `width` - Text is printed to the width of a rectangle on the page.
    `height` - Text is printed to the height of a rectangle on the page.
    `fontName` - Font to use, otherwise, default system font will be used. Or custom fonts i.e. /mnt/sdcard/xxx.ttf the path.
    `fontSize` - The font size, in pixel.
    `rotate` - Print the text rotated to requested degress. Value:
    0 : no rotation
    1 : rotate 90 degree
    2 : rotate 180 degree
    3 : rotate 270 degree
    `style` - Font style of value:
    0x0001 : underline
    0x0002 : italic
    0x0004 : bold
    0x0008 : reverse effect
    0x0010 : strike out
    you can mix the style by using the or operator, style= 0x0002|0x0004.
    `format` - Set to 0 means word wrap at the specified width range 0 to 384, set to 1 means no word wrap.
  Returns:
    Actual printing height if successful, -1 if failed.
  Example:
    `int ret = printerManager.open();
    if(ret == 0) {
    String data = "hello";
    int x = 200;
    int y = 200;
    int width = 100;
    int height = -1;
    String fontName = "/system/fonts/kaishu.ttf";
    int fontSize = 8;
    public static final int NO_ROTATE = 0;
    int style = 0x0001;
    int format = 0;
    int ret = printerManager.drawTextEx(data, x, y, width, height, fontName, fontSize, NO_ROTATE, style, format);
    if (ret < 0) {
    //drawTextEx failed
    }
    }`

### drawBarcode

    public int drawBarcode(java.lang.String data,
                           int x,
                           int y,
                           int barcodeType,
                           int width,
                           int height,
                           int rotate)

  Draw barcode on the current page.

  Parameters:
    `data` - The barcode text.
    `x` - Start point at X axis.
    `y` - Start point at Y axis.
    `barcodeType` - Following Table shows the supported symbology:

    BARCODE_CODE11 | 1
    BARCODE_C25MATRIX | 2
    BARCODE_C25INTER | 3
    BARCODE_C25IATA | 4
    BARCODE_C25LOGIC | 6 |
    BARCODE_C25IND | 7
    BARCODE_CODE39 | 8
    BARCODE_EXCODE39 | 9
    BARCODE_EANX | 13
    BARCODE_EAN128 | 16
    BARCODE_CODABAR | 18
    BARCODE_CODE128 | 20
    BARCODE_DPLEIT | 21
    BARCODE_DPIDENT | 22
    BARCODE_CODE16K | 23
    BARCODE_CODE49 | 24
    BARCODE_CODE93 | 25
    BARCODE_FLAT | 28
    BARCODE_RSS14 | 29
    BARCODE_RSS_LTD | 30
    BARCODE_RSS_EXP | 31
    BARCODE_TELEPEN | 32
    BARCODE_UPCA | 34
    BARCODE_UPCE | 37
    BARCODE_POSTNET | 40
    BARCODE_MSI_PLESSEY | 47
    BARCODE_FIM | 49
    BARCODE_LOGMARS | 50
    BARCODE_PHARMA | 51
    BARCODE_PZN | 52
    BARCODE_PHARMA_TWO | 53
    BARCODE_PDF417 | 55
    BARCODE_PDF417TRUNC | 56
    BARCODE_MAXICODE | 57
    BARCODE_QRCODE | 58
    BARCODE_CODE128B | 60
    BARCODE_AUSPOST | 63
    BARCODE_AUSREPLY | 66
    BARCODE_AUSROUTE | 67
    BARCODE_AUSREDIRECT | 68
    BARCODE_ISBNX | 69
    BARCODE_RM4SCC | 70
    BARCODE_DATAMATRIX | 71
    BARCODE_EAN14 | 72
    BARCODE_CODABLOCKF | 74
    BARCODE_NVE18 | 75
    BARCODE_JAPANPOST | 76
    BARCODE_KOREAPOST | 77
    BARCODE_RSS14STACK | 79
    BARCODE_RSS14STACK_OMNI | 80
    BARCODE_RSS_EXPSTACK | 81
    BARCODE_PLANET | 82
    BARCODE_MICROPDF417 | 84
    BARCODE_ONECODE | 85
    BARCODE_PLESSEY | 86
    BARCODE_AZTEC | 92

    `width` - There are four thickness level to the lines, 1 being the thinnest and 4 being the thickest.
    `height` - The barcode height in pixel.
    `rotate` - The barcode rotation, Value:
    0 : no rotation
    1 : rotate 90 degree
    2 : rotate 180 degree
    3 : rotate 270 degree
  Returns:
    Actual printing height if successful, -1 if failed.

### drawBitmap

    public int drawBitmap(Bitmap bmp,
                          int xDest,
                          int yDest)

  Draw a bitmap on the current page.

  Parameters:
    `bmp` - The bitmap to drawn.
    `xDest` - Start point at X axis.
    `yDest` - Start point at Y axis.
  Returns:
    Actual printing height is successful, -1 if failed.
  Example:
    `int ret = printerManager.open();
    if(ret == 0) {
    int x = 100;
    int y = 100;
    BitmapFactory.Options opts = new BitmapFactory.Options();
    Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.ticket, opts);
    int ret = printerManager.drawBitmap(img, x, y);
    if(ret < 0) {
    //drawBitmap failed
    }
    }`

### drawBitmapEx

    public int drawBitmapEx(byte[] bmp,
                            int xDest,
                            int yDest,
                            int widthDest,
                            int heightDest)

  Draw a mono-bitmaps on the current page.

  Parameters:
    `bmp` - ByteArray data for mono-bitmaps.
    `xDest` - Start point at X axis.
    `yDest` - Start point at Y axis.
    `widthDest` - Horizontal width bytes.
    `heightDest` - Vertical height point.
  Returns:
    Actual printing height is successful, -1 if failed.

### getStatus

    public int getStatus()

  Get the current state of the printer.

  Parameters:
    none
  Returns:
    The printer status. See
    `PRNSTS_OK`
    `PRNSTS_OUT_OF_PAPER`
    `PRNSTS_OVER_HEAT`
    `PRNSTS_UNDER_VOLTAGE`
    `PRNSTS_BUSY`
    `PRNSTS_ERR`
    `PRNSTS_ERR_DRIVER`