java zxing生成一维码_java Struts2 Zxing生成一维码

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

111

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值