我想创建一个 ZPL2 代码,用于打印给定的 PNG 图像。我尝试过例如Labelary Online (http://labelary.com),但我无法创建正确的图像编码。我将 PNG 转换为黑白图像,将字节数组转换为十六进制字符串,然后像这样插入:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bwImage, "png", baos);
byte[] bytes = baos.toByteArray();
System.out.println("^XA^GFA," + bytes.length + "," + bytes.length + "," + bytes.length / 8 + "," + getHexString(bytes) + "^XZ");
这适用于标签在线预览,但不适用于真正的打印机。第二点是,当我在标签网站上插入图像时,十六进制字符串看起来与我的有很大不同。 (它甚至不是十六进制字符串)。
我使用 http://labelary.com 作为内联图像转换器。我使用“添加图像”,然后选择图像数据,并将其保存到 *.grf 文件中。
如果您不想使用 Labelary,还可以使用 Zebra Setup Utilities,并将图像转换为 *.grf,按照本文操作:https://km.zebra.com/kb/index?page=内容&id=SO7892.
这是使用 Zebra SDK for Java SDK LINK-OS 的另一种方法。将库与 DLL 文件一起导入项目后,您可以使用以下代码:
public static String generateZPLCode(String imageFilePath, int newWidth, int newHeight, String x_coordinate, String y_coordinate) throws IOException, ZebraIllegalArgumentException {
// Load the image
BufferedImage image = ImageIO.read(new File(imageFilePath));
image = resize(image, newWidth, newHeight);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
InputStream in = new ByteArrayInputStream(baos.toByteArray());
byte[] grfBytes = PngToGrfConverterDesktop.pngToGrf(in);
String hexData = Hex.encodeHexString(grfBytes);
int totalBytes = grfBytes.length;
int bytesPerRow = totalBytes / image.getHeight();
StringBuilder zplCommand = new StringBuilder();
zplCommand.append("^F");
zplCommand.append(x_coordinate);
zplCommand.append(",");
zplCommand.append(y_coordinate);
String comand_image = "^GFA," + totalBytes + "," + totalBytes + "," + bytesPerRow + "," + hexData;
zplCommand.append(comand_image);
return zplCommand.toString();
}
private static BufferedImage resize(BufferedImage img, int newWidth, int newHeight) {
BufferedImage resized = new BufferedImage(newWidth, newHeight, img.getType());
AffineTransform at = new AffineTransform();
at.scale((double) newWidth / img.getWidth(), (double) newHeight / img.getHeight());
AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
resized = scaleOp.filter(img, resized);
return resized;
}
但还要确保导入库。
import com.zebra.sdk.graphics.internal.PngToGrfConverterDesktop;
import com.zebra.sdk.device.ZebraIllegalArgumentException;
有了这个,您将获得一个结果,您可以通过将代码放置在之间来测试此URL
^XA
{code_return_function}
^XZ