在我们的Android应用程序中,我们可以成功将图像打印到P4T打印机。我们使用pcx cpcl命令与其他收据文本一起打印图像。在打印收据之前,我们使用zebra sdk将图像上传到打印机内存。我们有zebra首先将我们的位图转换为ZebraImage然后上传它。在P4T上,这会产生一个.PCX文件,然后我们在cpcl标签中引用它。例如:
打印机配置:
E:signature.pcx
在Android应用程序中:
static private void sendImagesToPrinter(DevicePrinter devicePrinter, List<String> imagesToSend, String rootPath) throws IOException, ConnectionException, ZebraPrinterLanguageUnknownException, ZebraIllegalArgumentException {
for(String image:imagesToSend) {
//[0] -> image path, <[1]> -> image scale factor
String[] imageParams = image.split("\\|");
double scaleFactor = imageParams.length > 1 ? parseImageScale(imageParams[1]) : 1.0d;
File file = new File(StringUtils.pathCombine(rootPath,imageParams[0]));
if(!file.exists())
throw new FileNotFoundException("Image file not found " + file.getName());
ZebraImageI zebraImage = ZebraImageFactory.getImage(BitmapFactory.decodeFile(file.getAbsolutePath()));
devicePrinter.storeImage("E:" + file.getName(), zebraImage, (int)(zebraImage.getWidth() * scaleFactor), (int)(zebraImage.getHeight() * scaleFactor));
}
}
public void storeImage(String printerFullPath, ZebraImageI zebraImage, int width, int height) throws ConnectionException, ZebraPrinterLanguageUnknownException, IOException, ZebraIllegalArgumentException {
Connection connection = null;
try {
connection = ConnectionBuilder.build(connectionString);
connection.open();
ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);
printer.storeImage(printerFullPath, zebraImage, width, height);
}
finally {
if(connection != null)
connection.close();
}
}
cpcl格式文件:
PCX 10 280 !<signature.pcx
我们设置缩放参数来管理打印机存储的图像大小。这样可以在P4T打印机上打印图像,但是我们有一个QLn420可以打印图像的长字符串表示。
收据的其他文字部分通常在这些设备上打印。
任何人遇到此问题并知道如何修复它?
*编辑 我还尝试使用以下代码直接将斑马图像打印到打印机。无论哪个,我总是得到字符串(图像的base64表示。)
public void printImage(ZebraImageI zebraImage, int width, int height) throws ConnectionException, ZebraPrinterLanguageUnknownException, ZebraIllegalArgumentException {
Connection connection = null;
try {
connection = ConnectionBuilder.build(connectionString);
connection.open();
ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);
printer.printImage(zebraImage, 0, 0, width, height, false);
}
finally {
if(connection != null)
connection.close();
}
}
**编辑 在Qln420上,永远不会存储图像。我希望它在调用storeImage()后显示在“E:sigfile.pcx”但它永远不会保存。不知道为什么。
答案 0 :(得分:2)
我们的修复是在创建用于存储图像的新打印机时明确设置打印机语言:
public void storeImage(String printerFullPath, ZebraImageI zebraImage, int width, int height, PrinterLanguage printerLanguage) throws ConnectionException, ZebraPrinterLanguageUnknownException, IOException, ZebraIllegalArgumentException {
Connection connection = null;
try {
connection = ConnectionBuilder.build(connectionString);
connection.open();
ZebraPrinter printer = ZebraPrinterFactory.getInstance(printerLanguage, connection);
printer.storeImage(printerFullPath, zebraImage, width, height);
}
finally {
if(connection != null)
connection.close();
}
}
此外,我们发现删除图片的文件扩展名对一台设备很有帮助,因此我们现在就删除所有文件扩展名。 因此,对于我们来说,打印带有内嵌图像的CPCL格式收据适用于P4T,QLn420和ZQ520。
此外,我们发现在存储图像之前缩放图像也是必要的,因为大图像存储将失败而不会抛出异常。
ZebraImageI zebraImage = ZebraImageFactory.getImage(BitmapFactory.decodeFile(file.getAbsolutePath()));
double scaleFactor = (printedImageWidth == null) ? 1.0 : (double)printedImageWidth / zebraImage.getWidth();
//strip off the extension
String fileName = file.getName().split("\\.")[0];
devicePrinter.storeImage(fileName, zebraImage, (int)(zebraImage.getWidth() * scaleFactor), (int)(zebraImage.getHeight() * scaleFactor), printerLanguage);