All ArticlesMisc

Generating QR Codes in Java with ZXing

QR Codes are used more and more including both commercial tracking applications and convenience-oriented applications aimed at mobile phone users (known as mobile tagging). QR Codes storing addresses and URLs may appear in magazines, on signs, buses, business cards, or just about any object that users might need information about. There´re is not a big choice on libraries for generating QR Codes in Java right now. One possibility is the open-source library ZXing. This article will give an example for using this library.

The following code snipped gives you a ready to use Java method for generating QR Codes. For this example we used ZXing-1.5.

/**
     * Call this method to create a QR-code image. You must provide the OutputStream where the image data can be written.
     * 
     * @param outputStream The OutputStream where the QR-code image data is written.
     * @param content The string that should be encoded with the QR-code.
     * @param qrCodeSize The QR-code must be quadratic. So this is the number of pixel in width and height.
     * @param imageFormat The image format in which the image should be rendered. As Example 'png' or 'jpg'. See @javax.imageio.ImageIO for
     *        more information which image formats are supported.
     * @throws Exception If an Exception occur during the create of the QR-code or while writing the data into the OutputStream.
     */
    public void createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws SolseitException
    {
        try
        {
            // Create the ByteMatrix for the QR-Code that encodes the given String.

            Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
            hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            ByteMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);

            // Make the BufferedImage that are to hold the QRCode

            int matrixWidth = byteMatrix.getWidth();

            BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB);
            image.createGraphics();

            Graphics2D graphics = (Graphics2D) image.getGraphics();
            graphics.setColor(Color.WHITE);
            graphics.fillRect(0, 0, matrixWidth, matrixWidth);

            // Paint and save the image using the ByteMatrix

            graphics.setColor(Color.BLACK);

            for (int i = 0; i < matrixWidth; i++)
            {
                for (int j = 0; j < matrixWidth; j++)
                {
                    if (byteMatrix.get(i, j) == 0)
                    {
                        graphics.fillRect(i, j, 1, 1);
                    }
                }
            }

            ImageIO.write(image, imageFormat, outputStream);
        }
        catch (Exception ex)
        {
            ...
        }
    }

You can download the latest version of the library on the ZXing download site.

About this Article

jan.ziegler: Generating QR Codes in Java with ZXing

written by: jan.ziegler on 06 Jul 2010

tags used: QR Code, ZXing

news stored under: Misc

share this article:

Comments (3)

chris at dam (posted on 06 Jul 2010, 21:22)

we generated a qr code accessing the resources for our last user meeting in mediacockpit using a web service - if i knew solseit could do it i would have done it myself :-)

Terry Lawton (posted on 09 Apr 2012, 17:40)

Perfect post. Learn how to incorporate QR codes in your web apps to deliver quick information directly to your users' mobile device http://blog.caspio.com/web_apps/4-ways-to-use-qr-codes-in-your-web-apps/

deepak (posted on 22 Apr 2012, 21:30)

hi sir, how can i encode an image to a qr code using Zxing ?

Tell us what you think!