生成二维码

这是google的一个二维码工具(2个jar都是zxing的)

导入core-3.3.0.jar

下载链接: https://pan.baidu.com/s/1kU8RcEF 密码: ixdh

使用的时候,定义一个方法即可:

package com.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
* Created by 92307 on 2017/5/24 0024.
*/
public class QrCodeUtils {
//二维码颜色
private static final int BLACK = 0xFF000000;
//二维码颜色
private static final int WHITE = 0xFFFFFFFF;

/**
* ZXing 方式生成二维码
*
* @param text 二维码内容
* @param width 二维码宽
* @param height 二维码高
* @return BufferedImage
*/
public static BufferedImage createQrcode(String text, int width, int height) {
BufferedImage image = null;
Map<EncodeHintType, String> his = new HashMap<>();
//设置编码字符集
his.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
//1、生成二维码
BitMatrix encode = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, his);

//2、获取二维码宽高
int codeWidth = encode.getWidth();
int codeHeight = encode.getHeight();

//3、将二维码放入缓冲流
image = new BufferedImage(codeWidth, codeHeight, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < codeWidth; i++) {
for (int j = 0; j < codeHeight; j++) {
//4、循环将二维码内容定入图片
image.setRGB(i, j, encode.get(i, j) ? BLACK : WHITE);
}
}
} catch (WriterException e) {
e.printStackTrace();
}
return image;//返回的BufferedImage,在Controller中,可以用ServletOutputStream out = response.getOutputStream();ImageIO.write(image, "jpg", out);

}

/**
* 生成 并保存二维码,返回图片路径 * @param contents 二维码内容 * @param width 图片宽度 * @param height 图片高度 * @return 图片路径
*/
public static String getImgAndSave(String contents, int width, int height) {
BarcodeFormat format = BarcodeFormat.QR_CODE;//格式:二维码格式
String format_img = "png";//文件格式
Map<EncodeHintType, String> hints = new HashMap<>();//Map<EncodeHintType,?> hints
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");//设置编码格式
Random random = new Random();
String imgUrl = "D:\\Program Files/project/qrcode/" + random.nextInt() + "." + format_img;//先指定好一个文件(虚的【不存在】)(包含文件名:到时候会生成这个文件)
Path path = new File(imgUrl).toPath();
MultiFormatWriter writer = new MultiFormatWriter();
try {
BitMatrix bitMatrix = writer.encode(contents, format, width, height, hints);
MatrixToImageWriter.writeToPath(bitMatrix, format_img, path);
} catch (WriterException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return imgUrl;
}

public static void main(String[] args) {
String contents = "wode名字";
String imgUrl = getImgAndSave(contents, 200, 200);
System.out.println(imgUrl);
}
}

 

如果使用到 这个方法的时候:getImgAndSave;还需要导入一个jar包(javase-3.3.0.jar);下载地址:链接: https://pan.baidu.com/s/1o88vj8Q 密码: 23u6

 
示例:

在页面中显示二维码:

<img src="${pageContext.request.contextPath}/qrcode?name=我的名字">

后台代码:

1 @RequestMapping(value = "/qrcode")
2     public void qrCode(HttpServletResponse response, String name) throws IOException {
3         //把接收到的参数name 生成到二维码里面
4         BufferedImage qrcode = QrCodeUtils.createQrcode(name, 200, 200);
5         response.setContentType("image/jpeg");
6         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(response.getOutputStream());
7         encoder.encode(qrcode);
8     }

 

posted @ 2017-05-24 11:36  戴眼镜的蚂蚁  阅读(445)  评论(0编辑  收藏  举报