生成二维码的方式有很多,在此简单介绍Google的zxing使用
先加载需要用到的包,pom.xml文件里面添加:
<!-- 二维码生成 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>2.2</version>
</dependency>
工具类代码
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import net.sf.json.JSONObject;
/**
*
* 处理图片url
*/
public class QRCodeUtil {
public static void main(String[] args) throws WriterException, IOException {
testEncode();
testDecode();
}
/**
* 生成图像
*
* @throws WriterException
* @throws IOException
*/
public static void testEncode() throws WriterException, IOException {
String filePath = "D://";
String fileName = "text.png";
JSONObject json = new JSONObject();
json.put("url", "https://www.baidu.com");
json.put("author", "zwm");
String content = json.toString();// 内容
int width = 200; // 图像宽度
int height = 200; // 图像高度
String format = "png";// 图像类型
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
Path path = FileSystems.getDefault().getPath(filePath, fileName);
File file = new File(filePath + fileName);
MatrixToImageWriter.writeToFile(bitMatrix, format, file);// 输出图像
System.out.println("图片文件已经生成.请于D盘查找");
}
/**
* 解析图像
*/
public static void testDecode() {
String filePath = "D://text.png";
BufferedImage image;
try {
image = ImageIO.read(new File(filePath));
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码
JSONObject content = JSONObject.fromObject(result.getText());
System.out.println("图片中内容: ");
System.out.println("author:" + content.getString("author"));
System.out.println("url: " + content.getString("url"));
System.out.println("图片中格式: ");
System.out.println("encode: " + result.getBarcodeFormat());
} catch (IOException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
}
}
public static BufferedImage createQRCode(final String url) throws WriterException, IOException {
BufferedImage image = null;
// 二维码图片输出流
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
HashMap<EncodeHintType, Comparable> hints = new HashMap<EncodeHintType, Comparable>();
// 设置编码方式
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 设置QR二维码的纠错级别(H为最高级别)具体级别信息
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
BitMatrix bitMatrix = multiFormatWriter.encode(url, BarcodeFormat.QR_CODE, 400, 400, hints);
bitMatrix = updateBit(bitMatrix, 10);
image = MatrixToImageWriter.toBufferedImage(bitMatrix);
return image;
}
/**
* 自定义白边边框宽度
*
* @param matrix
* @param margin
* @return
*/
private static BitMatrix updateBit(final BitMatrix matrix, final int margin) {
int tempM = margin * 2;
// 获取二维码图案的属性
int[] rec = matrix.getEnclosingRectangle();
int resWidth = rec[2] + tempM;
int resHeight = rec[3] + tempM;
// 按照自定义边框生成新的BitMatrix
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
// 循环,将二维码图案绘制到新的bitMatrix中
for (int i = margin; i < resWidth - margin; i++) {
for (int j = margin; j < resHeight - margin; j++) {
if (matrix.get(i - margin + rec[0], j - margin + rec[1])) {
resMatrix.set(i, j);
}
}
}
return resMatrix;
}
}
直接运行即可看到效果,其中 createQRCode是用于直接将生成的二维码以流的形式在页面上输出,调用方法如下:
/**
* 根据URL生成二维码
* @param model
* @param request
* @param response
* @throws MaHuaServiceHandleException
*/
@RequestMapping("/getQRCode")
public void getQRCode(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean,HttpServletRequest request, HttpServletResponse response){
String url = "http://www.baidu.com";
//二维码图片输出流
OutputStream out = null;
try{
response.setContentType("image/jpeg;charset=UTF-8");
BufferedImage image = QRCodeUtil.createQRCode(url);
//实例化输出流对象
out = response.getOutputStream();
//画图
ImageIO.write(image, "png", response.getOutputStream());
out.flush();
out.close();
}catch (Exception e){
e.printStackTrace();
}finally {
try{
if (null != response.getOutputStream()) {
response.getOutputStream().close();
}
if (null != out) {
out.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
在页面上调用此方法:
<div style="height:100%;width:100%"><img style="width:100%" id="QRCode" src="./activity/getQRCode.do?" /></div>
这样生成的二维码则会再这个div的地方显示出来