一、导入依赖
<dependencies>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
二、生成/解析二维码
public class QRCodeUtil {
//二维码宽度,单位像素
private static final int CODE_WIDTH = 400;
//二维码高度,单位像素
private static final int CODE_HEIGHT = 400;
//二维码图片格式
private static final String FORMAT = "jpg";
//编码格式
private static final String CHARSET = "UTF-8";
//默认二维码文件夹路径
private static final String DEFAULT_FILE_DIR = "E:/tmp/qrcode/";
//二维码参数
private static Map<EncodeHintType, Object> hints = new HashMap();
static{
//设置字符编码类型
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
//设置纠错等级L/M/Q/H,纠错等级越高越不易识别,当前设置等级为最高等级H
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
//设置二维码边距,单位像素,值越小二维码距离四周越近
hints.put(EncodeHintType.MARGIN, 1);
}
//------------------------------生成二维码------------------------------
/**
* 生成二维码,写入文件
* @param codeContent 二维码内容
* @param fileName 文件名称
* @return
*/
public static String createQRFile(String codeContent, String fileName) throws Exception {
File file = new File(DEFAULT_FILE_DIR, fileName + "." + FORMAT);
//生成并保存二维码图片文件
BitMatrix bitMatrix = new MultiFormatWriter().encode(codeContent, BarcodeFormat.QR_CODE, CODE_WIDTH, CODE_HEIGHT, hints);
MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, file.toPath());
return file.getPath();
}
/**
* 生成二维码,写入输出流(例如:response.getOutputStream())
* @param codeContent 二维码内容
* @param out 输出流
*/
public static void createQROutput(String codeContent, OutputStream out) throws Exception {
BitMatrix bitMatrix = new MultiFormatWriter().encode(codeContent, BarcodeFormat.QR_CODE, CODE_WIDTH, CODE_HEIGHT, hints);
MatrixToImageWriter.writeToStream(bitMatrix, FORMAT, out);
}
//------------------------------解析二维码------------------------------
/**
* 解析二维码文件
* @param filePath 文件路径
* @return
*/
public static String parseQRFile(String filePath) throws Exception {
File file = new File(filePath);
if (file == null || !file.exists() || file.isDirectory()) {
return null;
}
BufferedImage bufferedImage = ImageIO.read(file);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
Hashtable hints = new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
Result result = new MultiFormatReader().decode(bitmap, hints);
return result.getText();
}
/**
* 解析网络URL二维码
* @param urlPath 二维码图片网络地址
* @return
*/
public static String parseQRUrl(String urlPath) throws Exception {
URL url = new URL(urlPath);
BufferedImage bufferedImage = ImageIO.read(url);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
Hashtable hints = new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
Result result = new MultiFormatReader().decode(bitmap, hints);
return result.getText();
}
}
注意:若想生成条形码,则只需修改 BarcodeFormat.QR_CODE为 BarcodeFormat.CODE_128,再修改图形长宽为(500, 250)即可