二維碼生成需要借助第三方j(luò)ar包浮禾。我這里使用的是QRcode。給大家一個(gè)連接尺借,自行下載: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);
//設(shè)定圖像顏色:BLACK
gs.setColor(Color.BLACK);
//設(shè)置偏移量 不設(shè)置肯能導(dǎo)致解析出錯(cuò)
int pixoff = 2;
//輸出內(nèi)容:二維碼
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();
}
}
第一個(gè)參數(shù)是鏈接的內(nèi)容绊起,比如:https://www.baidu.com
第二個(gè)是HttpServletResponse,為什么要傳這個(gè)參數(shù)呢燎斩,我是讓二維碼直接顯示到j(luò)sp中虱歪,在jsp頁(yè)面,定義:
<i mg src="code">
code是我servlet,讓src指向這個(gè)servlet瘫里。好吧实蔽,可能我表達(dá)的不夠清除,我把完整代碼貼一下:
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);
//設(shè)定圖像顏色:BLACK
gs.setColor(Color.BLACK);
//設(shè)置偏移量 不設(shè)置肯能導(dǎo)致解析出錯(cuò)
int pixoff = 2;
//輸出內(nèi)容:二維碼
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>
注意:當(dāng)二維碼出現(xiàn)顯示不全的情況谨读,
BufferedImage bufImg = new BufferedImage(140, 140, BufferedImage.TYPE_INT_RGB);
修改BufferedImage的兩個(gè)140,140參數(shù),通常和gs.clearRect(0, 0, 140, 140);一致就ok坛吁。
筆者能力有限劳殖,不足之處歡迎指出!