【项目需求】
在公司项目的备品备件模块中,有一个需求就是在入库的时候,根据二维码扫描入库,即每一种物品都有一个唯一的二维码,入库时不用手动输入商品信息,类似于超市的扫描条形码收银。
【需求分析】
要实现此功能要实现两个小功能:
一、给每个物品生成二维码(根据物品编号,仓库编号以及物品类型来生成)。
二、打印二维码
【功能实现】
一、下载生成二维码的jar包
a) Qrcode.jar
b) Qrcode_swetake.jar
二、编写生成二维码助手类:QRCodeEncoderHandler.java
package qr;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
/**
* 二维码生成器
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class QRCodeEncoderHandler {
/**
* 生成二维码(QRCode)图片
* @param content
* @param imgPath
*/
public void encoderQRCode(String content, OutputStream out) {
try {
int[] in={7,139,139};
//int size=120;
int size=120;
int qrversion=in[0];
int width= in[1];
int height= in[2];
Qrcode qrcodeHandler = new Qrcode();
//qrcodeHandler.setQrcodeErrorCorrect('H');
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
qrcodeHandler.setQrcodeVersion(qrversion);
// System.out.println(content);
//byte[] contentBytes = content.getBytes("gb2312");
byte[] contentBytes = content.getBytes("UTF-8");
BufferedImage bufImg = new BufferedImage(width,height,
BufferedImage.TYPE_INT_RGB);
Graphics2D gs =bufImg.createGraphics();
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, width,height);
// 设定图像颜色 > BLACK
gs.setColor(Color.BLACK);
// 设置偏移量不设置可能导致解析出错
int pixoff = 2;
// 输出内容 > 二维码
if (contentBytes.length > 0 && contentBytes.length <= size) {
boolean[][] codeOut =qrcodeHandler.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();
ImageIO.write(bufImg, "png", out);
} catch (Exception e) {
e.printStackTrace();
}
}
public void encoderQRCode(String content,String imgPath){
File imgFile = new File(imgPath);
OutputStream out=null;
try {
out= new FileOutputStream(imgFile);
this.encoderQRCode(content,out);
}catch(FileNotFoundException e) {
e.printStackTrace();
}finally{
if(out!=null){
try {
out.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
三、编写生成二维码的servlet类:QrServlet.java
package ac;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import qr.QRCodeEncoderHandler;
/**
*
* 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
* http://localhost:8080/tpk/qr?k=tilechina
*
* @author admin
*
*/
@WebServlet(name="qr",urlPatterns={"/qr"})
public class QrServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException {
String content=req.getParameter("k");
if(content==null||"".equals(content.trim())){
return;
}
QRCodeEncoderHandler qrCode = newQRCodeEncoderHandler();
resp.setHeader("Content-Type","image/png");
OutputStreamout=resp.getOutputStream();
qrCode.encoderQRCode(content,out);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException {
//super.doPost(req, resp);
this.doGet(req, resp);
}
}
四、编写显示二维码页面:QRCode.jsp
<%@page import="org.easyelf.util.ReqUtil"%>
<%@ page language="java"contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
Stringname=ReqUtil.escapeVal(request, "name");
Stringcode=ReqUtil.escapeVal(request, "code");
StringstoreCode=ReqUtil.escapeVal(request, "storeCode");
Stringtype=ReqUtil.escapeVal(request, "type");
Stringprint=ReqUtil.escapeVal(request, "print");
%>
<html>
<head>
<jsp:include page="/page/portal/include.jsp"/>
<script type="text/javascript" src="<%=path %>/plugins/jquery/jquery.jqprint-0.3.js">
</script>
<script type="text/javascript">
var name = "<%=name %>";
var code = "<%=code %>";
var storeCode= "<%=storeCode%>";
var type = "<%=type%>";
var print = "<%=print%>";
$(document).ready(function(){
var str="CODE="+code+";STORECODE="+storeCode+";TYPE="+type;
document.getElementById('code').innerHTML=str;
code=encodeURIComponent(str);
varobj_tutu=document.getElementById("tutu");
document.getElementById("tutu");
obj_tutu.src="<%=path%>/qr?k="+code;
if (print=="true"){
$("#QRCode").jqprint();
}
});
</script>
</head>
<body>
<div id ="QRCode">
<table style="margin:auto;">
<tr>
<th>
<span id="code"/>
</th>
</tr>
<tr>
<td style="text-align:center;"><img id="tutu"src="" /></td>
</tr>
</table>
</div>
</body>
</html>
注:打印功能调用的Jquery的打印插件:jquery.jqprint-0.3.js,直接调用jqprint()方法,就可以调用浏览器的打印功能。是不是很简单啊?
现在,启动tomcat,运行系统,查看相应页面,我们的二维码就生成了。
二维码生成原理:http://blog.csdn.net/songylwq/article/details/8643948
【总结】
刚开始说做二维码还觉得挺难的,其实挺简单的,前台页面传递一个已经进行编码的字符串参数,后台servlet通过解析这个字符串,根据什么图像,像素,大小什么的,就生成了我们想要的二维码。实现起来挺简单的,通过查看QR的原理,感觉又有很大收获!