依赖:
<!-- 二维码生成依赖包 https://mvnrepository.com/artifact/com.google.zxing/core -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.2</version>
</dependency>
<!-- demo其他依赖 https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
<!-- demo其他依赖 https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
具体实现:
/*
* @(#)QRUtil.java 2019-1-4 下午12:19:59
* QRcode
*/
package com.binmma.test;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.imageio.ImageIO;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
* QRUtil
* @author binmma
* @version 1.0
*
*/
public class QRUtil {
/** 黑 */
private static final int BLACK = 0xFF000000;
/** 白 */
private static final int WHITE = 0xFFFFFFFF;
@org.junit.Test
public void CreatQRImage() throws IOException {
CreatQRImage("http://www.baidu.com", 100, "e://", "jpg");
}
@org.junit.Test
public void createQRcode() throws IOException {
BufferedImage bufferedImage = createQRcode("http://www.baidu.com", 100);
}
@org.junit.Test
public void QRcodeBase64() throws IOException {
String rcodeBase64 = QRcodeBase64("http://www.baidu.com", 100);
}
/**
* 创建二维码图片<br/>
*
* @param content 二维码内容
* @param size 尺寸
* @param filePath 下载路径
* @param fileFormat 文件格式
* @throws IOException
*/
public static void CreatQRImage(String content, Integer size,
String filePath, String fileFormat) throws IOException {
BufferedImage qRcode = createQRcode(content, size);
ImageIO.write(qRcode, fileFormat,
new File(filePath, StringUtils.replace(
UUID.randomUUID().toString(), "-", "")
+ "." + fileFormat));
qRcode.flush();
}
/**
* 二维码 文件流<br/>
*
* @param content 二维码内容
* @param size 尺寸
* @return
* @throws IOException
*/
public static BufferedImage createQRcode(String content, Integer size) throws IOException {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
int defaultSize = 150;
if (size == null) {
size = defaultSize;
}
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 设定内容编码格式
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);// 设定纠错级别
hints.put(EncodeHintType.MARGIN, 0);// 设定图片周围留白,0表示不留
hints.put(EncodeHintType.MAX_SIZE, size);
hints.put(EncodeHintType.MIN_SIZE, size);
BufferedImage bi = null;
try {
BitMatrix bitMatrix = multiFormatWriter.encode(content,
BarcodeFormat.QR_CODE, size, size, hints);
BitMatrix deleteWhite = deleteWhite(bitMatrix);
bi = toBufferedImage(deleteWhite);
bi = zoomInImage(bi, size, size);//根据size放大、缩小生成的二维码
} catch (Exception e) {
}
return bi;
}
/**
* 生成二维码串<br/>
*
* @param content
* @param size
* @return
* @throws IOException
*/
public static String QRcodeBase64(String content, Integer size) throws IOException {
ByteArrayOutputStream os = null;
BufferedImage bi;
try {
bi = createQRcode(content, size);
os = new ByteArrayOutputStream();
ImageIO.write(bi, "png", os);
content = Base64.encodeBase64String(os.toByteArray());
} catch (Exception e) {
// log.error("二维码转码失败", e);
return StringUtils.EMPTY;
} finally {
os.close();
// IOUtils.closeQuietly(os);
}
return content;
}
/**
* 转缓冲图片<br>
*
* @param matrix 二维码矩阵
* @return
*/
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
/**
*
*去掉白边<br>
*
* @param matrix 二维码矩阵
* @return
*/
public static BitMatrix deleteWhite(BitMatrix matrix) {
int[] rec = matrix.getEnclosingRectangle();
int resWidth = rec[2] + 1;
int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = 0; i < resWidth; i++) {
for (int j = 0; j < resHeight; j++) {
if (matrix.get(i + rec[0], j + rec[1]))
resMatrix.set(i, j);
}
}
return resMatrix;
}
/**
*
* 设定图片大小<br/>
* @param originalImage
* @param width
* @param height
* @return
*/
public static BufferedImage zoomInImage(BufferedImage originalImage,
int width, int height) {
BufferedImage newImage = new BufferedImage(width, height,
originalImage.getType());
Graphics g = newImage.getGraphics();
g.drawImage(originalImage, 0, 0, width, height, null);
g.dispose();
return newImage;
}
}