日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

条形码和二维码 生成以及打印

發(fā)布時(shí)間:2024/3/26 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 条形码和二维码 生成以及打印 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、生成條形碼

jsp頁(yè)面引入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> 定義一個(gè)div:

<span style="white-space:pre"> </span><div id="bcTarget_tiaoxingma"></div> 編寫(xiě)js腳本:

<span style="white-space:pre"> </span>$("#bcTarget_tiaoxingma").barcode({code: "1234567", crc:false}, "int25",{barWidth:2, barHeight:20}); 調(diào)用js方法就可以了。

注意:


二、生成二維碼:(兩種方式)

方式一:用jquery中的qrcode插件來(lái)生成,此方法可以在頁(yè)面上顯示(html5),實(shí)現(xiàn)原理是div的qrcode事件,再打印的時(shí)候該時(shí)間如法傳入,導(dǎo)致打印預(yù)覽時(shí)無(wú)法顯示,且用特定打印機(jī)打印時(shí),無(wú)法正常打印。

引入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> 編寫(xiě)js腳本:

<span style="white-space:pre"> </span>$("#bcTarget5").qrcode({render: "canvas",width: 50,height:50, text: "www.helloweba.com",background : "#E3E0D5",foreground : "#FF3690"}); 觸發(fā)腳本即可。


方式二:利用jar插件來(lái)通過(guò)servlet來(lái)實(shí)現(xiàn),

引入jar包:zxing-core-2.0.jar,

新建一個(gè)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);}}


上述.java代碼中引用的工具類:EncoderHandler.java

package sec.ac.cn;import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import javax.imageio.ImageIO; import com.google.zxing.common.BitMatrix;public class EncoderHandler { // Fieldsprivate static final int BLACK = 0xFF000000;private static final int WHITE = 0xFFFFFFFF;// Empty constructorprivate EncoderHandler() {}// Methods/*** @param matrix:BitMatrix* @return BufferedImage*/public static BufferedImage toBufferedImage(BitMatrix matrix) {int width = matrix.getWidth();int height = matrix.getHeight();BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);for (int x = 0; x < width; x++) {for (int y = 0; y < height; y++) {image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);}}return image;}/*** @param matrix:BitMatrix* @param stream:OutputStream* @throws IOException*/public static void writeToStream(BitMatrix matrix, String format,OutputStream stream) throws IOException {BufferedImage image = toBufferedImage(matrix);if (!ImageIO.write(image, format, stream)) {throw new IOException("Could not write an image of format "+ format);}} } 配置web.xml

<?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頁(yè)面定義div:

<div id="bcTarget_erweima" ><img style="height:65px;width:65px" src="${pageContext.request.contextPath}/codes.do?qrcode=zhangsan" /> </div> 發(fā)布工程,訪問(wèn)頁(yè)面即可。


三、打印:條形碼或者二維碼在普通打印機(jī)上打印正常,但是想在專用的條形碼或者二維碼打印機(jī)上打印可沒(méi)有想象的那么簡(jiǎn)單。首先,專用的條形碼打印機(jī)的紙張大小規(guī)格不統(tǒng)一,如果想普通打印機(jī)一樣去執(zhí)行打印操作,專用打印機(jī)會(huì)將整個(gè)jsp頁(yè)面打印出來(lái),且不是打印在一個(gè)紙張上(放不下),浪費(fèi)好多專用紙張,且不合格,解決方案:利用Lodop打印插件,通過(guò)打印某個(gè)div的方式來(lái)打印。

引入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下載下來(lái)放到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();"; /> 編寫(xiě)腳本: 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(); } 預(yù)覽打印即可。lodop有直接打印方法:LODOP.PRINT();






總結(jié)

以上是生活随笔為你收集整理的条形码和二维码 生成以及打印的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。