pom 文件如果不是使maven的可以自己下载对应版本的jar
com.google.zxing
core
3.0.0
com.google.zxing
javase
3.0.0
第二部 编写工具类
package com.dianping.emidas.activity.biz.utils;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.oned.Code128Writer;
public class ZxingUtil{
/**
* 条形码编码
*
* @param contents
* @param width
* @param height
* @param imgPath
*/
public static void encode(String contents, int width, int height, String imgPath) {
int codeWidth = 3 + // start guard
(7 * 6) + // left bars
5 + // middle guard
(7 * 6) + // right bars
3; // end guard
codeWidth = Math.max(codeWidth, width);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
BarcodeFormat.EAN_13, codeWidth, height, null);
MatrixToImageWriter.writeToStream(bitMatrix, "png",
new FileOutputStream(imgPath));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解析条形码
*
* @param imgPath
* @return
*/
public static String decode(String imgPath) {
BufferedImage image = null;
Result result = null;
try {
image = ImageIO.read(new File(imgPath));
if (image == null) {
System.out.println("the decode image may be not exit.");
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
result = new MultiFormatReader().decode(bitmap, null);
return result.getText();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String imgPath = "c://zxing_EAN13.png";
// 益达无糖口香糖的条形码
// String contents = "6923450657713";
String contents="6901236341292";
int width = 105, height = 50;
ZxingUtil handler = new ZxingUtil();
handler.encode(contents, width, height, imgPath);
System.out.println("Michael ,you have finished zxing EAN13 encode.");
}
private static final String KEY = "keycode";
private static final String WIDTH = "mwidth";
private static final String HEIGHT = "mheight";
private static final String IMAGETYPE = "png";
public static void BarCode(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String keycode = "6923450657713";
if (keycode != null && !"".equals(keycode)) {
ServletOutputStream stream = null;
try {
Code128Writer writer = new Code128Writer();
int width=180;
int height=60;
String mwidth = req.getParameter(WIDTH);
if (mwidth != null && !"".equals(mwidth.trim())) {
try{
width=Integer.valueOf(mwidth);
} catch (NumberFormatException e) {
//TODO output to log
}
}
String mheight = req.getParameter(HEIGHT);
if (mheight != null && !"".equals(mheight.trim())) {
try{
height = Integer.valueOf(mheight);
} catch (NumberFormatException e) {
//TODO output to log
}
}
int codeWidth = 3 + // start guard
(7 * 6) + // left bars
5 + // middle guard
(7 * 6) + // right bars
3; // end guard
codeWidth = Math.max(codeWidth, width);
stream = resp.getOutputStream();
BitMatrix bitMatrix = new MultiFormatWriter().encode(keycode,
BarcodeFormat.EAN_13, codeWidth, height, null);
MatrixToImageWriter.writeToStream(bitMatrix, IMAGETYPE,
stream);
} catch (WriterException e) {
e.printStackTrace();
} finally {
if (stream != null) {
stream.flush();
stream.close();
}
}
}
}
}
编写Action
public void barcode(){
try {
ZxingUtil.BarCode(request, response);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
struts2 文件
jsp 文件
pageEncoding="UTF-8"%>
Insert title here