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