要用到一个jar包QRcode.jar
主要用到了com.swetake.util.Qrcode这个类。API链接如下:
http://www.swetake.com/qrcode/java/docs/index.html
以本人博客首页为例:
代码如下:(生成一个固定内容的二维码以图片形式输出到文件中)
CreateTwoBarImage.java(一个普通的java类)
package com.lmb;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
public class CreateTwoBarImage {
public void creatTxm(String param) throws Exception {
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M');
qrcode.setQrcodeEncodeMode('B');
qrcode.setQrcodeVersion(7);
byte[] bstr = param.getBytes("UTF-8");//返回用指定名字命名的字节数组值
BufferedImage bi = new BufferedImage(139, 139,
BufferedImage.TYPE_INT_RGB);//实例化指定参数的BufferedImage
Graphics2D g = bi.createGraphics();//返回一个呈现指定 BufferedImage 的 Graphics2D 对象
g.setBackground(Color.WHITE); // 设置该Graphics2D 对象的背景颜色
g.clearRect(0, 0, 139, 139); //擦除指定的矩形,并且用一个透明的颜色填充它
g.setColor(Color.BLACK); // 条码颜色
if (bstr.length > 0 && bstr.length < 123) {
boolean[][] b = qrcode.calQrcode(bstr); //通过calQrcode函数将byte数组转换成boolean数组 ,然后依据编码后的boolean数组绘图
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length; j++) {
if (b[j][i]) {
g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3); //填充指定的矩形
}
}
}
}
g.dispose(); //处理图形上下文,并释放系统资源
bi.flush();//将生成的BufferedImage序列化到磁盘
String FilePath = "F:/私人物品刘梦冰/学习资料/exercise/" + param + ".jpg";//生成的二维码要存放的文件路径
File f = new File(FilePath);
ImageIO.write(bi, "jpg", f);//将生成的二维码以图片的形式写入相应的文件
}
public static void main(String args[]) {
try {
new CreateTwoBarImage().creatTxm("lmb");
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果:
扫码之后显示的就是一个字符串“lmb”,如果我们想通过扫码访问一个网址,该怎么改进呢?
代码如下:(生成二维码显示到网页上)
PrintTwoBarCodeServlet、qrcode.jsp
PrintTwoBarCodeServlet.java
package com.lmb;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.swetake.util.Qrcode;
public class PrintTwoBarCodeServlet extends HttpServlet {
/**
* 专门用来生成二维码图片的servlet
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String code = request.getParameter("code");
Qrcode testQrcode = new Qrcode();
testQrcode.setQrcodeErrorCorrect('M');
testQrcode.setQrcodeEncodeMode('B');
testQrcode.setQrcodeVersion(7);
byte[] d = code.getBytes("gbk");
BufferedImage image = new BufferedImage(98, 98,
BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g = image.createGraphics();
g.setBackground(Color.WHITE);
g.clearRect(0, 0, 98, 98);
g.setColor(Color.BLACK);
if (d.length > 0 && d.length < 120) {
boolean[][] s = testQrcode.calQrcode(d);
for (int i = 0; i < s.length; i++) {
for (int j = 0; j < s.length; j++) {
if (s[j][i]) {
g.fillRect(j * 2 + 3, i * 2 + 3, 2, 2);
}
}
}
}
g.dispose();
image.flush();
ImageIO.write(image, "jpg", response.getOutputStream());
}
}
qrcode.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>显示页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
.box{width:400px;height:300px;margin:0 auto;
border:1px solid #39b2e2;text-align:center;
color:red;}
</style>
</head>
<body>
<div class="box">
<h2>扫一扫开启爱的密码</h2>
<img src="printTwoBarCode.do?code=http://blog.csdn.net/lmb55">
</div>
</body>
</html>
生成的二维码:
扫一扫的显示: