Java QR code

  • QR Code Generator

Utilize google QR code generator …
Define the charset, format, and size.
Use MatrixToImageWriter to write a BitMatrix to BufferedImage, file or stream.

toBufferedImage method

public static BufferedImage toBufferedImage(BitMatrix matrix) {...}

encode method

public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException {...}

decode method

public Result decode(BinaryBitmap image, Map<DecodeHintType, ?> hints) throws NotFoundException {...}

Available BarcodeFormat

public enum BarcodeFormat {
AZTEC,
CODABAR,
CODE_39,
CODE_93,
CODE_128,
DATA_MATRIX,
EAN_8,
EAN_13,
ITF,
MAXICODE,
PDF_417,
QR_CODE,
RSS_14,
RSS_EXPANDED,
UPC_A,
UPC_E,
UPC_EAN_EXTENSION;
}

An example goes here …

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.*;

public class ZxingUtil {
private static final Map<EncodeHintType, ErrorCorrectionLevel> encodeMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>();
private static final Map<DecodeHintType, ErrorCorrectionLevel> decodeMap = new HashMap<DecodeHintType, ErrorCorrectionLevel>();
private static final String charset="UTF-8", format="png";
private static final int size = 150;
private ZxingUtil() {}

/**
* QR code encoder
*
* @param data data to be encoded
* @throws WriterException
* @throws IOException
*/
public static BufferedImage createQRCode(String data)
throws WriterException, IOException {
return MatrixToImageWriter.toBufferedImage(new MultiFormatWriter()
.encode(new String(data.getBytes(charset), charset),
BarcodeFormat.QR_CODE, size, size, encodeMap));
}

/**
* QR code decoder
*
* @param image QR code image
* @return
* @throws FileNotFoundException
* @throws IOException
* @throws NotFoundException
*/
public static String readQRCode(byte[] image)
throws FileNotFoundException, IOException, NotFoundException {
return new MultiFormatReader().decode(
new BinaryBitmap(
new HybridBinarizer(
new BufferedImageLuminanceSource(
ImageIO.read(new ByteArrayInputStream(image))))), decodeMap).getText();
}

}

  • Example to Create QR Code

An example of using above method to create QR code …

/**
* create user QR scan code
* @param userScanCodeDTO
* @return Optional<BufferedImage>
*/
public Optional<Image> createUserQRCode(@NotNull UserScanCodeDTO userScanCodeDTO) {
log.debug("create user QR code : {}", userScanCodeDTO);
return Optional.of(userScanCodeDTO)
.map(usc -> {
BufferedImage bufferedImage = null;
try {
bufferedImage = ZxingUtil.createQRCode(usc.getScanCode());
} catch (Exception e) {
log.debug(e.getMessage());
}

return bufferedImage;
})
.map(bufferedImage -> {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
Image image = null;
try {
ImageIO.write(bufferedImage, "png", bs);
image = new Image()
.name(userScanCodeDTO.getScanCode())
.image(bs.toByteArray())
.imageContentType("image/png");
bs.close();
} catch (IOException e) {
log.debug(e.getMessage());
}
return image;
});
}

Ref:

  1. ZXing 3.3.0 API