前言:
前两天,因为项目的需要生成二维码,以前也没有弄完。于是与百度一下发现还是有很多代码可以借鉴的,
于是乎就找到一篇文章参考,完成了任务。在此向开源奉献的程序员们表示致敬。
本文参考:http://blog.csdn.net/sxg0205/article/details/50887057
http://jingyan.baidu.com/article/25648fc1902dbf9191fd0000.html
jar包依赖:可以从下面链接下载
下载地址:http://download.csdn.net/download/xinghuo0007/10103887
说明:如果没有积分,可以QQ1227387823给我要。
生成工具二维码工具类:
/**
* Created by xy on 2017/10/19.
* @author zxy
* 生成二维码工具类
*/
public class QRCodeUtil {
/**
* 二维码生成器
*
* @param content content可以是一个url 也可以是文本内容
* @return String 成功返回文件的base64字符串;失败返回null
* @throws IOException
*/
public static String generalQRCodeStr(String content) {
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
String binary = null;
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(
content, BarcodeFormat.QR_CODE, 200, 200, hints);
/* 实现一: 输出图片到指定目录
File outputFile = new File("d://1.jpg");
MatrixToImageWriter.writeToFile(bitMatrix, "png", outputFile);
*/
// 实现二:生成二维码图片并将图片转为二进制传递给前台
// 1、读取文件转换为字节数组
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedImage image = toBufferedImage(bitMatrix);
ImageIO.write(image, "jpg", out);
byte[] bytes = out.toByteArray();
// 2、将字节数组转为二进制
BASE64Encoder encoder = new BASE64Encoder();
binary = encoder.encodeBuffer(bytes).trim();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return binary;
}
/**
* 二维码生成器
* @param content content可以是一个url 也可以是文本内容
* @param filePath 生成文件的路径,包含文件名称
* @return boolean 成功true;失败false
* @throws IOException
*/
@SuppressWarnings({"unchecked", "rawtypes", "restriction"})
public static boolean generalQRCodeFile(String content, String filePath) {
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(
content, BarcodeFormat.QR_CODE, 200, 200, hints);
// 实现一: 输出图片到指定目录
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();//文件父级创建目录不存在,则创建
file.createNewFile();//创建文件
}
MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 辅助方法
* 生成图片
* */
private 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) ? 0xFF000000 : 0xFFFFFFFF);
}
}
return image;
}
}
使用方法:
案例一:在页面上直接引用
后台代码:
String url = "https://www.baidu.com";
//生成base64格式,可以在页面上直接引用
String base64QRCode = QRCodeUtil.generalQRCodeStr(url);
request.setAttrubute("base64QRCode",base64QRCode );
前台代码:
//前台页面:
<div class="align_center" style="overflow: hidden">
<span style="margin-top: 81px;margin-left: 49px;display: inline-block">百度二维码:</span>
<img src="data:image/jpg;base64,${base64QRCode}" style="display: inline-block;float: right;margin-right: 38px;">
</div>
注意:使用<img>标签,src前面一定要有 data:image/jpg;base64,
案例二:生成文件
String url = "https://www.baidu.com";
String filePath = "D://test//测试二维码.jpg";
boolean isSuccess = QRCodeUtil.generalQRCodeFile(url, filePath);
if(isSuccess ){
System.out.print("生成成功!");
}else{
System.out.print("生成失败!");
}