我正在尝试使用zpl语言在zebra打印机(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));我仍然不能打印正确的图像。我遵循下面的网址(http://labelary.com/viewer.html),并试图在我们上传图片时看到代码。我发现在上传图片后,zebra查看器生成的base64与我使用上面的代码生成的完全不同。我在stackoverflow上发布了几篇文章,但我仍然不能解决这个问题。
我知道我在哪里做错了,但我不知道如何解决它。实际上,我不能为给定的图像生成ASCII十六进制Base64代码。这是我的想法。
任何帮助都是有价值的。
谢谢,
发布于 2016-05-26 21:49:55
试一下这个函数:
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上。
https://stackoverflow.com/questions/37434525
复制相似问题