条形码和二维码 生成以及打印
生活随笔
收集整理的這篇文章主要介紹了
条形码和二维码 生成以及打印
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、生成條形碼
jsp頁面引入js:
<META content="IE=9" http-equiv="X-UA-Compatible"><span style="white-space:pre"> </span> <script type="text/javascript" src="jquery/jquery-1.3.2.min.js"></script><span style="white-space:pre"> </span> <script type="text/javascript" src="jquery/jquery-barcode-2.0.1.js"></script><script type="text/javascript" src="jquery/jquery.qrcode.min.js"></script> 定義一個div:
<span style="white-space:pre"> </span><div id="bcTarget_tiaoxingma"></div> 編寫js腳本:
<span style="white-space:pre"> </span>$("#bcTarget_tiaoxingma").barcode({code: "1234567", crc:false}, "int25",{barWidth:2, barHeight:20}); 調用js方法就可以了。
注意:
二、生成二維碼:(兩種方式)
方式一:用jquery中的qrcode插件來生成,此方法可以在頁面上顯示(html5),實現原理是div的qrcode事件,再打印的時候該時間如法傳入,導致打印預覽時無法顯示,且用特定打印機打印時,無法正常打印。
引入js:
<span style="white-space:pre"> </span> <META content="IE=9" http-equiv="X-UA-Compatible"><span> </span> <script type="text/javascript" src="jquery/jquery-1.3.2.min.js"></script><span> </span> <script type="text/javascript" src="jquery/jquery-barcode-2.0.1.js"></script><script type="text/javascript" src="jquery/jquery.qrcode.min.js"></script> 定義div: <span style="white-space:pre"> </span><div id="bcTarget" ></div> 編寫js腳本: <span style="white-space:pre"> </span>$("#bcTarget5").qrcode({render: "canvas",width: 50,height:50, text: "www.helloweba.com",background : "#E3E0D5",foreground : "#FF3690"}); 觸發腳本即可。方式二:利用jar插件來通過servlet來實現,
引入jar包:zxing-core-2.0.jar,
新建一個servlet.java :
package sec.ac.cn;import java.io.IOException; import java.util.Hashtable;import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix;/*** @author uimagine* 浜岀淮鐮佺敓鎴怱ervlet*/ public class CodeServlet extends HttpServlet {/*** Default SerialVersionUID*/private static final long serialVersionUID = 1L;/*** doGet*/@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String code = request.getParameter("code");String qrcode = request.getParameter("qrcode"); int width = 300; int height = 300; // 浜岀淮鐮佺殑鍥劇墖鏍煎紡 String format = "gif"; Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); // 鍐呭鎵?嬌鐢ㄧ紪鐮? hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); ServletOutputStream stream = response.getOutputStream(); try {BitMatrix bitMatrix = null;if(qrcode!=null){bitMatrix = new MultiFormatWriter().encode(qrcode, BarcodeFormat.QR_CODE, width, height, hints);}if(code!=null){width = 505; height = 50; bitMatrix = new MultiFormatWriter().encode(code, BarcodeFormat.CODE_128, width, height, null);}EncoderHandler.writeToStream(bitMatrix, format, stream);} catch (WriterException e) {e.printStackTrace();} stream.flush(); stream.close(); response.flushBuffer(); }/*** doPost*/@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}}
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><servlet><description>This is the description of my J2EE component</description><display-name>This is the display name of my J2EE component</display-name><servlet-name>CodeServlet</servlet-name><servlet-class>sec.ac.cn.CodeServlet</servlet-class></servlet><servlet-mapping><servlet-name>CodeServlet</servlet-name><url-pattern>/codes.do</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list> </web-app>
三、打印:條形碼或者二維碼在普通打印機上打印正常,但是想在專用的條形碼或者二維碼打印機上打印可沒有想象的那么簡單。首先,專用的條形碼打印機的紙張大小規格不統一,如果想普通打印機一樣去執行打印操作,專用打印機會將整個jsp頁面打印出來,且不是打印在一個紙張上(放不下),浪費好多專用紙張,且不合格,解決方案:利用Lodop打印插件,通過打印某個div的方式來打印。
引入js:LodopFuncs.js
<script language="javascript" src="js/LodopFuncs.js"></script>
引入插件:
<object id="LODOP_OB" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width=0 height=0> <embed id="LODOP_EM" type="application/x-print-lodop" width=0 height=0 pluginspage="install_lodop.exe"></embed> </object> 將install_lodop.exe下載下來放到webroot路徑下。定義div
<div id="bcTarget_erweima" ><img style="height:65px;width:65px" src="${pageContext.request.contextPath}/codes.do?qrcode=zhangsan" /> </div> 定義 打印按鈕: <input type="button" value="dayin" οnclick="printme_1();"; /> 編寫腳本: var LODOP; //聲明為全局變量 function printme_1(){LODOP=getLodop(document.getElementById('LODOP_OB'),document.getElementById('LODOP_EM')); var strFormHtml="<body style='margin-top: 18px;margin-left: 20px;'>"+document.getElementById('<span style="font-family: Arial, Helvetica, sans-serif;">bcTarget_erweima</span>').innerHTML+"</body>"strFormHtml.replace('<div','<span');strFormHtml.replace('</div','</span');LODOP.SET_PRINT_PAGESIZE(1,600,400,'');LODOP.ADD_PRINT_HTM(0,0,600,450,strFormHtml);LODOP.PREVIEW(); } 預覽打印即可。lodop有直接打印方法:LODOP.PRINT();
總結
以上是生活随笔為你收集整理的条形码和二维码 生成以及打印的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 云盘秒变本地磁盘
- 下一篇: QQ浏览器HD iOS 动态化/热修复方