public class Printer
extends java.lang.Object
This class implements methods for control printers with extended version of
the Datecs's ESC/P control language. For data transmit it use instances
InputStream
and OutputStream
classes. The
InputStream
instance must provide a proper implementation of
InputStream.available()
method.
If the printer is set to work in "Protocol mode" then the protocol data
encapsulation must be handled through an instance of
ProtocolAdapter
, using
ProtocolAdapter.CHANNEL_PRINTER
logical channel socket.
All methods are internally synchronized with instance of current object.
Modifier and Type | Class and Description |
---|---|
static interface |
Printer.ConnectionListener
Interface definition for a Printer event callback.
|
Modifier and Type | Field and Description |
---|---|
static int |
ALIGN_CENTER
The printing object is centered.
|
static int |
ALIGN_LEFT
The printing object is left aligned.
|
static int |
ALIGN_RIGHT
The printing object is left aligned.
|
static int |
BARCODE_CODABAR
CODABAR (NW-7) barcode type.
|
static int |
BARCODE_CODE128
CODE 128 barcode type.
|
static int |
BARCODE_CODE128AUTO
CODE 128 Auto barcode type.
|
static int |
BARCODE_CODE39
CODE 39 barcode type.
|
static int |
BARCODE_CODE93
CODE 93 barcode type.
|
static int |
BARCODE_EAN128
EAN 128 barcode type.
|
static int |
BARCODE_EAN13
EAN13 (JAN13) barcode type.
|
static int |
BARCODE_EAN8
EAN 8 (JAN8) barcode type.
|
static int |
BARCODE_ITF
ITF barcode type.
|
static int |
BARCODE_PDF417
PDF417 barcode type.
|
static int |
BARCODE_UPCA
UPC-A barcode type.
|
static int |
BARCODE_UPCE
UPC-E barcode type.
|
static int |
FILL_BLACK
The area is filled (black color).
|
static int |
FILL_INVERTED
The area is inverted.
|
static int |
FILL_WHITE
The area is cleared (white color).
|
static int |
HRI_ABOVE
The HRI code is drawn above the barcode.
|
static int |
HRI_BELOW
The HRI code is drawn below the barcode.
|
static int |
HRI_BOTH
The HRI code is drawn both above and below the barcode.
|
static int |
HRI_NONE
No HRI code is being drawn.
|
static int |
PAGE_BOTTOM
Printing from bottom to top, feed to right.
|
static int |
PAGE_LEFT
Printing page from left to right, feed to bottom.
|
static int |
PAGE_RIGHT
Printing page from right to left, feed to top.
|
static int |
PAGE_TOP
Printing page from top to bottom, feed to left.
|
static int |
SETTINGS_BLUETOOTH
Bluetooth settings.
|
Constructor and Description |
---|
Printer(java.io.InputStream in,
java.io.OutputStream out)
Constructs a new instance of this class from a given
InputStream and OutputStream . |
Printer(java.io.OutputStream out)
Constructs a new instance of this class from a given
OutputStream . |
Modifier and Type | Method and Description |
---|---|
void |
beep()
Sounds the buzzer.
|
void |
calibrateBMMSensor()
Black mark mode sensor calibration.
|
void |
close()
Close the streams and release all associated resources.
|
void |
cutPaper()
Cut paper.
|
void |
cutPaper(int m,
int n)
Cut paper.
|
void |
drawPageFrame(int x,
int y,
int width,
int height,
int fillMode,
int thickness)
Draws a rectangle frame with selected thickness in page mode.
|
void |
drawPageRectangle(int x,
int y,
int width,
int height,
int fillMode)
Draws a rectangle in page mode.
|
void |
feedLabel()
Feed to the next label.
|
void |
feedPaper(int lines)
Feeds paper with specified lines number.
|
void |
flush()
Flushes the output stream and forces any buffered output bytes to be
written out.
|
void |
flush(boolean wait)
Flushes the output stream and forces any buffered output bytes to be
written out.
|
PrinterInformation |
getInformation()
Returns the printer information.
|
PrinterInformation |
getInformation(int timeout)
Returns the printer information.
|
java.lang.String |
getSerialNumber()
Return printer serial number.
|
java.lang.String[] |
getSettings(int type)
Reading printer settings.
|
int |
getStatus()
Returns the printer status.
|
int |
getStatus(int timeout)
Returns the printer status.
|
int |
getTemperature()
Returns the printer head temperature.
|
TouchChip |
getTouchChip()
Get instance of TouchChip module.
|
int |
getVoltage()
Returns the printer battery voltage.
|
int |
getVoltagePercentage()
Returns the printer battery level in percents.
|
void |
journalFormat()
Format journal
|
void |
journalPrint()
Print journal
|
byte[] |
journalRead()
Read journal
|
java.lang.String |
journalRead(java.lang.String charset)
Read journal
|
void |
journalWrite(byte[] data)
Write journal data.
|
void |
journalWrite(java.lang.String text,
java.lang.String charset)
Write journal text.
|
void |
melody(java.lang.String data)
Making (beeping) a sequence of sounds with a certain frequency and duration.
|
void |
printArabicText(int codetable,
java.lang.String text)
Print Arabic text.
|
void |
printBarcode(int type,
byte[] data)
Prints a barcode.
|
void |
printBarcode(int type,
java.lang.String data)
Prints a barcode.
|
void |
printCompressedImage(int[] argb,
int width,
int height,
int align,
boolean dither) |
void |
printCompressedImage(int[] argb,
int width,
int height,
int align,
boolean dither,
boolean crop) |
void |
printImage(int[] argb,
int width,
int height,
int align,
boolean dither) |
void |
printImage(int[] argb,
int width,
int height,
int align,
boolean dither,
boolean crop) |
void |
printLogo()
Prints a stored bit image (logo).
|
void |
printLogo(boolean wide,
boolean high)
Prints a stored bit image (logo).
|
void |
printPage()
Prints the non blank page area only in page mode.
|
void |
printQRCode(int size,
int eccLv,
byte[] data)
Prints the QRCode.
|
void |
printQRCode(int size,
int eccLv,
java.lang.String data)
Prints the QRCode.
|
void |
printSelfTest()
Prints self test receipt.
|
void |
printTaggedText(java.lang.String s)
Prints a tagged text using default charset for encoding.
|
void |
printTaggedText(java.lang.String s,
java.lang.String encoding)
Prints a tagged text using the named charset for encoding.
|
void |
printText(byte[] b)
Prints a text using default charset for encoding.
|
void |
printText(java.lang.String s)
Prints a text using default charset for encoding.
|
void |
printText(java.lang.String s,
java.lang.String encoding)
Prints a text using the named charset for encoding.
|
java.lang.String |
readBarcode(int timeout)
Reads the barcode.
|
java.lang.String[] |
readCard(boolean first,
boolean second,
boolean third,
int timeout)
Reads the magnetic stripe card.
|
void |
reset()
Resets printer to it's default settings.
|
void |
selectCodetable(int codetable)
Selects a code table.
|
void |
selectPageMode()
Switches from standard mode to page mode.
|
void |
selectStandardMode()
Switches from page mode to standard mode.
|
void |
setAlign(int align)
Sets alignment for the next printed text row or object.
|
void |
setBarcode(int align,
boolean small,
int scale,
int hri,
int height)
Sets the various barcode parameters.
|
void |
setConnectionListener(Printer.ConnectionListener listener)
Set a callback listener to notify the connection state is changed.
|
static void |
setDebug(boolean on)
Specify whether the object can output debug information
|
void |
setIntensity(int level)
Set intensity level.
|
void |
setLineSpace(int lines)
Sets the line space of text rows.
|
void |
setPageRegion(int x,
int y,
int width,
int height,
int direction)
Defines the relative position, size print direction of the page.
|
void |
setPageXY(int x,
int y)
Sets the print position in current page region.
|
void |
setPresenterActiveMode(int n)
Select the active mode on the presenter.
|
void |
setPresenterOpMode(int n)
Select the presenter operation.
|
void |
setQRCode(int cellSize)
Sets the cell size for two dimensional barcode QR Code.
|
void |
setRetractiveModeTimeout(int timeout)
Set the time-out for the retractive mode.
|
void |
turnOff()
Turn off printer.
|
void |
write(byte[] b)
Writes all bytes from the specified byte array to this printer.
|
void |
write(byte[] b,
int offset,
int length)
Writes
length bytes from the specified byte array to this
printer. |
void |
write(int b)
Writes the specified byte to this printer.
|
public static final int ALIGN_LEFT
public static final int ALIGN_CENTER
public static final int ALIGN_RIGHT
public static final int BARCODE_UPCA
public static final int BARCODE_UPCE
public static final int BARCODE_EAN13
public static final int BARCODE_EAN8
public static final int BARCODE_CODE39
public static final int BARCODE_ITF
public static final int BARCODE_CODABAR
public static final int BARCODE_CODE93
public static final int BARCODE_CODE128
public static final int BARCODE_PDF417
public static final int BARCODE_CODE128AUTO
public static final int BARCODE_EAN128
public static final int HRI_NONE
public static final int HRI_ABOVE
public static final int HRI_BELOW
public static final int HRI_BOTH
public static final int PAGE_LEFT
public static final int PAGE_BOTTOM
public static final int PAGE_RIGHT
public static final int PAGE_TOP
public static final int FILL_WHITE
public static final int FILL_BLACK
public static final int FILL_INVERTED
public static final int SETTINGS_BLUETOOTH
public Printer(java.io.OutputStream out) throws java.io.IOException
OutputStream
.
The in
is set to null
.
out
- the output stream.java.io.IOException
- if an I/O error occurs.public Printer(java.io.InputStream in, java.io.OutputStream out) throws java.io.IOException
InputStream
and OutputStream
.in
- the input stream.out
- the output stream.java.io.IOException
- if an I/O error occurs.public void close()
public static void setDebug(boolean on)
on
- True to enable debug; otherwise false.public void write(int b) throws java.io.IOException
b
- the byte.java.io.IOException
- if an I/O error occurs.public void write(byte[] b) throws java.io.IOException
If b
is null
, a
NullPointerException
is thrown.
b
- the data.java.io.IOException
- if an I/O error occurs.write(byte[], int, int)
public void write(byte[] b, int offset, int length) throws java.io.IOException
length
bytes from the specified byte array to this
printer.
If b
is null
, a
NullPointerException
is thrown.
if offset
is negative, or length
is negative,
or offset + length
is greater than the length of the array
b
, then an IndexOutOfBoundsException
is thrown.
b
- the data.offset
- the start offset in the data.length
- the number of bytes to write.java.io.IOException
- if an I/O error occurs.public void setConnectionListener(Printer.ConnectionListener listener)
listener
- the listener.public void flush() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public void flush(boolean wait) throws java.io.IOException
wait
- if enabled this method will wait to complete printing.java.io.IOException
- if an I/O error occurs.public PrinterInformation getInformation() throws java.io.IOException
If in
is null
, a
NullPointerException
is thrown.
PrinterInformation
.java.io.IOException
- if an I/O error occurs.public java.lang.String getSerialNumber() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public PrinterInformation getInformation(int timeout) throws java.io.IOException
If in
is null
, a
NullPointerException
is thrown.
timeout
- The time to complete operation.PrinterInformation
.java.io.IOException
- if an I/O error occurs.public int getVoltage() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public int getVoltagePercentage() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public int getTemperature() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public int getStatus() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public int getStatus(int timeout) throws java.io.IOException
timeout
- The time to complete operation.java.io.IOException
- if an I/O error occurs.public java.lang.String[] readCard(boolean first, boolean second, boolean third, int timeout) throws java.io.IOException
If the in
is null
, a
NullPointerException
is thrown.
If first
is false
, and second
is
false
, and third
is false
then a
IllegalArgumentException
is thrown.
If wait
is negative, a IllegalArgumentException
is thrown.
first
- the flag indicates where to read first track (track 1).second
- the flag indicates where to read second track (track 2).third
- the flag indicates where to read third track (track 3).timeout
- the time in milliseconds to read the card data. The actual
scan time may differ, but will be as close as possible to this
value.null
.java.io.IOException
- if an I/O error occurs.public java.lang.String readBarcode(int timeout) throws java.io.IOException
If in
is null
, a
NullPointerException
is thrown.
If wait
is negative, a IllegalArgumentException
is thrown.
timeout
- the time in seconds to read the barcode. The actual scan
time may differ, but will be as close as possible to this
value.java.io.IOException
- if an I/O error occurs.public void reset() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public void turnOff() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public void printSelfTest() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public void selectCodetable(int codetable) throws java.io.IOException
If codetable
is negative, a
IllegalArgumentException
is thrown.
codetable
- the code table.java.io.IOException
- if an I/O error occurs.public void setLineSpace(int lines) throws java.io.IOException
Line space is the vertical distance between beginning of two consecutive
text rows. The text rows can not be overlapped. When the
lines
value is less then vertical text size it shall be
adjusted to the lower possible value.
lines
- the line space of text rows, value between 0 and 255.java.io.IOException
- if an I/O error occurs.public void setAlign(int align) throws java.io.IOException
If align
illegal, a IllegalArgumentException
is
thrown.
align
- the alignment of text or object. See the ALIGN_
* constant field values.java.io.IOException
- if an I/O error occurs.public void feedPaper(int lines) throws java.io.IOException
lines
- the lines to feed paper, value between 0 and 255.java.io.IOException
- if an I/O error occurs.public void beep() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public void melody(java.lang.String data) throws java.io.IOException
The data is in format, similar to the one used for writing notes and can be of any length. The first invalid character cancels the command.
For more information please refer to printer documentation.
data
- the note data.java.io.IOException
- if an I/O error occurs.public void printText(byte[] b) throws java.io.IOException
b
- the text in byte array.java.io.IOException
- if an I/O error occurs.public void printText(java.lang.String s) throws java.io.IOException
The text is encode into a sequence of bytes using the default charset encoding.
If s
is null
, a
NullPointerException
is thrown.
s
- the text.java.io.IOException
- if an I/O error occurs.public void printText(java.lang.String s, java.lang.String encoding) throws java.io.IOException
The text is encode into a sequence of bytes using the given charset.
If s
is null
, a
NullPointerException
is thrown.
s
- the text.encoding
- the name of a supported charset.java.io.IOException
- if an I/O error occurs.public void printTaggedText(java.lang.String s) throws java.io.IOException
The text is encode into a sequence of bytes using the default charset encoding.
{reset} | Reset to default settings. |
{br} | Line break. Equivalent of new line. |
{b}, {/b} | Set or clear bold font style. |
{u}, {/u} | Set or clear underline font style. |
{i}, {/i} | Set or clear italic font style. |
{s}, {/s} | Set or clear small font style. |
{h}, {/h} | Set or clear high font style. |
{w}, {/w} | Set or clear wide font style. |
{left} | Aligns text to the left paper edge. |
{center} | Aligns text to the center of paper. |
{right} | Aligns text to the right paper edge. |
s
- the tagged text to print.java.io.IOException
- if an I/O error occurs.public void printTaggedText(java.lang.String s, java.lang.String encoding) throws java.io.IOException
The text is encode into a sequence of bytes using the given charset.
s
- the tagged text to print.encoding
- the name of a supported charset.java.io.IOException
- if an I/O error occurs.printTaggedText(String)
public void printArabicText(int codetable, java.lang.String text) throws java.io.IOException
codetable
- the Arabic codetable.text
- the text to print.java.io.IOException
- if an I/O error occurs.public void printImage(int[] argb, int width, int height, int align, boolean dither, boolean crop) throws java.io.IOException
java.io.IOException
public void printImage(int[] argb, int width, int height, int align, boolean dither) throws java.io.IOException
java.io.IOException
public void printCompressedImage(int[] argb, int width, int height, int align, boolean dither, boolean crop) throws java.io.IOException
java.io.IOException
public void printCompressedImage(int[] argb, int width, int height, int align, boolean dither) throws java.io.IOException
java.io.IOException
public void printLogo(boolean wide, boolean high) throws java.io.IOException
wide
- the flag specifies to print logo in double width size.high
- the flag specifies to print logo in double height size.java.io.IOException
- if an I/O error occurs.public void printLogo() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public void setBarcode(int align, boolean small, int scale, int hri, int height)
align
- the align of barcode. See the ALIGN_
* constant
field values.small
- the HRI code font size.scale
- the scale factor of barcode, value between 2 and 4.hri
- the HRI text position of barcode. See the HRI_
*
constant field values.height
- the height of barcode, value between 1 and 255.public void printBarcode(int type, byte[] data) throws java.io.IOException
type
- the barcode type. See the BARCODE_
* constant
field values.data
- the barcode data.java.io.IOException
- if an I/O error occurs.setBarcode(int, boolean, int, int, int)
public void printBarcode(int type, java.lang.String data) throws java.io.IOException
The data is encode into a sequence of bytes using the default charset.
If data
is null
, a
NullPointerException
is thrown.
If type
is not valid, or data.length
is not
valid for a specified type
, then a IllegalArgumentException
is thrown.
type
- the barcode type. See the BARCODE_
* constant
field values.data
- the barcode data.java.io.IOException
- if an I/O error occurs.setBarcode(int, boolean, int, int, int)
public void printQRCode(int size, int eccLv, byte[] data) throws java.io.IOException
size
- the size of symbol, value in {0, 1, 4, 6, 8, 10, 12, 14}.eccLv
- the error collection control level, where
1: L (7%)
2: M (15%)
3: Q (25%)
4: H (30%)
data
- the QRCode data. The data must be between 1 and 448 symbols
long.java.io.IOException
- if an I/O error occurs.public void printQRCode(int size, int eccLv, java.lang.String data) throws java.io.IOException
size
- the size of symbol, value in {0, 1, 4, 6, 8, 10, 12, 14}.eccLv
- the error collection control level, where
1: L (7%)
2: M (15%)
3: Q (25%)
4: H (30%)
data
- the QRCode data. The data must be between 1 and 448 symbols
long.java.io.IOException
- if an I/O error occurs.public void setQRCode(int cellSize) throws java.io.IOException
cellSize
- the cell size.java.io.IOException
- if an I/O error occurs.public void calibrateBMMSensor() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public void feedLabel() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public void selectPageMode() throws java.io.IOException
In page mode the result of incoming commands is forwarded to a reserved
memory area (page). The page place, size and print direction is defined
using setPageRegion(int, int, int, int, int)
. At the end the
collected information is printed using printPage()
. Almost all
commands works in page mode. The centering and right alignment is working
in the currently defined page width.
java.io.IOException
- if an I/O error occurs.setPageRegion(int, int, int, int, int)
,
selectStandardMode()
public void selectStandardMode() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public void printPage() throws java.io.IOException
The command checks the whole memory of the page mode. The area from the first horizontal line wit at least 1 black dot to the last such line is printed.
java.io.IOException
- if an I/O error occurs.selectPageMode()
public void setPageRegion(int x, int y, int width, int height, int direction) throws java.io.IOException
If x
is negative, or y
is negative, or
width
is negative, or height
is negative, or
direction
is not a valid value, then
IllegalArgumentException
is thrown.
x
- the horizontal offset of the relative top left corner of the
page.y
- the vertical offset of the relative top left corner of the page.width
- the width of the page.height
- the height of the page.direction
- the print direction of the page. See PAGE_
*
constant field values.java.io.IOException
- if an I/O error occurs.selectPageMode()
public void setPageXY(int x, int y) throws java.io.IOException
If the position is outside the current page region, the command is not accepted. The real new coordinates depend on the print direction.
If x
is negative, or y
is negative, then
IllegalArgumentException
is thrown.
x
- the horizontal position.y
- the vertical position.java.io.IOException
- if an I/O error occurs.selectPageMode()
public void drawPageRectangle(int x, int y, int width, int height, int fillMode) throws java.io.IOException
If x
is negative, or y
is negative, or
width
is negative, or height
is negative, or
fillMode
is not a valid value, then
IllegalArgumentException
is thrown.
x
- the horizontal position of the top left corner of the rectangle
in the page region.y
- the vertical position of the top left corner of the rectangle in
the the page region.width
- the width of the rectangle.height
- the height of the rectangle.fillMode
- the fill mode used to draw the rectangle. See the
FILL_
* constant field values.java.io.IOException
- if an I/O error occurs.selectPageMode()
public void drawPageFrame(int x, int y, int width, int height, int fillMode, int thickness) throws java.io.IOException
If x
is negative, or y
is negative, or
width
is negative, or height
is negative, or
fillMode
is not a valid value, or thickness
is
negative, then IllegalArgumentException
is thrown.
x
- the horizontal position of the top left corner of the rectangle
frame in the page region.y
- the vertical position of the top left corner of the rectangle
frame in the the page region.width
- the width of the rectangle frame.height
- the height of the rectangle frame.fillMode
- the fill mode used to draw the rectangle frame. See the
FILL_
* constant field values.thickness
- the frame thickness.java.io.IOException
- if an I/O error occurs.selectPageMode()
public java.lang.String[] getSettings(int type) throws java.io.IOException
type
- the printer settings type. It can be one of the SETTINGS_* constant values.java.io.IOException
- if an I/O error occurs.public void setIntensity(int level) throws java.io.IOException
level
- the intensity leveljava.io.IOException
- if an I/O error occurs.public TouchChip getTouchChip()
public void journalFormat() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public void journalWrite(byte[] data) throws java.io.IOException
data
- the data, up to 400 bytes long.java.io.IOException
- if an I/O error occurs.public void journalWrite(java.lang.String text, java.lang.String charset) throws java.io.IOException
text
- the text, up to 400 symbols long.charset
- the text charset.java.io.IOException
- if an I/O error occurs.public void journalPrint() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public byte[] journalRead() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public java.lang.String journalRead(java.lang.String charset) throws java.io.IOException
charset
- the text charset.java.io.IOException
- if an I/O error occurs.public void cutPaper(int m, int n) throws java.io.IOException
m
- Specifies cut mode. Possible values are:
n
- a value used in depends of 'm' parameter.java.io.IOException
- if an I/O error occurs.public void cutPaper() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public void setPresenterActiveMode(int n) throws java.io.IOException
n
- The mode, a value from 0 to 7.
The default is 7
The automatic release mode enables to release paper automatically, synchronizing with cutter action.
The automatic retractive mode enables to retract paper automatically synchronizing with the time-out.
java.io.IOException
- if an I/O error occurs.public void setPresenterOpMode(int n) throws java.io.IOException
n
- Manual retractive operation, a value from 0 to 255 (xxxxxxx0B)
The command is enabled to function when the operation mode is valid.
java.io.IOException
- if an I/O error occurs.public void setRetractiveModeTimeout(int timeout) throws java.io.IOException
timeout
- The timeout in seconds, a value from 0 to 61. If value is 0 the
auto retractive is off.java.io.IOException
- if an I/O error occurs.