1

I have requirement to convert ZPL code to an Image in Java. But I cannot use Labelary to do this job as its a licensed one. Can somebody please help me with the approach.

3
  • Can somebody help me on this. Pls Commented Aug 9, 2018 at 9:21
  • some problem here.. Commented Mar 6, 2019 at 10:28
  • got any solution? Commented Jan 22, 2020 at 7:45

2 Answers 2

-1

You must add the Zebra SDK for Java SDK LINK-OS. Once the library is imported into the project along with the DLL files, you can use this code:

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

But also make sure to import the library.

import com.zebra.sdk.graphics.internal.PngToGrfConverterDesktop;
import com.zebra.sdk.device.ZebraIllegalArgumentException;

With this, you will have a result that you can test on this URL by placing the code between

^XA
 {code_return_function}
^XZ
Sign up to request clarification or add additional context in comments.

Comments

-1

You could use the ZPL documentation from Zebra for getting started.
But there are several java libraries out there for parsing ZPL code. Pick one the fits your needs best.

1 Comment

Could you shared the Java libraries and document how to use them? I'd appreciate it, and your answer would be complete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.