我正在使用下面的代码打印Android SDK的位图,可以在此链接中找到: https://www.zebra.com/us/en/products/software/barcode-printers/link-os/link-os-sdk.html#mainpartabscontainer_794f=downloads
//variables
int printQty= 3;
String printerAddress= ...;
Connection connection = new BluetoothConnection(printerAddress);
connection.open();
//for removing the useless margin printed
connection.write("! U1 JOURNAL\r\n! U1 SETFF 100 2\r\n".getBytes());
ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);
Bitmap bitmapToPrint = large bitmap here;
ZebraImageAndroid zebraImageToPrint = new ZebraImageAndroid(bitmapToPrint);
for (int i = 0; i < printQty; i++){
printer.printImage(zebraImageToPrint, 0, 0, -1, -1, false);
}
bitmapToPrint.recycle();
connection.close();
问题是: 由于位图很大,打印过程需要花费很多时间 有没有办法避免循环并告诉打印机多少次打印而不多次调用 printImage ?
我在文档中搜索了很多但是我没有找到有用的东西,有没有办法实现这个目的?用CPCL可以达到同样的效果吗?
由于 垫
答案 0 :(得分:1)
使用storeimage将图像存储在打印机上。然后发送pritner命令以打印图像打印量为3。你的打印机使用zpl它看起来像&#34; ^ xa ^ xgR:image.grf ^ fs ^ pq3 ^ xz&#34;
您需要查看ZPL指南以确定,但这是一般解决方案。存储图像然后调用它。最后删除图像或只使用相同的文件名,图像只会写在最后一张图像上。
答案 1 :(得分:0)
这是我在最后解决的问题
int printQty = 5; //or whatever number you want
Coonection connection = new BluetoothConnection(deviceAddress);
connection.open();
//with SETFF command we add 50 millimiters of margin at the end of the the print (without this the print is wasting a lot of paper)
//ref https://km.zebra.com/kb/index?page=forums&topic=021407fb4efb3012e55595f77007e8a
connection.write("! U1 JOURNAL\r\n! U1 SETFF 50 2\r\n".getBytes());
ZebraPrinter printer = ZebraPrinterFactory.getInstance(PrinterLanguage.CPCL, connection);
//get the bitmap to print
bitmapToPrint = PdfUtils.pdfToBitmap(getApplicationContext(), pdfStamped, 0);
int width = bitmapToPrint.getWidth();
int height = bitmapToPrint.getHeight();
float aspectRatio = width / ZEBRA_RW420_WIDTH; //ZEBRA_RW420_WIDTH = 800f
float multiplier = 1 / aspectRatio;
//scale the bitmap to fit the ZEBRA_RW_420_WIDTH print
bitmapToPrint = Bitmap.createScaledBitmap(bitmapToPrint, (int)(width * multiplier), (int)(height * multiplier), false);
//get the new bitmap and add 20 pixel more of margin
int newBitmapHeight = bitmapToPrint.getHeight() + 20;
//create the Zebra object with the new Bitmap
ZebraImageAndroid zebraImageToPrint = new ZebraImageAndroid(bitmapToPrint);
//the image is sent to the printer and stored in R: folder
printer.storeImage("R:TEMP.PCX", zebraImageToPrint, -1, -1);
//create the print commands string
String printString =
"! 0 200 200 " + newBitmapHeight + " " + printQty + "\r\n"//set the height of the bitmap and the quantity to print
+ "PW 831" + "\r\n"//MAX_PRINT_WIDTH
+ "TONE 50" + "\r\n"//print intensity tone 0-200
+ "SPEED 2" + "\r\n"//print speed (less = more accurate) 1 2.5cm/s | 2 - 5cm/s | 3 - 7.6cm/s
+ "ON-FEED REPRINT" + "\r\n"//enable reprint on FEED button press
+ "NO-PACE" + "\r\n"
+ "BAR-SENSE" + "\r\n"
+ "PCX 20 20 !<TEMP.PCX" + "\r\n"//get the image we stored before in the printer
+ "FORM" + "\r\n"
+ "PRINT" + "\r\n";//print
//send the commands to the printer, the image will be printed now
connection.write(printString.getBytes());
//delete the image at the end to prevent printer memory sutaration
connection.write("! U1 do \"file.delete\" \"R:TEMP.PCX\"\r\n".getBytes());
//close the connection with the printer
connection.close();
//recycle the bitmap
bitmapToPrint.recycle();
答案 2 :(得分:0)
我修改了@MatPag答案,并增加了对多台斑马打印机的支持。 请按照下列步骤操作:
标签:
宽度:576个点<-------这是以像素为单位的宽度
现在使用“使用以下功能”可以打印出斑马纹,而无需多余的空格:
private void printPhotoFromExternalManual(final Bitmap bitmapToPrint,final int printingQty, final int widthSupported) {
new Thread(new Runnable() {
public void run() {
try {
getAndSaveSettings();
Looper.prepare();
helper.showLoadingDialog("Sending image to printer");
Connection connection = getZebraPrinterConn();
int printQty = printingQty; //or whatever number you want
connection.open();
//with SETFF command we add 50 millimiters of margin at the end of the the print (without this the print is wasting a lot of paper)
//ref https://km.zebra.com/kb/index?page=forums&topic=021407fb4efb3012e55595f77007e8a
// connection.write("! U1 setvar \"device.languages\" \"CPCL\"\r\n".getBytes());
connection.write("! U1 JOURNAL\r\n! U1 SETFF 50 2\r\n".getBytes());
ZebraPrinter printer = ZebraPrinterFactory.getInstance(PrinterLanguage.CPCL, connection);
float ZEBRA_WIDTH = widthSupported;
int width = bitmapToPrint.getWidth();
int height = bitmapToPrint.getHeight();
float aspectRatio = width / ZEBRA_WIDTH; //ZEBRA_RW420_WIDTH = 800f
float multiplier = 1 / aspectRatio;
//scale the bitmap to fit the ZEBRA_RW_420_WIDTH print
Bitmap bitmapToPrint1 = Bitmap.createScaledBitmap(bitmapToPrint, (int) (width * multiplier), (int) (height * multiplier), false);
bitmapToPrint.recycle();
//get the new bitmap and add 20 pixel more of margin
int newBitmapHeight = bitmapToPrint1.getHeight() + 20;
//create the Zebra object with the new Bitmap
ZebraImageAndroid zebraImageToPrint = new ZebraImageAndroid(bitmapToPrint1);
//the image is sent to the printer and stored in R: folder
printer.storeImage("R:TEMP.PCX", zebraImageToPrint, -1, -1);
//create the print commands string
String printString =
"! 0 200 200 " + newBitmapHeight + " " + printQty + "\r\n"//set the height of the bitmap and the quantity to print
+ "PW " + ((int) ZEBRA_WIDTH) + "\r\n"//MAX_PRINT_WIDTH
+ "TONE 50" + "\r\n"//print intensity tone 0-200
+ "SPEED 2" + "\r\n"//print speed (less = more accurate) 1 2.5cm/s | 2 - 5cm/s | 3 - 7.6cm/s
+ "ON-FEED REPRINT" + "\r\n"//enable reprint on FEED button press
+ "NO-PACE" + "\r\n"
+ "BAR-SENSE" + "\r\n"
+ "PCX 20 20 !<TEMP.PCX" + "\r\n"//get the image we stored before in the printer
+ "FORM" + "\r\n"
+ "PRINT" + "\r\n";//print
//send the commands to the printer, the image will be printed now
connection.write(printString.getBytes());
//delete the image at the end to prevent printer memory sutaration
connection.write(("! U1 do \"file.delete\" \"R:TEMP.PCX\"\r\n").getBytes());
//close the connection with the printer
connection.close();
//recycle the bitmap
bitmapToPrint1.recycle();
if (file != null) {
file.delete();
file = null;
}
} catch (ConnectionException e) {
helper.showErrorDialogOnGuiThread(e.getMessage());
} catch (ZebraIllegalArgumentException e) {
helper.showErrorDialogOnGuiThread(e.getMessage());
} finally {
helper.dismissLoadingDialog();
Looper.myLooper().quit();
}
}
}).start();
}
在我的情况下,上面的函数是这样使用的:printPhotoFromExternalManual(bitmap,1,576);
享受加盖编码