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 methodpublic static BufferedImage toBufferedImage(BitMatrix matrix) {...}
encode methodpublic BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException {...}
decode methodpublic 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 …
/** |
Ref: