在页面上通过“打印”按钮,打印div内容,实现标签的打印(含有条形码和二维码)。

在页面上通过“打印”按钮,打印div内容,实现标签的打印(含有条形码和二维码)。

操作步骤:1,加载js,jquery-1.3.2.min.js     引用jquery。

                                                        jquery-barcode-2.0.1.js             引用条形码

                                           jquery.qrcode.min.js                 引用二维码

                                          LodopFuncs.js                            web打印控件 

                               2,获取二维码后台实现:

                          EncodeHandle。java                 

[java]  view plain  copy
  1. package wlrc.com;  
  2.   
  3.   
  4. import java.awt.image.BufferedImage;  
  5. import java.io.IOException;  
  6. import java.io.OutputStream;  
  7. import javax.imageio.ImageIO;  
  8. import com.google.zxing.common.BitMatrix;  
  9.   
  10.     
  11.     
  12.     
  13. public class EncoderHandler {    
  14.         
  15.     // Fields  
  16.         private static final int BLACK = 0xFF000000;  
  17.         private static final int WHITE = 0xFFFFFFFF;  
  18.   
  19.         // Empty constructor  
  20.         private EncoderHandler() {  
  21.   
  22.         }  
  23.           
  24.         // Methods  
  25.           
  26.         /** 
  27.          * 获取BufferedImage 
  28.          * @param matrix:BitMatrix 
  29.          * @return BufferedImage 
  30.          */  
  31.         public static BufferedImage toBufferedImage(BitMatrix matrix) {  
  32.             int width = matrix.getWidth();  
  33.             int height = matrix.getHeight();  
  34.             BufferedImage image = new BufferedImage(width, height,  
  35.                     BufferedImage.TYPE_INT_RGB);  
  36.             for (int x = 0; x < width; x++) {  
  37.                 for (int y = 0; y < height; y++) {  
  38.                     image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);  
  39.                 }  
  40.             }  
  41.             return image;  
  42.         }  
  43.           
  44.       
  45.           
  46.   
  47.         /** 
  48.          * 将二维码输出到流 
  49.          * @param matrix:BitMatrix 
  50.          * @param format:图片格式 
  51.          * @param stream:OutputStream 
  52.          * @throws IOException 
  53.          */  
  54.         public static void writeToStream(BitMatrix matrix, String format,  
  55.                 OutputStream stream) throws IOException {  
  56.             BufferedImage image = toBufferedImage(matrix);  
  57.             if (!ImageIO.write(image, format, stream)) {  
  58.                 throw new IOException("Could not write an image of format "  
  59.                         + format);  
  60.             }  
  61.         }  
  62. }    

                                  调用二维码生成的servlet

[java]  view plain  copy
  1. package wlrc.com;  
  2.   
  3.   
  4. import java.io.IOException;    
  5. import java.util.Hashtable;  
  6.     
  7. import javax.servlet.ServletException;    
  8. import javax.servlet.ServletOutputStream;  
  9. import javax.servlet.http.HttpServlet;    
  10. import javax.servlet.http.HttpServletRequest;    
  11. import javax.servlet.http.HttpServletResponse;    
  12.   
  13. import com.google.zxing.BarcodeFormat;  
  14. import com.google.zxing.EncodeHintType;  
  15. import com.google.zxing.MultiFormatWriter;  
  16. import com.google.zxing.WriterException;  
  17. import com.google.zxing.common.BitMatrix;  
  18.   
  19. /** 
  20.  * @author uimagine 
  21.  * 二维码生成Servlet 
  22.  */  
  23. public class CodeServlet extends HttpServlet {  
  24.       
  25.     /** 
  26.      * Default SerialVersionUID 
  27.      */  
  28.     private static final long serialVersionUID = 1L;  
  29.   
  30.     /** 
  31.      * doGet 
  32.      */  
  33.     @Override  
  34.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  35.           
  36.         String code = request.getParameter("code");  
  37.         String qrcode = request.getParameter("qrcode");    
  38.         int width = 300;    
  39.         int height = 300;    
  40.         // 二维码的图片格式    
  41.         String format = "gif";    
  42.         Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();    
  43.         // 内容所使用编码    
  44.         hints.put(EncodeHintType.CHARACTER_SET, "utf-8");    
  45.         ServletOutputStream stream = response.getOutputStream();    
  46.         try {  
  47.               
  48.             BitMatrix bitMatrix = null;  
  49.             if(qrcode!=null){  
  50.                 bitMatrix = new MultiFormatWriter().encode(qrcode, BarcodeFormat.QR_CODE, width, height, hints);  
  51.             }  
  52.             if(code!=null){  
  53.                 width = 505;    
  54.                 height = 50;    
  55.                 bitMatrix = new MultiFormatWriter().encode(code, BarcodeFormat.CODE_128, width, height, null);  
  56.             }  
  57.             EncoderHandler.writeToStream(bitMatrix, format, stream);  
  58.         } catch (WriterException e) {  
  59.             e.printStackTrace();  
  60.         }    
  61.         stream.flush();    
  62.         stream.close();    
  63.         response.flushBuffer();     
  64.     }  
  65.       
  66.     /** 
  67.      * doPost 
  68.      */  
  69.     @Override  
  70.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  71.         this.doGet(request, response);  
  72.     }  
  73.   
  74. }  

                                  web.xml  

                  

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="3.0"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
  6.   <servlet>  
  7.     <description>This is the description of my J2EE component</description>  
  8.     <display-name>This is the display name of my J2EE component</display-name>  
  9.     <servlet-name>CodeServlet</servlet-name>  
  10.     <servlet-class>wlrc.com.CodeServlet</servlet-class>  
  11.   </servlet>  
  12.   
  13.   <servlet-mapping>  
  14.     <servlet-name>CodeServlet</servlet-name>  
  15.     <url-pattern>/CodeServlet.do</url-pattern>  
  16.   </servlet-mapping>  
  17.   
  18. </web-app>  

                     3,jsp前台页面调用

                   (1),加载javascript

                            

[javascript]  view plain  copy
  1. <object id="LODOP_OB" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width=0 height=0>   
  2.     <embed id="LODOP_EM" type="application/x-print-lodop" width=0 height=0    pluginspage="install_lodop.exe"></embed>  
  3. </object> <pre class="javascript" name="code"><script type="text/javascript">  
[javascript]  view plain  copy
  1.     function load(){  
  2.           $("#bcTarget_tiaoxingma").barcode({code: "1234567", crc:false}, "int25",{barWidth:2, barHeight:20});  
  3.     }  
  4.   
  5.     
  6. //bcTarget5中ie8一下render用table   ie9以上用canvas  两个不同的渲染     
  7. var LODOP; //声明为全局变量    
  8.        function printme_1(){  
  9.     LODOP=getLodop(document.getElementById('LODOP_OB'),document.getElementById('LODOP_EM'));    
  10.     var strFormHtml="<body style='margin-top: 15px;margin-left: 15px;'>"+document.getElementById       ('biaoqian').innerHTML+"</body>"  
  11.     strFormHtml.replace('<div','<span');  
  12.     strFormHtml.replace('</div','</span');  
  13.     LODOP.SET_PRINT_PAGESIZE(1,600,400,'');  
  14.     LODOP.ADD_PRINT_HTM(0,0,600,450,strFormHtml);  
  15.     LODOP.PREVIEW();  
[javascript]  view plain  copy
  1. LODOP.print();    
</script>
 
 

                   (2)创建打印的div

[html]  view plain  copy
  1. <div id="biaoqian" style="width: 200px;height: 80px;margin-top: 0px; ">  
  2.    <div id="bcTarget_erweima" style="margin-left: 0px;margin-top: 0px;width: 50px; display: inline; margin-bottom: 0px;"><img  style="height:65px;width:65px" src="${pageContext.request.contextPath}/CodeServlet.do?qrcode=zhangsan" />  </div>  
  3.    <div id="bcTarget_tiaoxingma" style="width:150px;height:40px;display: inline;margin : 0px 0px 0px 5px;"></div>  
  4. </div>  

                  (3)打印div

[javascript]  view plain  copy
  1. <input type="button" value="dayin" onclick="printme_1();" />  



                   

猜你喜欢

转载自blog.csdn.net/qq_30848071/article/details/79700372