Search code examples
androidprintingzebra-printers

Zebra RW420 Android SDK print multiple copies avoiding a loop


I'm using the code below to print a bitmap with the Android SDK which can be found at this 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();

The problem is: the printing process is taking a lot of time because the bitmap is large.
Is there a way to avoid a loop and tell to the printer how many quantity to print without calling printImage multiple times?

i've searched a lot in the documentation but i've not found something usefull, is there a way to achieve this? With CPCL can i achieve the same effect?

Thanks Mat


Solution

  • This how i solved it at the end

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