Search code examples
androidzebra-printers

Zebra Android Printing with v2.8.2148 images print as string instead of pixels


In our android app, we can successfully print images to a P4T printer. We use the pcx cpcl command to print an image inline with other receipt text. Just prior to printing the receipt we upload the image to the printer memory using the zebra sdk. We have zebra first convert our bitmap to a ZebraImage and then upload it. On the P4T this results in a .PCX file that we then reference in our cpcl label. Example:

printer config:

E:signature.pcx

in the android app:

static private void sendImagesToPrinter(DevicePrinter devicePrinter, List<String> imagesToSend, String rootPath) throws IOException, ConnectionException, ZebraPrinterLanguageUnknownException, ZebraIllegalArgumentException {
    for(String image:imagesToSend) {
        //[0] -> image path, <[1]> -> image scale factor
        String[] imageParams = image.split("\\|");
        double scaleFactor = imageParams.length > 1 ? parseImageScale(imageParams[1]) : 1.0d;

        File file = new File(StringUtils.pathCombine(rootPath,imageParams[0]));
        if(!file.exists())
            throw new FileNotFoundException("Image file not found " + file.getName());

        ZebraImageI zebraImage = ZebraImageFactory.getImage(BitmapFactory.decodeFile(file.getAbsolutePath()));

        devicePrinter.storeImage("E:" + file.getName(), zebraImage, (int)(zebraImage.getWidth() * scaleFactor), (int)(zebraImage.getHeight() * scaleFactor));
    }
}

public void storeImage(String printerFullPath, ZebraImageI zebraImage, int width, int height) throws ConnectionException, ZebraPrinterLanguageUnknownException, IOException, ZebraIllegalArgumentException {
    Connection connection = null;
    try {
        connection = ConnectionBuilder.build(connectionString);
        connection.open();
        ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);

        printer.storeImage(printerFullPath, zebraImage, width, height);

    }
    finally {
        if(connection != null)
            connection.close();
    }
}

The cpcl format file:

PCX 10 280 !<signature.pcx

We set a scaling parameter to manage the printer-stored image size. This prints the image fine on the P4T printer but we have a QLn420 that prints a long character string representation of the image.

The other text parts of the receipt print normally on these devices.

Anyone experience this problem and know how to fix it?

*Edit I've also tried directly printing a zebra image to the printer using the following code. No matter which, I always get the string (base64 representation of the image.)

public void printImage(ZebraImageI zebraImage, int width, int height) throws ConnectionException, ZebraPrinterLanguageUnknownException, ZebraIllegalArgumentException {
    Connection connection = null;
    try {
        connection = ConnectionBuilder.build(connectionString);
        connection.open();
        ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);
        printer.printImage(zebraImage, 0, 0, width, height, false);
    }
    finally {
        if(connection != null)
            connection.close();
    }
}

**Edit On the Qln420 the image is never stored. I'm expecting for it to show up at "E:sigfile.pcx" after calling storeImage() but it never saves. Don't know why yet.


Solution

  • The fix for us was to explicitly set the printer language when creating a new printer for storing the image:

    public void storeImage(String printerFullPath, ZebraImageI zebraImage, int width, int height, PrinterLanguage printerLanguage) throws ConnectionException, ZebraPrinterLanguageUnknownException, IOException, ZebraIllegalArgumentException {
        Connection connection = null;
        try {
            connection = ConnectionBuilder.build(connectionString);
            connection.open();
    
            ZebraPrinter printer = ZebraPrinterFactory.getInstance(printerLanguage, connection);
    
            printer.storeImage(printerFullPath, zebraImage, width, height);
    
        }
        finally {
            if(connection != null)
                connection.close();
        }
    }
    

    Also, we found that removing the image's file extension was helpful for one device so we remove the file extension in all cases now. So for us, printing a CPCL format receipt with an inline image works on P4T, QLn420 and ZQ520.

    Additionally, we found that scaling the image prior to storing it is also necessary since a large image store will fail without throwing an exception.

        ZebraImageI zebraImage = ZebraImageFactory.getImage(BitmapFactory.decodeFile(file.getAbsolutePath()));
        double scaleFactor = (printedImageWidth == null) ? 1.0 : (double)printedImageWidth / zebraImage.getWidth();
        //strip off the extension
        String fileName = file.getName().split("\\.")[0];
        devicePrinter.storeImage(fileName, zebraImage, (int)(zebraImage.getWidth() * scaleFactor), (int)(zebraImage.getHeight() * scaleFactor), printerLanguage);