Wednesday, February 6, 2013

Image to Zebra GRF

So we use a Zebra printer at work to print thermal labels. Well I wanted to add an image to the label. Zebra makes you use a windows only all in one tool that is way too bloated for my taste. So a bit of research and I made my own to convert BMP images to GRF ascii. It's in Java and it now converts multiple different image formats. Note that the branch is still under testing since I don't have a zebra printer with me. Branch is nativeImageAPI https://github.com/asharif/img2grf

15 comments:

  1. HI, need ur help. I tried to run "mvn clean package" but got the following errors:

    Unable to locate the Javac Compiler in:
    /opt/java/32/jre1.7.0_17/../lib/tools.jar
    Please ensure you are using JDK 1.4 or above and
    not a JRE (the com.sun.tools.javac.Main class is required).

    Could you tell me what should i do?

    ReplyDelete
    Replies
    1. Hi vee, Do you have Java installed on your machine? It seems like maven is having a problem finding the java compiler (javac). What OS do you have running? If *nix then please type echo $JAVA_HOME in terminal. if Windows then echo %JAVA_HOME% at command prompt. Let me know.

      Delete
  2. Wow...Excelent work!
    I will always be thankful with you.

    ReplyDelete
  3. HI Arash,

    Thank you for sharing your BMP to GRF tool. I was able to make it work and found it very useful for the actual conversion I was trying to do before that kept on failing. Anyway, I'd like to ask, is this restricted to a 2-bit bitmap image? Because I am modifying an existing colored image and convert it into a 2-bit image bitmap but it is not pretty anymore. I'd like to get your opinion if it is much better I should have the image re-created by artist into flat black and white.

    I am using the GRF output for direct communication with a Zebra printer from a linux console. But on a previous experience with Zebra printouts but under Windows system, I know it is possible to printout a PDF (with nice layout and image inserts) and they look pretty well but this is coursed through Windows drivers.

    I also sent the message above via G+ Hope you got it and you don't mind the direct inquiry!

    Thanks in advance.

    ReplyDelete
    Replies
    1. Hi there! I'm glad it is sorta working out for you! To answer your question, yes it is only for bitonal images. The GRF format is just ascii and the algorithm used in this tool is designed just to detect between the two.

      Delete
  4. Hi,
    I'm developing labels for output on Zebra printers and since I needed GRF files I searched google and found your converter tool! Great!

    Now, I'm new to Maven but have managed to install it and also to successfully run the "mvn clean package" command for your tool.
    Now my problem is to actually run it...

    The help says to run java -jar img2grf.jar -f {path to file} to execute, but somehow I get it wrong. Can you help me?

    My directory for the tool is C:\Users\\img2grf-master, and test.bmp lies right there. The jar files lies in target folder, so I haven't moved anything. Not knowing which of the jar files to use, I've tried the below commands:

    java -jar target\img2grf-1.0-jar-with-dependencies.jar -f {test.bmp}
    java -jar target\img2grf-1.0-jar -f {test.bmp}

    For the first command I get "Error. No file found at path: {test.bmp}.
    The other command generates "Failed to load Main-Class manifest attribute from target\img2grf-1.0.jar"

    What am I doing wrong here? Thank you so much in advance if you have time to help!

    BR,
    Ulrika Enskog

    ReplyDelete
    Replies
    1. Hi there. If you have Skype handle I can maybe show you and we can go back and forth a bit. My handle is arash.sharif. I will be available later tonight around 10pm. I would gladly help then.

      Delete
    2. I didn't see your reply until now. And at 10 pm Californian time (at least I think I saw somewhere that's where you're from?) I was still asleep way over here in Sweden...
      Now I didn't bring my pc with me from work over the weekend so I'll just have to get on it on Monday again.

      But could you tell me in what way I should pass the path to the file? Should it be with or without " ", should it be with backslash or forwardslash and shoud I write the entire path starting from C: or simply base the path on the folder I'm in when writing the command in the command prompt..?
      I ususally program ABAP for SAP and not so much cmd commands or java either, even though I know it a bit. So that's my excuse for maybe asking basic questions :)

      Thanksfor taking your time!

      Delete
    3. Hi there, The command your looking for is

      java -jar target\img2grf-1.0-jar-with-dependencies.jar -f {test.bmp}

      test.bmp in this case needs to be from whatever directory your running java -jar from. Also you might want to try the nativeImageAPI branch. I don't have access to a zebra printer anymore so I need some help with testing this branch. The new branch allows many more image types than just BMP.

      Delete
  5. Try this one ...
    https://mega.co.nz/#!DJBgwYzD!09rTYzwi_2OfEVKN20osMlMdLClv2qyDXUbFYra82ks

    I was facing tons of issues back in 2005 when was coding some libraries in C++ for those printers and found a lot of info on ZPL guide.
    For GRF files I created this app to help me on daily basis image conversion.
    Didn't touched this code anymore since then .. so probably has some room to improvement .. but a quick test and look like working fine (except the image size max to 600x600 .. something related to old systems 9 years ago .. maybe memory .. maybe screen resolution .. can't remember)

    ReplyDelete
  6. Hello,
    thanks for the tool... But i get al lot of ArrayIndexOutOfBoundsException exceptions...

    withoutHeaderBytes[newByteIndex++] = origBytes[j];

    what am i doing wrong? changing the size of the bmp sometimes helps...

    Thanks,

    ReplyDelete
    Replies
    1. I have been having the same issue. It seems to have something to do with the width of the image. The height seems to have noting to do with it (from what I have tried).

      Delete
    2. I created my own tool using python. From what I can tell, it does not have that issue.
      There is an example of how to use it at the very bottom of the program.
      You will have to pip install regex, pillow, and numpy.
      Just paste this into a .py file and run it:

      from PIL import Image
      import re
      import numpy as np

      def image2grf(filePath, length = None, width = None):
      #Open the image
      image = Image.open(filePath)

      image = image.convert("1") #Ensure that it is black and white image

      #Resize image to desired size
      if ((length != None) and (width != None)):
      size = (length, width)
      image.thumbnail(size, Image.ANTIALIAS)

      #Convert image to binary array
      bitmap = np.asarray(image, dtype = 'int')
      bitmap = np.asarray(bitmap, dtype = 'str').tolist()

      #Convert binary array to binary string
      binaryString = ""
      for row in bitmap:
      #Join the row to the string
      row = "".join(row)

      #Make each pixel square (for some reason it is rectangular)
      binaryString += row
      binaryString += row
      binaryString += row
      binaryString += row

      #Convert binary string to hex string
      hexString = re.sub("0","F",binaryString)
      hexString = re.sub("1","0",hexString)

      #Calculate bytes per row and total bytes
      bytesPerRow = len(bitmap[0]) / 2
      totalBytes = bytesPerRow * len(bitmap) * 4 #0.5 for each line piece in a line

      #Compose data
      data = "~DGimage," + str(totalBytes) + "," + str(bytesPerRow) + "," + hexString

      #Save image
      fileHandle = open(r"labelPicture.grf", "w")
      fileHandle.write(data)
      fileHandle.close()

      if __name__ == '__main__':
      image2grf(r"test.bmp")

      Delete
    3. Looks like the site took out my indentation. Take out the '>' at the end of each line for this to work:

      >from PIL import Image
      >import re
      >import numpy as np
      >
      >def image2grf(filePath, length = None, width = None):
      > #Open the image
      > image = Image.open(filePath)
      >
      > image = image.convert("1") #Ensure that it is black and white image
      >
      > #Resize image to desired size
      > if ((length != None) and (width != None)):
      > size = (length, width)
      > image.thumbnail(size, Image.ANTIALIAS)
      >
      > #Convert image to binary array
      > bitmap = np.asarray(image, dtype = 'int')
      > bitmap = np.asarray(bitmap, dtype = 'str').tolist()
      >
      > #Convert binary array to binary string
      > binaryString = ""
      > for row in bitmap:
      > #Join the row to the string
      > row = "".join(row)
      >
      > #Make each pixel square (for some reason it is rectangular)
      > binaryString += row
      > binaryString += row
      > binaryString += row
      > binaryString += row
      >
      > #Convert binary string to hex string
      > hexString = re.sub("0","F",binaryString)
      > hexString = re.sub("1","0",hexString)
      >
      > #Calculate bytes per row and total bytes
      > bytesPerRow = len(bitmap[0]) / 2
      > totalBytes = bytesPerRow * len(bitmap) * 4 #0.5 for each line piece in a line
      >
      > #Compose data
      > data = "~DGimage," + str(totalBytes) + "," + str(bytesPerRow) + "," + hexString
      >
      > #Save image
      > fileHandle = open(r"labelPicture.grf", "w")
      > fileHandle.write(data)
      > fileHandle.close()
      >
      >if __name__ == '__main__':
      > image2grf(r"test.bmp")

      Delete
    4. Forget it. Here is a link to github.

      https://github.com/JoshMayberry/Utilities/blob/master/image2grf.py

      Delete