【问题标题】:Image printing with zebra printer using zpl command使用 zpl 命令使用斑马打印机打印图像
【发布时间】:2016-09-22 21:41:25
【问题描述】:

我正在尝试使用 zpl 语言在斑马打印机 (LP-2844-Z) 上打印图像。在 ZPL 文档中,它说您必须将图像转换为 ASCII HexaDecimal。然后使用 GF 命令可以打印图像。 我尝试下面的代码来获取图像并将其转换为十六进制

URL oracle = new URL(urlString);
URLConnection yc = oracle.openConnection();
BufferedImage bufferedImage = ImageIO.read(yc.getInputStream());
int rowsData = bufferedImage.getWidth()/8;
System.out.println(rowsData);
byte[] pixel = ((DataBufferByte)bufferedImage.getRaster().getDataBuffer()).getData();
System.out.println(pixel.length);
System.out.println(Hex.encodeHex(pixel, false));

然后我尝试使用斑马打印机打印这些数据,但它没有打印正确的图像。我尝试了另一个代码来获取图像字节并将其转换为十六进制

URL oracle = new URL(urlString);
URLConnection yc = oracle.openConnection();
InputStream inputStream = yc.getInputStream();
byte[] imageData = IOUtils.toByteArray(inputStream);
System.out.println(Hex.encodeHex(pixel, false));

我仍然无法打印正确的图像。我遵循以下 URL (http://labelary.com/viewer.html) 并在我们上传图片时尝试查看代码。我发现上传图片后,斑马查看器生成的base64与我使用上面的代码生成的完全不同。我浏览了几篇关于 stackoverflow 的帖子,但仍然无法解决这个问题。

我知道我在哪里做错了,但我不知道如何解决它。实际上我无法为给定的图像生成 ASCII Hex Base64 代码。这是我的想法。

如有任何帮助,我们将不胜感激。

谢谢,

【问题讨论】:

  • 你可以看看zebra-toolkit。它在 Objective-C 中,但很容易阅读。在文件 UIImage+HexRepresentation.m 中,您将看到创建给定图像的 zpl ascii-hex 字符串表示的方法。

标签: java jakarta-ee zebra-printers zpl zpl-ii


【解决方案1】:

试试这个功能:

private static String zplEncode(String graphicFileLocation) {
    Path imagePath = Paths.get(URI.create("file://" + graphicFileLocation));
    String returnResults = "";
    try {
    byte[] binaryData = Files.readAllBytes(imagePath);
    for (byte b : binaryData)
    {
        String hexRep = String.format("{0:X}", b);
        if (hexRep.length() == 1)
            hexRep = "0" + hexRep;
        returnResults += hexRep;
      }
    } catch (IOException ex) {
        // Do something here
    }
    return returnResults;
}

我基于this answer from 2012

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多