在页面上通过“打印”按钮,打印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
- package wlrc.com;
- 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
- /**
- * 获取BufferedImage
- * @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 format:图片格式
- * @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);
- }
- }
- }
调用二维码生成的servlet
- package wlrc.com;
- 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
- * 二维码生成Servlet
- */
- 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);
- }
- }
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="3.0"
- 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_3_0.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>wlrc.com.CodeServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>CodeServlet</servlet-name>
- <url-pattern>/CodeServlet.do</url-pattern>
- </servlet-mapping>
- </web-app>
3,jsp前台页面调用
(1),加载javascript
- <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> <pre class="javascript" name="code"><script type="text/javascript">
- function load(){
- $("#bcTarget_tiaoxingma").barcode({code: "1234567", crc:false}, "int25",{barWidth:2, barHeight:20});
- }
- //bcTarget5中ie8一下render用table ie9以上用canvas 两个不同的渲染
- var LODOP; //声明为全局变量
- function printme_1(){
- LODOP=getLodop(document.getElementById('LODOP_OB'),document.getElementById('LODOP_EM'));
- var strFormHtml="<body style='margin-top: 15px;margin-left: 15px;'>"+document.getElementById ('biaoqian').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.print();
(2)创建打印的div
- <div id="biaoqian" style="width: 200px;height: 80px;margin-top: 0px; ">
- <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>
- <div id="bcTarget_tiaoxingma" style="width:150px;height:40px;display: inline;margin : 0px 0px 0px 5px;"></div>
- </div>
(3)打印div
- <input type="button" value="dayin" οnclick="printme_1();" />