web项目中使用标签打印机打印条形码及二维码

在页面上通过“打印”按钮,打印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 hints = new Hashtable();  
        // 内容所使用编码  
        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  

                  



  
    This is the description of my J2EE component
    This is the display name of my J2EE component
    CodeServlet
    wlrc.com.CodeServlet
  

  
    CodeServlet
    /CodeServlet.do
  

                     3,jsp前台页面调用

                   (1),加载javascript

                            

 
	
 
 
  
 
  

                   (2)创建打印的div

                  (3)打印div

   

web项目中使用标签打印机打印条形码及二维码_第1张图片

                  


 

 

 


 

你可能感兴趣的:(2014年6月)