How to convert png image to zpl string in java?

I have found a code to generate zpl string for an image in java. However, when I am executing this java code it is giving me zpl string which is nothing but black image.

Code is

Java Code

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import javax.imageio.ImageIO;

public class ZPLConveter {

    private int blackLimit = 380;

    private int total;

    private int widthBytes;

    private boolean compressHex = false;

    private static Map<Integer, String> mapCode = new HashMap<Integer, String>();

    {

        mapCode.put(1, "G");

        mapCode.put(2, "H");

        mapCode.put(3, "I");

        mapCode.put(4, "J");

        mapCode.put(5, "K");

        mapCode.put(6, "L");

        mapCode.put(7, "M");

        mapCode.put(8, "N");

        mapCode.put(9, "O");

        mapCode.put(10, "P");

        mapCode.put(11, "Q");

        mapCode.put(12, "R");

        mapCode.put(13, "S");

        mapCode.put(14, "T");

        mapCode.put(15, "U");

        mapCode.put(16, "V");

        mapCode.put(17, "W");

        mapCode.put(18, "X");

        mapCode.put(19, "Y");

        mapCode.put(20, "g");

        mapCode.put(40, "h");

        mapCode.put(60, "i");

        mapCode.put(80, "j");

        mapCode.put(100, "k");

        mapCode.put(120, "l");

        mapCode.put(140, "m");

        mapCode.put(160, "n");

        mapCode.put(180, "o");

        mapCode.put(200, "p");

        mapCode.put(220, "q");

        mapCode.put(240, "r");

        mapCode.put(260, "s");

        mapCode.put(280, "t");

        mapCode.put(300, "u");

        mapCode.put(320, "v");

        mapCode.put(340, "w");

        mapCode.put(360, "x");

        mapCode.put(380, "y");       

        mapCode.put(400, "z");           

    }

    public String convertfromImg(BufferedImage image) throws IOException {

        String cuerpo = createBody(image);

        if(compressHex)

           cuerpo = encodeHexAscii(cuerpo);

        return headDoc() + cuerpo + footDoc();       

    }

    private String createBody(BufferedImage orginalImage) throws IOException {

        StringBuffer sb = new StringBuffer();

        Graphics2D graphics = orginalImage.createGraphics();

        graphics.drawImage(orginalImage, 0, 0, null);

        int height = orginalImage.getHeight();

        int width = orginalImage.getWidth();

        int rgb, red, green, blue, index=0;       

        char auxBinaryChar[] =  {'0', '0', '0', '0', '0', '0', '0', '0'};

        widthBytes = width/8;

        if(width%8>0){

            widthBytes= (((int)(width/8))+1);

        } else {

            widthBytes= width/8;

        }

        this.total = widthBytes*height;

        for (int h = 0; h<height; h++)

        {

            for (int w = 0; w<width; w++)

            {

                rgb = orginalImage.getRGB(w, h);

                red = (rgb >> 16 ) & 0x000000FF;

                green = (rgb >> 8 ) & 0x000000FF;

                blue = (rgb) & 0x000000FF;

                char auxChar = '1';

                int totalColor = red + green + blue;

                if(totalColor>blackLimit){

                    auxChar = '0';

                }

                auxBinaryChar[index] = auxChar;

                index++;

                if(index==8 || w==(width-1)){

                    sb.append(fourByteBinary(new String(auxBinaryChar)));

                    auxBinaryChar =  new char[]{'0', '0', '0', '0', '0', '0', '0', '0'};

                    index=0;

                }

            }

            sb.append("\n");

        }

        return sb.toString();

    }

    private String fourByteBinary(String binaryStr){

        int decimal = Integer.parseInt(binaryStr,2);

        if (decimal>15){

            return Integer.toString(decimal,16).toUpperCase();

        } else {

            return "0" + Integer.toString(decimal,16).toUpperCase();

        }

    }

    private String encodeHexAscii(String code){

        int maxlinea =  widthBytes * 2;       

        StringBuffer sbCode = new StringBuffer();

        StringBuffer sbLinea = new StringBuffer();

        String previousLine = null;

        int counter = 1;

        char aux = code.charAt(0);

        boolean firstChar = false;

        for(int i = 1; i< code.length(); i++ ){

            if(firstChar){

                aux = code.charAt(i);

                firstChar = false;

                continue;

            }

            if(code.charAt(i)=='\n'){

                if(counter>=maxlinea && aux=='0'){

                    sbLinea.append(",");

                } else     if(counter>=maxlinea && aux=='F'){

                    sbLinea.append("!");

                } else if (counter>20){

                    int multi20 = (counter/20)*20;

                    int resto20 = (counter%20);

                    sbLinea.append(mapCode.get(multi20));

                    if(resto20!=0){

                        sbLinea.append(mapCode.get(resto20) + aux);   

                    } else {

                        sbLinea.append(aux);   

                    }

                } else {

                    sbLinea.append(mapCode.get(counter) + aux);

                    if(mapCode.get(counter)==null){

                    }

                }

                counter = 1;

                firstChar = true;

                if(sbLinea.toString().equals(previousLine)){

                    sbCode.append(":");

                } else {

                    sbCode.append(sbLinea.toString());

                }               

                previousLine = sbLinea.toString();

                sbLinea.setLength(0);

                continue;

            }

            if(aux == code.charAt(i)){

                counter++;               

            } else {

                if(counter>20){

                    int multi20 = (counter/20)*20;

                    int resto20 = (counter%20);

                    sbLinea.append(mapCode.get(multi20));

                    if(resto20!=0){

                        sbLinea.append(mapCode.get(resto20) + aux);   

                    } else {

                        sbLinea.append(aux);   

                    }

                } else {

                    sbLinea.append(mapCode.get(counter) + aux);

                }

                counter = 1;

                aux = code.charAt(i);

            }           

        }

        return sbCode.toString();

    }

    private String headDoc(){

        String str = "^XA " +

                        "^FO0,0^GFA,"+ total + ","+ total + "," + widthBytes +", ";

        return str;

    }

    private String footDoc(){

        String str = "^FS"+

                        "^XZ";       

        return str;

    }

    public void setCompressHex(boolean compressHex) {

        this.compressHex = compressHex;

    }

    public void setBlacknessLimitPercentage(int percentage){

        blackLimit = (percentage * 768 / 100);

    }

    public static void main(String[] args) throws Exception {

        BufferedImage orginalImage = ImageIO.read(new File("/tmp/Logo.png"));

        ZPLConveter zp = new ZPLConveter();

        zp.setCompressHex(true);

        zp.setBlacknessLimitPercentage(50);       

        System.out.println(zp.convertfromImg(orginalImage));       

    }

}

Anonymous (not verified)
Hi, If you are using the Link

Hi,

If you are using the Link-OS SDK, there is a simple converting method:

PrinterUtil.convertGraphic()

PrinterUtil (Zebra API for Android (build v2.14.5097))

This converts several common graphics types like .png and .jpg to a Zebra downloadable .GRF format.

Hope this helps!

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Anonymous (not verified)
Hi.I'm trying to use

Hi.

I'm trying to use PrinterUtil.convertGraphic() to convert a thumbnail image captured on my Android device and insert it into a label.  But I'm having difficulty figuring out how to use it.  My app:

1. Captures an image using Camera2 API

2. Extracts the thumbnail bitmap in onActivityResult() and displays it in an ImageView

3. Saves the thumbnail to internal memory as a JPEG (I've also tried PNG)

When the user hits the Print button, the app:

1. Reads in the JPEG

2. Trys to convert it with PrinterUtil.convertGraphic()

But I get the Exception:   ZebraIllegalArgumentException: Unsupported file type for graphics conversion

Any suggestions on what I might be doing wrong?

Thanks.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Anonymous (not verified)
Correction:I'm not even

Correction:

I'm not even getting to the PrinterUtil.convertGraphic().

My Exception is happening when I try to get the ZebraImageI using ZebraImageFactory.getImage().  That's where I'm sending it the JPEG or PNG and am getting the "Unsupported file type" Exception.

Thanks.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Dmitry Prokhorov
Hello Tony,There are 3

Hello Tony,

There are 3 different ZebraImageFactory.getImage() methods that are using different parameters.

ZebraImageFactory (Zebra API for Android (build v2.14.5097))

Which one you are using? Could you please to share a source code?

I believe you may need to check your JPEG (or PNG) saved at step #3.

Dmitry Prokhorov

Software Engineer, Kutir Mobility

Posted on behalf of Zebra Technologies

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Anonymous (not verified)
This took some poking around,

This took some poking around, but I figured it out.  For other's reference, here is my Java code for Android, printing to a ZD510 wristband printer over Bluetooth Low Engergy:

private void printLabelWithImage(Bitmap bitmap, Context context)

  {

   //  The context is the passed in from the calling method

   //  using getApplicationContext()

 

   Connection conn = new BluetoothLeConnection(nfcMacAddress, context);

   try

   {

    conn.open();

  

    ZebraPrinter zprinter = ZebraPrinterFactory.getInstance(conn);

    ZebraImageI zi = ZebraImageFactory.getImage(bitmap);

    zprinter.storeImage("E:IMAGE.PNG", zi, zi.getWidth(), zi.getHeight());

    Thread.sleep(500);

    string label = "^XA~TA000~JSN^LT0^MNM^MTD^PON^PMN^LH0,0^JMA^PR2,2~SD21^JUS^LRN^CI0^XZ" +

     "^XA" +

     "^MMT" +

     "^PW300" +

     "^LL3300" +

     "^LS0" +

     "^FO0,2400" +

    "^IME:IMAGE.PNG^FS" +                                                        //  This is where the image is used.

     "^FT70,2395^A0B,67,67^FH\\^FD**patient_name**^FS" +

     "^FT121,2288^A0B,42,40^FH\\^FD**birth**^FS" +

     "^FT123,2393^A0B,42,40^FH\\^FDDOB:^FS" +

     "^FT174,2391^A0B,42,40^FH\\^FDPatient ID:^FS" +

     "^FT172,2204^A0B,42,40^FH\\^FD**id**^FS" +

     "^BY5,0^FT280,1347^BOB,5,N,0,N,1," +

     "^FH\\^FD**barcode**^FS" +

     "^PQ1,0,1,Y^XZ");

   

    conn.write(label.getBytes());

   

    Thread.sleep(500);

    conn.close();

   }

   catch (Execption e)

   { e.printStackTrace() }

          }

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Log in to post comments