1
votes

I am trying to print image on zebra printer(LP-2844-Z) using zpl language. In ZPL documentaion it says you have to convert image into ASCII HexaDecimal. Then by using GF command you can print image. I tried below code to get image and convert it into hexa decimal

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

And then I tried to print this data using zebra printer but it is not printing correct image. I tried another code to get image bytes and converted it into hexa decimal

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

Still I am not able to print correct image. I follow following URL (http://labelary.com/viewer.html) and tried to see code when we upload image. What I found that after uploading image, the base64 generated by zebra viewer is totally different which I generated using above code. I go through several post on stackoverflow but still I am not able to solve out this problem.

I know where I am doing mistake but I don't know how to solve it. Actually I am not able to generate ASCII Hex Base64 code for given image. This is my thought.

Any help is appriciated.

Thanks,

1
You can take a look at zebra-toolkit. It's in Objective-C but it's pretty easy to read. In the file UIImage+HexRepresentation.m you will see a method for creating the zpl ascii-hex string representation of a given image.Diego

1 Answers

0
votes

Give this function a try:

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

I based it upon this answer from 2012.