在页面上通过“打印”按钮,打印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
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=""+document.getElementById ('biaoqian').innerHTML+""
strFormHtml.replace('