您的位置:首页 > 其它

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

2014-06-26 10:09 337 查看
一、生成条形码

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
*/
@Override
protected 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
*/
@Override
protected 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 {

// Fields
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;

// Empty constructor
private 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页面定义div:
<div id="bcTarget_erweima" ><img style="height:65px;width:65px" src="${pageContext.request.contextPath}/codes.do?qrcode=zhangsan" /> </div> 发布工程,访问页面即可。

三、打
4000
印:条形码或者二维码在普通打印机上打印正常,但是想在专用的条形码或者二维码打印机上打印可没有想象的那么简单。首先,专用的条形码打印机的纸张大小规格不统一,如果想普通打印机一样去执行打印操作,专用打印机会将整个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" onclick="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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  二维码 条形码 打印