Parthiban
Parthiban

Reputation: 105

QR code generation in Java using ZXing ("Zebra Crossing") API

The below code generate QR code successfully.

The height and width of the QR code vary based on the input qrCodeData we given.

Fox example if qrCodeData=Hello World! then height and width of generated QR code is low. If qrCodeData=A mobile phone is a portable telephone that can make and receive calls over a radio frequency link while the user is moving within a telephone service area. The radio frequency link establishes a connection to the switching systems of a mobile phone operator, which provides access to the public switched telephone network (PSTN) then height and width of generated QR code is larger.

Attached sample QR codes. for less data for large data

I want to generate QR code with same height and width irrespective to the data given in qrCodeData. Some one advise me. Thanks in advance.

package com.javapapers.java;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QRCode {

public static void main(String[] args) throws WriterException, IOException,
        NotFoundException {
String qrCodeData = "Hello World!";
String filePath = "QRCode.png";
String charset = "UTF-8"; // or "ISO-8859-1"
Map<EncodeHintType, ErrorCorrectionLevel> hintMap = new                                                                      HashMap<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

    createQRCode(qrCodeData, filePath, charset, hintMap, 200, 200);
    System.out.println("QR Code image created successfully!");

    System.out.println("Data read from QR Code: "
            + readQRCode(filePath, charset, hintMap));

}

public static void createQRCode(String qrCodeData, String filePath,
        String charset, Map hintMap, int qrCodeheight, int qrCodewidth)
        throws WriterException, IOException {
    BitMatrix matrix = new MultiFormatWriter().encode(
            new String(qrCodeData.getBytes(charset), charset),
            BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight, hintMap);
    MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath
            .lastIndexOf('.') + 1), new File(filePath));
}

public static String readQRCode(String filePath, String charset, Map hintMap)
        throws FileNotFoundException, IOException, NotFoundException {
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
            new BufferedImageLuminanceSource(
                    ImageIO.read(new FileInputStream(filePath)))));
    Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap,
            hintMap);
    return qrCodeResult.getText();
}
}

Upvotes: 3

Views: 6502

Answers (1)

Thomas Fritsch
Thomas Fritsch

Reputation: 10127

Your images (to be more precisely: the pixel parts of the images) have different sizes, because there is a white margin around it. By default this margin, the so-called QR code quiet zone, has a width of 4. Hence, to get rid of the margin you can set it to 0.
Modify your main method like this:

Map<EncodeHintType, Object> hintMap = new HashMap<EncodeHintType, Object>();
hintMap.put(EncodeHintType.MARGIN, 0);
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

createQRCode(qrCodeData, filePath, charset, hintMap, 200, 200);

Then the resulting images have no margin and the pixel parts have same size.

enter image description here enter image description here

Upvotes: 2

Related Questions