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.
-
Can somebody help me on this. Plsuser3440451– user34404512018-08-09 09:21:10 +00:00Commented Aug 9, 2018 at 9:21
-
some problem here..Chris– Chris2019-03-06 10:28:12 +00:00Commented Mar 6, 2019 at 10:28
-
got any solution?Mithun Sarker– Mithun Sarker2020-01-22 07:45:58 +00:00Commented Jan 22, 2020 at 7:45
Add a comment
|
2 Answers
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
Comments
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
aSemy
Could you shared the Java libraries and document how to use them? I'd appreciate it, and your answer would be complete.