Friday, February 24, 2012

Barcode: Professional java barcode API (reader & writer)

Originally Barcodes were 1D representation of data using width and spacing of bars. Common bar code types are UPC barcodes (as shown) which are seen on product packages. 

There are 2D barcodes as well (they are still called Barcodes even though they don’t use bars). A common example of 2D bar code is PDF417, QR Code (as shown) which is commonly used by mobile phone apps. You can read history and more info about Barcodes on Wikipedia..
QR Code
UPC-A
PDF417 code
There is an open source Java library called ‘zxing’ (Zebra Crossing) which can read and write many differently types of bar codes formats.
I tested zxing and it was able to read a pure barcode and embedded in the middle of a 100 dpi grayscale busy text document!
This article demonstrates how to use zxing to read and write bar codes from a Java program.
1- Getting the library
To download the source code and build the binaries, which was actually quite easy.

The source code of the library is available on Google Code. At the time of writing, 2.0 is the latest version of zxing.
  1. Download the release file ZXing-2.0.zip (which contains of mostly source files) from here.
  2. Unzip the file in a local directory
  3. You will need to build 2 jar files from the downloaded source: core.jar, javase.jar
2- Including the libraries in your project
If you are using ant or normal project, add the core.jar and javase.jar to your project’s classpath.
Once you have the jars included in your project’s classpath, you are now ready to read and write barcodes from java!

Below is an example using the zxing API as generic way of reading and writing any type of barcodes, and improve this engine with hints passed to encoder or decoder methods.

3- Reading a Bar Code


You can read the bar code by first loading the image as an input stream and then calling this utility method.



4- Writing a Bar Code

You can encode a small text string as follows:

5- JavaDocs & documentation help

The Javadocs are part of the downloaded zip file. You can find a list of supported bar code formats in the Javadocs.
Update (2/2/2013):
Hi Please find the below link contains the full explanation of barcode API. Developing advanced Barcode applications using "BarcodeEngine" library
If you need any other help leave your comments, it will be nice if you leave your Reactions.

41 comments :

  1. Hi Mohamed,

    Nice blog! Is there an email address I can contact you in private?

    ReplyDelete
    Replies
    1. my mail is mohamed.taman@gmail.com

      Delete
    2. Indeed nice tutorial Mohamed, thanks for taking time and sharing your knowledge. Now days even QR code is getting popular, hope to see tutorial on that as well. By the way I have also shared How to do LDAP Authentication using Spring Security you may find useful.

      Thanks
      Javin

      Delete
    3. Dear Javin, i will try share the source code for complete BarcodEngine for decoding and encoding.

      Delete
  2. This is part of conversation about this blog I liked to share it with you so you can benefit from it

    Someone
    --------
    I read ur article about AXing api in java it was a good one and really helped me a lot

    can u please tell me how can i encode an image as qr code using AXing ?

    Me
    ---
    The sample I introduced is multi-barcode encoder & decoder, just pass to the method the barcode format you need to encode and it will take care of that.

    If you need any further help don't hesitate to contact me.

    Someone
    -------
    I created it with text means my name,address, website all

    but i cold not include my image on that QR code that's why i asked you

    Me
    --
    You can't encode your image inside QRCode or any barcode format directly, you can get the image stream as binary string and encode it inside the QR code all barcode info should be String. http://en.wikipedia.org/wiki/QR_code

    ReplyDelete
  3. Mohamed, I have been tasked with reading a barcode from a scanned document saved as a pdf. I was wondering if you had any tips on getting the pdf over to an image file that can be read by ZXing?

    Thanks

    ReplyDelete
    Replies
    1. You can use iText API to conver pdf document to image then read it by the barcode reader.

      Delete
  4. I want to add Barcode in PDF File using Java, please suggest.

    ReplyDelete
    Replies
    1. 1- You can generate the Barcode and include it in your pdf as image by using iText API that manipulate the pdf documents.

      2- If you designing a report, generate the barcode you want in a specific path, then point it to the generated barcode image.

      Delete
    2. hi Mohamed,
      i wanna write a java application to design labels with barcodes.
      thus, positioning the content of the labels and modifying t when necessary.
      could you help me?

      thanks

      Delete
  5. very helpful tutorial Thanks mohamed
    but i still have some doubts
    1 i m using Barcode scanner so is there any listener we can apply on this code so that whenever scanner scan something then it will automatically read.

    2 you are giving the path of images for reading the barcode. but in my case if i m directly scan the barcode through scanner then what are the change i have to do in this code.

    any type of help is highly appreciated

    Thanksss

    ReplyDelete
    Replies
    1. for point number 1, you can have a listener on the scanner API so when it reads a content it fires the action calling the above code.

      for point number 2, Nothing should be changed as it is preferred to get your content from the scanner and put it in a temp file till finished the streaming, then pass that file to decode method.

      Delete
    2. Thanks mohamed

      for point number 1.. can u plzzz give me some idea that how to put listener in between the code so that whenever scanner scan something then it will automatically read.

      for point 2 you mean to say that whenever scanner scan the barcode then first it save the content in image format.

      Thanks for reply

      Delete
    3. You can mix the 2 points with each other as the flowing:
      1- After your scanner read the barcode it will save it to a file in a specific folder, make a listener on that folder that watch for newly added images by Using Quartz API job.

      2- The implementation of the job use the barcode reader to read your image.

      Delete
  6. hi,

    can i print this bar code through my bar code printer?

    i want to print barcode for my inventory through this printer from y database value

    ReplyDelete
  7. i would like to know how to make a listener on a folder that watch for newly added images by Using Quartz API job?

    ReplyDelete
    Replies
    1. I will give you the idea there are 2 solutions as the following:
      First:
      ----
      In the folder that you put the images in, you will create a Quartz job that runs every 2 seconds for example, get this folder and traverse its content and check if there is new files added, and when you process thies files move them to another folder, then the original folder will be empty, the point is that it will save your processing time when the files get increased, by checking its creation date.

      Second:
      ------
      If you use jdk7 you can use the FileVisitor which make the traversal process very easy and it is already in jdk7 no need to get another libs for this issue.
      Check out this article that will help you http://tamanmohamed.blogspot.com/2012/03/jdk7-part-1-power-of-java-7-nio2-jsr.html

      Delete
  8. Hi Mohammed Taman ... , do you have tutorial of a reader barcode from webcam directly ?

    thanks ...

    ReplyDelete
    Replies
    1. I am preparing a full tutorial article on Barcode usage including the source code.

      Delete
    2. Hi

      hope you are fine, and please find the below link contains the full explanation of barcode API.

      http://tamanmohamed.blogspot.com/2013/02/barcode-developing-advanced-barcode.html

      for any comment please left it on blog post.

      Delete
  9. can anyone(mohamed taman) give exact steps from (starting to end)
    How this actually works. I noticed the code but how to read a file from barcode scanner i.e how it works with my code....

    secondly how can i generate a barcode (zebra style)please elaborate the above example with a little explaination..

    thank you

    ReplyDelete
    Replies
    1. I am preparing a full tutorial article on Barcode usage including the source code.

      Delete
  10. Replies
    1. I am preparing a full tutorial article on Barcode usage including the source code.

      Delete
  11. Hi, I am doing a bachelor project and i want to integrate barcode scanner to my app that will read the barcode from my own database. can you send your code, will be thankfull l0ve@abv.bg or give any other articles

    ReplyDelete
    Replies
    1. I am preparing a full tutorial article on Barcode usage including the source code, and i will help you as you want, you are welcome.

      Delete
  12. Hi Mohamed Taman.....
    I am curious to know about what is source code to scan a BAR Code( Please put a ponder upon my query, I am not asking for QR-COde scanner. I already integrate QR- raeder in my App) . Please help me.

    ReplyDelete
  13. AoA Mohamed,
    I am facing compile time error at line 14 of decode method. The compiler is unable to find read() method. Can you tell me what I need to do in order to make this code work. Do I need to extend my class from some other class? Please reply ASAP. thnx in advance

    ReplyDelete
    Replies
    1. Thanks dear
      In coming couple of weeks ISA I will complete the article about the full barcode engine and publish it.

      Delete
    2. Hi

      hope you are fine, and please find the below link contains the full explanation of barcode API.

      http://tamanmohamed.blogspot.com/2013/02/barcode-developing-advanced-barcode.html

      for any comment please left it on blog post.

      Delete
  14. hiii...I want to create barcode image so is writing method can create image of barcode??

    ReplyDelete
    Replies
    1. Hi

      hope you are fine, and please find the below link contains the full explanation of barcode API.

      http://tamanmohamed.blogspot.com/2013/02/barcode-developing-advanced-barcode.html

      for any comment please left it on blog post.

      Delete
  15. Dear Muhammad :
    Thanks A lot , I'v been searching for 2 days and I didn't found a solved example
    on this topic .
    But here is my problem , I don't know how to start creating a barcode Scanner to get the code
    from the scanner.
    can you describe it more please if you can with example .
    Thanks Again.

    ReplyDelete
    Replies
    1. You need just a scanned picture that should be passed to the BarcodeEngine decode method

      Refer to the following link for full details:

      http://tamanmohamed.blogspot.com/2013/02/barcode-developing-advanced-barcode.html

      Delete
  16. sir, some scanners can they detect qr code ?

    ReplyDelete
    Replies
    1. Yes, most of scanners can detect multiple barcode types , but you should ask first as there are a specific barcode readers for certain barcode types.

      for example, there reader for PDF_417 only. and so on

      Delete
  17. Hi Taman,

    I am trying to read a barcode from an image clicked with cell phone camera. One of the online softwares was able to read this barcode, but I was having trouble reading it through the code above. Can you tell me if there are any changes needed to the program or the image itself for it to work?

    ReplyDelete
    Replies
    1. Hi Dear,
      Thanks for your interest, I have added new article (http://tamanmohamed.blogspot.com/2013/02/barcode-developing-advanced-barcode.html ) that describes my barcode engine with full example and provides the source code lib alongside with documentation.

      When you read try to use tryHarder option with decoding configurations.

      Delete
  18. :) Thank you for sharing this article.This java barcode API can be very useful!

    ReplyDelete