我需要将 ZPL 代码转换为 Java 中的图像。但我不能使用
Labelary
来完成这项工作,因为它是经过许可的。
有人可以帮我解决这个问题吗?
您可以使用 Zebra 的 ZPL 文档 开始使用。
但是有几个 Java 库可用于解析 ZPL 代码。图一最适合您的需求。
您必须添加 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;
有了这个,您将获得一个结果,您可以通过将代码放置在之间来测试此URL
^XA
{code_return_function}
^XZ