0

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

MatPag
  • 41,742
  • 14
  • 105
  • 114

3 Answers3

2

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();
MatPag
  • 41,742
  • 14
  • 105
  • 114
1

use storeimage to store the image on the printer. Then send pritner commands to print the image with a print quantitiy of three. It your printer is using zpl it would look something like "^xa^xgR:image.grf^fs^pq3^xz"

You will want to look at the ZPL guide to be sure but that is the general solution. Store the image and then recall it. In the end delete the image or just always use the same file name and the image will just write over the last image.

banno
  • 1,529
  • 8
  • 12
  • The problem is converting the JPEG to GRF with Java code, because there is no native command to print a stored JPEG or PNG, i will try something i found here: http://stackoverflow.com/questions/21905693/convert-image-to-grf-format – MatPag Oct 23 '15 at 08:45
  • Really thanks, i've found this link too https://km.zebra.com/kb/index?page=content&id=SO8881&actp=LIST on the same argument. I will try in the next days when i have time to work on it. – MatPag Oct 28 '15 at 10:19
  • In practice ~DY command replaces storeImage function right? then i've to print it with another command similar to the one you posted in the answer – MatPag Oct 28 '15 at 10:54
  • i tried everything and there is no way to store my PNG and print it. I think the biggest problem is the non monochrome bitmap and the low memory available in the printer which is 4MB for my model. I will try converting the Bitmap to black&white and check if something change – MatPag Nov 06 '15 at 08:15
  • That is the best way anyways as you can control the dithering algorithm. Get it down to one bit per pixel and go. – banno Nov 07 '15 at 13:07
  • i've tried with the B&W bitmap but seems that can't be stored on the printer, i tried to save it on E: and R: but the result is the same :( – MatPag Nov 12 '15 at 10:10
  • i've found a solution, i will post a complete solution when i've finished the work, because in the entire WEB there is no a complete guide to follow. – MatPag Nov 17 '15 at 14:51
  • i've added the answer with 2 years of delay XD, btw thank you for your help :) – MatPag Jun 08 '17 at 07:39
1

I have Modified @MatPag answer and added support for multiple printers of zebra. Follow these steps:

  • Turn Off Zebra printer
  • press feed + power button (release power button when power bars appear but release feed button only when printer prints something)
  • You will receive a print after sometime which have feild like this:

Label:

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

Now Use Below Function to print out your zebra print without extra spaces:

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

    }

Above function is used like this in my case: printPhotoFromExternalManual(bitmap,1,576);

Enjoy happing coding

Zulqurnain Jutt
  • 1,077
  • 1
  • 9
  • 21