How to create QR Codes in Java

Bar codes aren’t just for cereal boxes any more. 2-D barcodes like QR Codes are easily read by smart phones and are showing up everywhere from magazines to restaurant fronts to business cards. With some of the open-source libraries out there, it’s not hard to create your own QR Codes with java.

To generate a quick code for standard content, there are several online generators, like this. Here is a link to this site:

Urls, contacts, calendar events, even wifi network info are getting put into QR Codes. But you don’t have to stick to standard content or to provided generators. So how would you encode ‘hello world’ in a QR Code?

The good folks working on ZXing provide many tools for working with bar codes in java. The project does not seem to be in maven repositories, so to get started, download ZXing-1.6.zip from their site. I didn’t notice any built jars in there, but it’s easy to build from the source with ant or maven (build.xml and pom.xml files are provided).

Build the ‘core’ and ‘javase’ projects and put the jars into your classpath. Then create a string, use the zxing writer to encode it in a matrix, and save it to a png file:


Note that the string is encoded in ISO-8859-1. A lot of barcodes don’t support unicode. The resulting QR Code png file is:

Theoretically, thousands of characters can be encoded in QR Codes, but at least one source recommended not going above 800 characters or so. Here’s some text of about 700 characters:
Why would you encode a big blob of text in a barcode? Well, you probably wouldn’t. But there are plenty of useful scenarios for QR Codes and with more and more people buying barcode readers, aka smart phones, they’re only going to keep growing.

This entry was posted in java, software and tagged . Bookmark the permalink.

16 Responses to How to create QR Codes in Java

  1. Pingback: How to create QR Codes in Java

  2. I enjoy using the rich functionality of Google Charts (which include QR, and much more) so I decided to donate into the public domain a simple one page class, which demonstrates how to harness its full amazing functionality. I hope you and your readers enjoy it.

    https://cajo.dev.java.net/files/documents/974/154016/GoogleChart.java

  3. Pingback: QR код власними руками на Java | QR код для кожного!

  4. I just loaded the 700 character QR code in QuickMarks4 directly of of my screen! That app is amazing!

  5. peter says:

    Is there a versio for jdk1.4? I need to create oracle java stored procedure to create QR code on database..

  6. Chris says:

    The Code line:
    MatrixToImageWriter.writeToFile(matrix, “PNG”, file);
    doesn’t work in combination with android.
    Getting Exception. What can I do to solve this problem?

    Thanks for your help. Chris

  7. Leboulanger says:

    Hello,

    I Have an error on the line :

    MatrixToImageWriter.writeToFile(matrix, “PNG”, file);

    The error is “cannot be resolved”.

    How to resolve this problem ?

    Thank you

    • tborthwick says:

      I’d take a guess that your file object has a problem, like an invalid path.

      • Leboulanger says:

        Yes, I had forget zxing javase

        I obtain this error now :

        java.io.FileNotFoundException: /var/www/domaine/PUBLIC/tmp/qr-code.png (No such file or directory)
        at java.io.RandomAccessFile.open(Native Method)
        at java.io.RandomAccessFile.(RandomAccessFile.java:233)
        at javax.imageio.stream.FileImageOutputStream.(FileImageOutputStream.java:69)
        at com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstance(FileImageOutputStreamSpi.java:55)
        at javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:409)
        at javax.imageio.ImageIO.write(ImageIO.java:1520)
        at com.google.zxing.client.j2se.MatrixToImageWriter.writeToFile(MatrixToImageWriter.java:65)

        Or the file exist …

        I don’t understand

        Thanks

  8. Melanie says:

    hello

    i ran the code as a standalone app and it worked. However if i try the same code on android it fails. Please reply

    • tborthwick says:

      I think the zxing libraries only support reading bar codes on android, not generating them. You could doublecheck their site to see if they have any suggestions.

  9. Martin says:

    It is possible to use this qr code library for Java to create qr code generator on Android.

  10. Meillus says:

    Theoretically, thousands of characters can be encoded in QR Codes, but at least one source recommended not going above 800 characters or so. Here’s some text of about 700 characters:
    Why would you encode a big blob of text in a barcode? Well, you probably wouldn’t. But there are plenty of useful scenarios for QR Codes and with more and more people buying barcode readers, aka smart phones, they’re only going to keep growing.

    ——————————————————————–

    Accually I need to encode a novel in a barcode… For an artistic project… Is it possible? We don’t care if it could be scanned or not…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>