1

我正在尝试使用 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 代码。这是我的想法。

任何帮助都会得到帮助。

谢谢,

4

1 回答 1

0

试试这个功能:

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;
}

我基于2012 年的这个答案

于 2016-05-26T13:49:55.407 回答