使用場景
應對一些需要生成二維碼的地方,
例如:
- 二維碼超鏈接
- 二維碼支付等場景
引入jar包
<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
使用方式
createQrCode(new FileOutputStream(new File(filePath)),"www.baidu.com",900,"JPEG");
第一個參數:
傳入文件輸出流第二個參數:
傳入二維碼內容第三個參數:
二維碼大小第四個參數:
文件類型
源碼
package com.wang.util;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Hashtable;
/**
* @Author: WanG
* @Date: 2019-04-16 15:14
* @version: v1.0
* @description: TODO
*/
public class QrCodeUtil {
/**
* 生成包含字符串信息的二維碼圖片
* @param outputStream 文件輸出流路徑
* @param content 二維碼攜帶信息
* @param qrCodeSize 二維碼圖片大小
* @param imageFormat 二維碼的格式
* @throws WriterException
* @throws IOException
*/
public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException{
//設置二維碼糾錯級別MAP
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 矯錯級別
QRCodeWriter qrCodeWriter = new QRCodeWriter();
//創建比特矩陣(位矩陣)的QR碼編碼的字符串
BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
// 使BufferedImage勾畫QRCode (matrixWidth 是行二維碼像素點)
int matrixWidth = byteMatrix.getWidth();
BufferedImage image = new BufferedImage(matrixWidth-200, matrixWidth-200, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
// 使用比特矩陣畫並保存圖像
graphics.setColor(Color.BLACK);
for (int i = 0; i < matrixWidth; i++){
for (int j = 0; j < matrixWidth; j++){
if (byteMatrix.get(i, j)){
graphics.fillRect(i-100, j-100, 1, 1);
}
}
}
return ImageIO.write(image, imageFormat, outputStream);
}
/**
* 讀二維碼並輸出攜帶的信息
*/
public static void readQrCode(InputStream inputStream) throws IOException{
//從輸入流中獲取字符串信息
BufferedImage image = ImageIO.read(inputStream);
//將圖像轉換爲二進制位圖源
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
Result result = null ;
try {
result = reader.decode(bitmap);
} catch (ReaderException e) {
e.printStackTrace();
}
System.out.println(result.getText());
}
/**
* 讀取二進制文件流
*
* @param pdfPath 文件物理路徑
* @return
*/
public static ByteArrayOutputStream readStream(String pdfPath) {
return PdfUtil.readPdfStream(pdfPath);
}
/**
* 測試代碼
* @throws WriterException
*/
public static void main(String[] args) throws IOException, WriterException {
String filePath = "/Users/WanG/Desktop/qrtest.jpg";
createQrCode(new FileOutputStream(new File(filePath)),"www.baidu.com",900,"JPEG");
readQrCode(new FileInputStream(new File(filePath)));
}
}