二维码生成需要借助第三方jar包。我这里使用的是QRcode。给大家一个连接,自行下载:http://download.csdn.net/detail/xiaokui_wingfly/7957815
生成二维码代码:
public void encoderQRCoder(String content, HttpServletResponse response) {
try {
Qrcode handler = new Qrcode();
handler.setQrcodeErrorCorrect('M');
handler.setQrcodeEncodeMode('B');
handler.setQrcodeVersion(7);
byte[] contentBytes = content.getBytes("UTF-8");
BufferedImage bufImg = new BufferedImage(140, 140, BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufImg.createGraphics();
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, 140, 140);
//设定图像颜色:BLACK
gs.setColor(Color.BLACK);
//设置偏移量 不设置肯能导致解析出错
int pixoff = 2;
//输出内容:二维码
if (contentBytes.length > 0 && contentBytes.length < 124) {
boolean[][] codeOut = handler.calQrcode(contentBytes);
for (int i = 0; i < codeOut.length; i++) {
for (int j = 0; j < codeOut.length; j++) {
if (codeOut[j][i]) {
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
} else {
System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,120 ]. ");
}
gs.dispose();
bufImg.flush();
OutputStream outputStream = response.getOutputStream();
ImageIO.write(bufImg, "PNG", outputStream);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
第一个参数是链接的内容,比如:https://www.baidu.com
第二个是HttpServletResponse,为什么要传这个参数呢,我是让二维码直接显示到jsp中,在jsp页面,定义:
<i mg src="code">
code是我servlet,让src指向这个servlet。好吧,可能我表达的不够清除,我把完整代码贴一下:
public class CodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public CodeServlet() {
super();
}
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
// TODO Auto-generated method stub
super.service(arg0, arg1);
String content = "http://www.baidu.com";
encoderQRCoder(content, arg1);
}
public void encoderQRCoder(String content, HttpServletResponse response) {
try {
Qrcode handler = new Qrcode();
handler.setQrcodeErrorCorrect('M');
handler.setQrcodeEncodeMode('B');
handler.setQrcodeVersion(-1);
System.out.println(content);
byte[] contentBytes = content.getBytes("UTF-8");
BufferedImage bufImg = new BufferedImage(80, 80, BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufImg.createGraphics();
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, 140, 140);
//设定图像颜色:BLACK
gs.setColor(Color.BLACK);
//设置偏移量 不设置肯能导致解析出错
int pixoff = 2;
//输出内容:二维码
if(contentBytes.length > 0 && contentBytes.length < 124) {
boolean[][] codeOut = handler.calQrcode(contentBytes);
for(int i = 0; i < codeOut.length; i++) {
for(int j = 0; j < codeOut.length; j++) {
if(codeOut[j][i]) {
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff,3, 3);
}
}
}
} else {
System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,120 ]. ");
}
gs.dispose();
bufImg.flush();
response.reset();
//生成二维码QRCode图片
OutputStream outputStream = response.getOutputStream();
ImageIO.write(bufImg, "png", outputStream);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
<servlet>
<servlet-name>CodeService</servlet-name>
<servlet-class>com.webgo.servlets.CodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CodeService</servlet-name>
<url-pattern>/code</url-pattern>
</servlet-mapping>
注意:当二维码出现显示不全的情况,
BufferedImage bufImg = new BufferedImage(140, 140, BufferedImage.TYPE_INT_RGB);
修改BufferedImage的两个140,140参数,通常和gs.clearRect(0, 0, 140, 140);一致就ok。
笔者能力有限,不足之处欢迎指出!