public interface BarcodeUtil
package test.com.zebra.sdk.job.examples;
import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.comm.TcpConnection;
import com.zebra.sdk.common.card.containers.GraphicsInfo;
import com.zebra.sdk.common.card.containers.JobStatusInfo;
import com.zebra.sdk.common.card.enumerations.CardSide;
import com.zebra.sdk.common.card.enumerations.GraphicType;
import com.zebra.sdk.common.card.enumerations.OrientationType;
import com.zebra.sdk.common.card.enumerations.PrintType;
import com.zebra.sdk.common.card.exceptions.ZebraCardException;
import com.zebra.sdk.common.card.graphics.ZebraCardGraphics;
import com.zebra.sdk.common.card.graphics.ZebraCardImage;
import com.zebra.sdk.common.card.graphics.ZebraCardImageI;
import com.zebra.sdk.common.card.graphics.ZebraGraphics;
import com.zebra.sdk.common.card.graphics.barcode.CodeQRUtil;
import com.zebra.sdk.common.card.graphics.barcode.ZebraBarcodeFactory;
import com.zebra.sdk.common.card.graphics.barcode.enumerations.Rotation;
import com.zebra.sdk.common.card.jobSettings.ZebraCardJobSettingNames;
import com.zebra.sdk.common.card.printer.ZebraCardPrinter;
import com.zebra.sdk.common.card.printer.ZebraCardPrinterFactory;
import com.zebra.sdk.device.ZebraIllegalArgumentException;
public class PrintBarcodeExample {
public static void main(String[] args) {
Connection connection = null;
ZebraCardPrinter zebraCardPrinter = null;
ZebraGraphics graphics = null;
try {
connection = new TcpConnection("1.2.3.4", 9100);
connection.open();
zebraCardPrinter = ZebraCardPrinterFactory.getInstance(connection);
if (zebraCardPrinter == null) {
return;
}
graphics = new ZebraCardGraphics(zebraCardPrinter);
graphics.initialize(0, 0, OrientationType.Landscape, PrintType.MonoK, Color.WHITE);
List<GraphicsInfo> graphicsData = new ArrayList<GraphicsInfo>();
drawBarcodeImage(graphics, graphicsData);
zebraCardPrinter.setJobSetting(ZebraCardJobSettingNames.K_OPTIMIZATION_FRONT, "Barcode");
int jobId = zebraCardPrinter.print(1, graphicsData);
boolean bSuccess = pollJobStatus(zebraCardPrinter, jobId);
if (!bSuccess) {
zebraCardPrinter.cancel(jobId);
}
JobStatusInfo jStatus = zebraCardPrinter.getJobStatus(jobId);
System.out.println("Job complete: " + jStatus.printStatus);
} catch (Exception e) {
e.printStackTrace();
} finally {
cleanUp(connection, zebraCardPrinter, graphics);
}
}
private static void drawBarcodeImage(ZebraGraphics graphics, List<GraphicsInfo> graphicsData) throws IOException {
CodeQRUtil barcodeUtil = ZebraBarcodeFactory.getQRCode(graphics);
barcodeUtil.drawBarcode("https://www.zebra.com", 50, 50, Rotation.ROTATE_0);
ZebraCardImageI image = graphics.createImage();
graphicsData.add(addBasicImage(CardSide.Front, PrintType.MonoK, 0, 0, -1, image.getImageData()));
}
private static GraphicsInfo addBasicImage(CardSide side, PrintType printType, int xOffset, int yOffset, int fillColor, byte[] imageData) {
GraphicsInfo graphicsInfo = new GraphicsInfo();
graphicsInfo.fillColor = fillColor;
graphicsInfo.graphicData = imageData != null ? new ZebraCardImage(imageData) : null;
graphicsInfo.graphicType = imageData != null ? GraphicType.BMP : GraphicType.NA;
graphicsInfo.opacity = 0;
graphicsInfo.overprint = false;
graphicsInfo.printType = printType;
graphicsInfo.side = side;
graphicsInfo.xOffset = xOffset;
graphicsInfo.yOffset = yOffset;
return graphicsInfo;
}
private static boolean pollJobStatus(ZebraCardPrinter device, int actionID) throws ConnectionException, ZebraCardException, ZebraIllegalArgumentException {
boolean success = false;
long dropDeadTime = System.currentTimeMillis() + 40000;
long pollInterval = 500;
// Poll job status
JobStatusInfo jStatus = null;
do {
jStatus = device.getJobStatus(actionID);
System.out.println(String.format("Job %d, Status:%s, Card Position:%s, " + "ReadyForNextJob:%s, Mag Status:%s, Contact Status:%s, Contactless Status:%s, Error Code:%d, Alarm Code:%d", actionID, jStatus.printStatus, jStatus.cardPosition, jStatus.readyForNextJob,
jStatus.magneticEncoding, jStatus.contactSmartCard, jStatus.contactlessSmartCard, jStatus.errorInfo.value, jStatus.alarmInfo.value));
if (jStatus.contactSmartCard.contains("station")) {
success = true;
break;
} else if (jStatus.contactlessSmartCard.contains("station")) {
success = true;
break;
} else if (jStatus.printStatus.contains("done_ok")) {
success = true;
break;
} else if (jStatus.printStatus.contains("alarm_handling")) {
System.out.println("Alarm Dectected: " + jStatus.alarmInfo.description);
success = false;
} else if (jStatus.printStatus.contains("error") || jStatus.printStatus.contains("cancelled")) {
success = false;
break;
}
if (System.currentTimeMillis() > dropDeadTime) {
success = false;
break;
}
try {
Thread.sleep(pollInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (true);
return success;
}
private static void cleanUp(Connection connection, ZebraCardPrinter genericPrinter, ZebraGraphics graphics) {
if (graphics != null) {
graphics.close();
graphics = null;
}
try {
if (genericPrinter != null) {
genericPrinter.destroy();
genericPrinter = null;
}
} catch (ZebraCardException e) {
e.printStackTrace();
}
if (connection != null) {
try {
connection.close();
connection = null;
} catch (ConnectionException e) {
e.printStackTrace();
}
}
}
}
Modifier and Type | Method and Description |
---|---|
void |
drawBarcode(String data,
int x,
int y,
int width,
int height,
Rotation rotation)
Draws the selected barcode symbology with the specified width and height at the specified x and y coordinates.
|
android.graphics.Typeface |
getFont()
Gets the font of the human-readable part.
|
int |
getMargin()
Gets the current margin value.
|
float |
getTextSize()
Gets the text size for the human-readable part.
|
void |
setCharacterSet(String characterSet)
Sets the character encoding type.
|
void |
setFont(android.graphics.Typeface font)
Sets the font of the human-readable part.
|
void |
setMargin(int margin)
Sets the margin value.
|
void |
setMessagePosition(HumanReadablePlacement placement)
Sets the placement of the human-readable part.
|
void |
setTextSize(float textSize)
Sets the text size for the human-readable part.
|
void drawBarcode(String data, int x, int y, int width, int height, Rotation rotation) throws IllegalArgumentException, ZebraCardException
data
- Data to encode.x
- The x coordinate.y
- The y coordinate.width
- The barcode width.height
- The barcode height.rotation
- The barcode rotation.IllegalArgumentException
- If the data is not valid for the specified barcode type.ZebraCardException
- If an error occurs while drawing the barcode.android.graphics.Typeface getFont()
void setFont(android.graphics.Typeface font)
font
- The font type.float getTextSize()
void setTextSize(float textSize)
textSize
- The size of the text.void setMessagePosition(HumanReadablePlacement placement)
placement
- The placement of the human-readable part.void setCharacterSet(String characterSet)
characterSet
- The character encoding type (e.g., UTF-8).void setMargin(int margin)
margin
- The margin value in pixels.int getMargin()
© 2017 ZIH Corp. All Rights Reserved.