生成黑白二维码
导入依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
zxing相关依赖
谷歌的zxing:生成普通的黑白二维码
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
搭建前端页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>生成二维码</h2>
请输入内容<input type="text" id="url"><button type="button" onclick="btn()">生成二维码</button>
<img id="erm"/>
</body>
<script>
function btn(){
let url= document.getElementById("url").value;
let erwmImg = document.getElementById("erm");
//src 跳转到后台路径
erwmImg.src="/myqrcode/qrcode?url="+url;
}
</script>
</html>
效果
搭建后台
创建QRCodeServlet 继承HttpServlet 重写doGet方法
try{
//使用谷歌开源库实现生成黑白二维码
//1.获取前端文本内容
String url=req.getParamter("url");
//2.创建一个Map集合 用Map集合存储二维码相关属性
//2.1设置二维码的误差校正级别
map.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.H);
//2.2设置二维码字符集
map.put(EncodeHintType.CHARACTER_SET,"utf-8");
//设置二维码四周留白
map.put(EncodeHintType.MARGIN,1);
//3.创建zxing的核心对象 MultiFormatWriter 多格式写入器
//通过MultiFormatWriter对象生成二维码
MultiFormatWriter mu = new MultiFormatWriter();
//mu.encode(内容,格式,宽度,长度,参数)
//返回bitMatrix 是一个矩阵 实际是一个二维数组 元素是布尔值 true fasle
//true白 false黑
BitMatrix bitMatrix= mu.encode(url, BarcodeFormat.QR_CODE,300,300,map);
//获取矩阵宽度
int width=bitMatrix.getWidth();
//获取矩阵高度
int height=bitMatrix.getHeight();
//利用BufferedImage 生成二维码
BufferedImage image = new BufferedImage();
//4.遍历二维数组
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
image.setRGB(i,j,bitMatrix.get(i,j)?0xFF000000:0xFFFFFFFF);
}
}
//5.将图片响应到浏览器
ServletOutputStream out = resp.getOutputStream();
ImageIO.write(image,"png",out);
ImageIO.setUseCache(false);
out.flush();
out.close();
} catch (WriterException e) {
e.printStackTrace();
}
}
最后效果
生成带logo二维码
后端代码改动
@WebServlet("/路径")
//fileSizeThreshold 能够存放的缓存大小 maxFileSize 图片最大值 maxRequestSize 请求最大值
@MulitPartConfig(fileSizeThreshold=1024*1024*2,maxFileSize=1024*1024*10,maxRequestSize=1024*1024*100)
public class QrCodeServlertest extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try{
//生成带logo的二维码
//1.获取前端传的二维码内容
String content=req.getParameter("content");
//2.创建map存放生成二维矩阵的参数
Map map = new HashMap();
//2.1校正校验
map.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionlevel.H);
//2.2设置编码
map.put(EncodeHintType.CHARACTER_SET,"utf-8");
//2.3设置四周留白
mao.put(EncodeHintType.MARGIN,1);
//生成二维矩阵
//3.创建多格式写入器
MulitFormatWirte wr =new MulitFormatWirte();
BitMatrix encode=wr.encode(content,BarcodeFormat.QR_CODE,400,400,map);
//3.1获取高宽
int w = encode.getWidth();
int h = encode.getHeight();
//4.生成二维码
BufferImage image =new BufferImage(w,h,BufferImage.TYPE_INT_BGR);
//遍历矩阵
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
image.setRGB(i,j,encode.get(i,j)?0x000000:0xFFFFFF);
}
}
//到这黑白二维码生成了
//获取logo
Part logo= req.getPart("logo");
InputStream in = logo.getInputStream();
Image read = ImageIO.read(in);
//设置宽高
int widthlogo = read.getWidth(null);
int heigthlogo = read.getHeight(null);
if (widthlogo>60){
widthlogo=60;
}
if (heigthlogo>60){
heigthlogo=60;
}
//使用平滑算法将图片缩放
Image sfimage =read.getScaledInstance(widthlogo, heigthlogo, Image.SCALE_SMOOTH);
//生成2D笔 BuffImage下的createGraphics方法 生成2D笔
Graphics2D graphics=image.createGraphics();
//计算图片放入的位置
int newWidth = (400-widthlogo)/2;
int newHeight = (400-heigthlogo )/2;
//开始将logo画入二维码
graphics.drawImage(sfimage,newWidth,newHeight,null);
//设置logo边的弧度
Shape round=new new RoundRectangle2D.Float(width, height, widthlogo, heigthlogo, 10, 10);
//设置 2D的笔触宽度
graphics.setSocket(new BaisSocket(4.0f));
graphics.drow(round);
graphics.dispose();
//将二维码响应到浏览器
ServletOutputStream out = resp.getOutputStream();
ImageIO.wire(image,"png",out);
}catch (WriterException e) {
e.printStackTrace();
}
}
}
实现效果