Zebra RW420 Android SDK 打印多份避免循环

Zebra RW420 Android SDK print multiple copies avoiding a loop

我使用下面的代码通过 Android SDK 打印位图,可以在 link 找到: 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能达到同样的效果吗?

谢谢 垫子

使用storeimage 将图像存储在打印机上。然后发送打印命令打印图像,打印数量为三。如果您的打印机正在使用 zpl,它看起来像 "^xa^xgR:image.grf^fs^pq3^xz"

您需要查看 ZPL 指南以确保确定,但这是通用的解决方案。存储图像,然后调用它。最后删除图像或始终使用相同的文件名,图像将覆盖最后一张图像。

最后我是这样解决的

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

我修改了@MatPag 的回答并添加了对多台斑马打印机的支持。 请按照以下步骤操作:

  • 关闭 Zebra 打印机
  • 按下进纸键+电源键(出现电源条时松开电源键,打印机打印完成后松开进纸键)
  • 您将在一段时间后收到打印件,内容如下:

Label:

Width: 576 dots <------- this is width in px

现在使用下面的函数打印出没有多余空格的斑马纹:

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

享受快乐编码