ZXing(Zebra Crossing) is a two-dimensional code parsing and generation open source library developed by Google. ZXing GitHub address import
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>
Copy the code
public class QRCodeUtil {
/** * Generate a QR code *@paramContent Source content *@paramOutputStream outputStream *@throws Exception
*/
public static void createQRImage(String content, OutputStream outputStream) throws Exception {
Hashtable hints = new Hashtable();
// Specify the error correction level
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300.300,
hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); }}// Save to disk
ImageIO.write(image, "jpg", outputStream);
outputStream.flush();
outputStream.close();
}
public static void createQRImage2(String content,OutputStream outputStream) throws Exception {
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300.300,
hints);
// Generate a PNG image and save it to imgPath
MatrixToImageWriter.writeToStream(bitMatrix, "jpg",outputStream);
outputStream.flush();
outputStream.close()
}
public static void createQRImage2(String content,File file) throws Exception {
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300.300,
hints);
// Generate a PNG image and save it to imgPath
MatrixToImageWriter.writeToFile(bitMatrix, "jpg",file);
}
public static void main(String[] args) throws Exception {
String codeUrl = "http://www.baidu.com";
// Use the order number as the image name of the QR code
File file = new File("E:\\"."Test 3" + ".jpg");
FileOutputStream out=newFileOutputStream(file); createQRImage(codeUrl, out); System.out.println(file.delete()); }}Copy the code
There are many kinds of bar codes, and two-dimensional (bar) codes are one of them. The content is also different, some can only represent pure numbers, can not represent letters.
BarcodeFormat.CODE_128; Barcodeformat.code_39; barcodeformat.code_39; BarcodeFormat.CODE_93; BarcodeFormat.CODABAR; // Barcodeformat. DATA_MATRIX; // Barcodeformat. DATA_MATRIX; // Barcodeformat. DATA_MATRIX; BarcodeFormat.EAN_8; BarcodeFormat.EAN_13; BarcodeFormat.ITF; BarcodeFormat.PDF417; // barcodeformat.qr_code; // barcodeformat.rss_expanded; BarcodeFormat.RSS14; BarcodeFormat.UPC_E; // Unified product code E:7 digits, the last digit is the check bit barcodeformat.upc_a; // universal product code A:12 digits, the last digit is the check bit barcodeformat.upc_ean_extension;Copy the code