ZXing生成条码二维码并输出到web页面的示例

来源:互联网 发布:穿越火线手游淘宝商城 编辑:程序博客网 时间:2024/04/30 15:28

1、生成条码的servlet代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
importjava.io.IOException;
 
importjavax.servlet.ServletException;
importjavax.servlet.ServletOutputStream;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
 
importcom.google.zxing.BarcodeFormat;
importcom.google.zxing.WriterException;
importcom.google.zxing.client.j2se.MatrixToImageWriter;
importcom.google.zxing.common.BitMatrix;
importcom.google.zxing.oned.Code128Writer;
 
/**
 * @Description: 生成条码(CODE128格式)  */
publicclass BarCode1DServlet extendsHttpServlet {
 
    /**  
     * @Fields serialVersionUID : default serialVersionUID
     */
    privatestatic final long serialVersionUID = 1L;
     
    privatestatic final String KEY = "keycode";
    privatestatic final String WIDTH = "mwidth";
    privatestatic final String HEIGHT = "mheight";
    privatestatic final String IMAGETYPE = "JPEG";
 
    @Override
    protectedvoid doGet(HttpServletRequest req, HttpServletResponse resp)
            throwsServletException, IOException {
        String keycode = req.getParameter(KEY);
        if(keycode != null&& !"".equals(keycode)) {
            ServletOutputStream stream = null;
            try{
                Code128Writer writer = newCode128Writer();
                intwidth=180;
                intheight=60;
                String mwidth = req.getParameter(WIDTH);
                if(mwidth != null&& !"".equals(mwidth.trim())) {
                    try{
                        width=Integer.valueOf(mwidth);
                    catch(NumberFormatException e) {
                                        //TODO output to log
                    }
                }
                String mheight = req.getParameter(HEIGHT);
                if(mheight != null&& !"".equals(mheight.trim())) {
                    try{
                        height = Integer.valueOf(mheight);
                    catch(NumberFormatException e) {
                        //TODO output to log
                    }
                }
                stream = resp.getOutputStream();
                BitMatrix m = writer.encode(keycode, BarcodeFormat.CODE_128, width, height);
                MatrixToImageWriter.writeToStream(m, IMAGETYPE, stream);
            catch(WriterException e) {
                e.printStackTrace();
            finally{
                if(stream != null) {
                    stream.flush();
                    stream.close();
                }
            }
        }
    }
 
    @Override
    protectedvoid doPost(HttpServletRequest req, HttpServletResponse resp)
            throwsServletException, IOException {
        this.doGet(req, resp);
    }
     
     
 
}

2、生成qrcode二维码的servlet示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
importjava.io.IOException;
 
importjavax.servlet.ServletException;
importjavax.servlet.ServletOutputStream;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
 
importcom.google.zxing.BarcodeFormat;
importcom.google.zxing.WriterException;
importcom.google.zxing.client.j2se.MatrixToImageWriter;
importcom.google.zxing.common.BitMatrix;
importcom.google.zxing.qrcode.QRCodeWriter;
 
/**
 * @Description: 生成二维码 (QR格式)
 * @author lwei
 */
publicclass BarCode2DServlet extendsHttpServlet {
 
    /**  
     * @Fields serialVersionUID : serialVersionUID
     */
     
    privatestatic final long serialVersionUID = 1L;
     
    privatestatic final String KEY = "keycode";
    privatestatic final String SIZE = "msize";
    privatestatic final String IMAGETYPE = "JPEG";
 
    @Override
    protectedvoid doGet(HttpServletRequest req, HttpServletResponse resp)
            throwsServletException, IOException {
        String keycode = req.getParameter(KEY);
         
        if(keycode != null&& !"".equals(keycode)) {
            ServletOutputStream stream = null;
            try{
                intsize=129;
                String msize = req.getParameter(SIZE);
                if(msize != null&& !"".equals(msize.trim())) {
                    try{
                        size=Integer.valueOf(msize);
                    catch(NumberFormatException e) {
                        //TODO output to log
                    }
                }
                stream = resp.getOutputStream();
                QRCodeWriter writer = newQRCodeWriter();
                BitMatrix m = writer.encode(keycode, BarcodeFormat.QR_CODE, size, size);
                MatrixToImageWriter.writeToStream(m, IMAGETYPE, stream);
            catch(WriterException e) {
                e.printStackTrace();
            finally{
                if(stream != null) {
                    stream.flush();
                    stream.close();
                }
            }
        }
    }
 
    @Override
    protectedvoid doPost(HttpServletRequest req, HttpServletResponse resp)
            throwsServletException, IOException {
        this.doGet(req, resp);
    }
     
}

3、web.xml中的配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        <servlet>
        <servlet-name>barCode</servlet-name>
        <servlet-class>com.XXX.XXX.common.servlet.BarCode1DServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>barCode</servlet-name>
        <url-pattern>/barCode</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>qrCode</servlet-name>
        <servlet-class>com.XXX.XXX.common.servlet.BarCode2DServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>qrCode</servlet-name>
        <url-pattern>/qrCode</url-pattern>
    </servlet-mapping>

4、页面引用


?
1
2
<imgalt="条码"src="${pageContext.request.contextPath}/barCode?keycode=RT100200300400"></img>
    <imgalt="二维码"src="${pageContext.request.contextPath}/qrCode?keycode=RT100200300400500"></img>

0 0