前言:
JBarcode入门教程我就不写了,可以参考:点击打开链接
我的这篇教程和上篇博客的不同之处:
1 上篇博客直接生成二维码图片放到d盘的某个文件夹下,我的二维码生成二维码后直接用Base64编码然后返回到前台页面。
2 上篇博客只介绍了生成商品条形码,其他二维如Code93码、ISBN码、ISSN码、Code128码等等都没有介绍。我的博客里会介绍这些条码怎么生成。我文末会介绍常用条码及其如何选择适合自己项目的条码。
博客正文:
1 什么是JBarcode工具,这个工具支持哪些条码
2 如何用 JBarcode生成条形码,我这里提供生成商品条形码和128条形码
- package barcode;
- import java.awt.image.BufferedImage;
- import java.io.ByteArrayOutputStream;
- import javax.imageio.ImageIO;
- import org.apache.commons.lang.StringUtils;
- import org.jbarcode.JBarcode;
- import org.jbarcode.encode.Code128Encoder;
- import org.jbarcode.encode.EAN13Encoder;
- import org.jbarcode.encode.InvalidAtributeException;
- import org.jbarcode.paint.BaseLineTextPainter;
- import org.jbarcode.paint.EAN13TextPainter;
- import org.jbarcode.paint.WidthCodedPainter;
- import sun.misc.BASE64Encoder;
- public class BarcodeUtil {
- /**
- * 128条形码
- *
- * @param strBarCode
- * 条形码:0-100位
- * @param dimension
- * 商品条形码:尺寸
- * @param barheight
- * 商品条形码:高度
- * @return 图片(Base64编码)
- */
- public static String generateBarCode128(String strBarCode,String dimension,String barheight) {
- try {
- ByteArrayOutputStream outputStream = null;
- BufferedImage bi = null;
- int len = strBarCode.length();
- JBarcode productBarcode = new JBarcode(Code128Encoder.getInstance(),
- WidthCodedPainter.getInstance(),
- EAN13TextPainter.getInstance());
- // 尺寸,面积,大小 密集程度
- productBarcode.setXDimension(Double.valueOf(dimension).doubleValue());
- // 高度 10.0 = 1cm 默认1.5cm
- productBarcode.setBarHeight(Double.valueOf(barheight).doubleValue());
- // 宽度
- productBarcode.setWideRatio(Double.valueOf(30).doubleValue());
- // 是否显示字体
- productBarcode.setShowText(true);
- // 显示字体样式
- productBarcode.setTextPainter(BaseLineTextPainter.getInstance());
- // 生成二维码
- bi = productBarcode.createBarcode(strBarCode);
- outputStream = new ByteArrayOutputStream();
- ImageIO.write(bi, “jpg”, outputStream);
- BASE64Encoder encoder = new BASE64Encoder();
- // System.err.println(encoder.encode(outputStream.toByteArray()));
- return encoder.encode(outputStream.toByteArray());
- } catch (Exception e) {
- e.printStackTrace();
- return “encodeError”;
- }
- }
- /**
- * 商品条形码
- * @param strBarCode
- * 商品条形码:13位
- * @param dimension
- * 商品条形码:尺寸
- * @param barheight
- * 商品条形码:高度
- * @return 图片(Base64编码)
- */
- public static String generateBarCode(String strBarCode,String dimension,String barheight) {
- // isNumeric 是否是数值
- // 校验。。。。。
- try {
- ByteArrayOutputStream outputStream = null;
- BufferedImage bi = null;
- int len = strBarCode.length();
- JBarcode productBarcode = new JBarcode(EAN13Encoder.getInstance(),
- WidthCodedPainter.getInstance(),
- EAN13TextPainter.getInstance());
- String barCode = strBarCode.substring(0, len - 1);
- String code = strBarCode.substring(len - 1, len);
- //校验13位
- String checkCode = productBarcode.calcCheckSum(barCode);
- if (!code.equals(checkCode)) {
- return “checkCodeError”;
- }
- // 尺寸,面积,大小
- productBarcode.setXDimension(Double.valueOf(dimension).doubleValue());
- // 高度 10.0 = 1cm 默认1.5cm
- productBarcode.setBarHeight(Double.valueOf(barheight).doubleValue());
- // 宽度
- productBarcode.setWideRatio(Double.valueOf(25).doubleValue());
- // 是否校验13位,默认false
- productBarcode.setShowCheckDigit(true);
- //显示字符串内容中是否显示检查码内容
- // productBarcode.setShowCheckDigit(true);
- // 生成二维码
- bi = productBarcode.createBarcode(barCode);
- outputStream = new ByteArrayOutputStream();
- ImageIO.write(bi, “jpg”, outputStream);
- BASE64Encoder encoder = new BASE64Encoder();
- // System.err.println(encoder.encode(outputStream.toByteArray()));
- return encoder.encode(outputStream.toByteArray());
- } catch (Exception e) {
- e.printStackTrace();
- return “encodeError”;
- }
- }
- /**
- * @param args
- * @throws InvalidAtributeException
- */
- public static void main(String[] args) throws InvalidAtributeException {
- String encode = BarcodeUtil.generateBarCode(“6936983800013”,”0.5”,”30”);
- String encode2 = BarcodeUtil.generateBarCode128(“69369833450938430579753045230800013”,”0.5”,”30”);
- System.out.println(encode);
- }
- }
package barcode;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import javax.imageio.ImageIO;
import org.apache.commons.lang.StringUtils;
import org.jbarcode.JBarcode;
import org.jbarcode.encode.Code128Encoder;
import org.jbarcode.encode.EAN13Encoder;
import org.jbarcode.encode.InvalidAtributeException;
import org.jbarcode.paint.BaseLineTextPainter;
import org.jbarcode.paint.EAN13TextPainter;
import org.jbarcode.paint.WidthCodedPainter;
import sun.misc.BASE64Encoder;
public class BarcodeUtil {
/**
* 128条形码
*
* @param strBarCode
* 条形码:0-100位
* @param dimension
* 商品条形码:尺寸
* @param barheight
* 商品条形码:高度
* @return 图片(Base64编码)
*/
public static String generateBarCode128(String strBarCode,String dimension,String barheight) {
try {
ByteArrayOutputStream outputStream = null;
BufferedImage bi = null;
int len = strBarCode.length();
JBarcode productBarcode = new JBarcode(Code128Encoder.getInstance(),
WidthCodedPainter.getInstance(),
EAN13TextPainter.getInstance());
// 尺寸,面积,大小 密集程度
productBarcode.setXDimension(Double.valueOf(dimension).doubleValue());
// 高度 10.0 = 1cm 默认1.5cm
productBarcode.setBarHeight(Double.valueOf(barheight).doubleValue());
// 宽度
productBarcode.setWideRatio(Double.valueOf(30).doubleValue());
// 是否显示字体
productBarcode.setShowText(true);
// 显示字体样式
productBarcode.setTextPainter(BaseLineTextPainter.getInstance());
// 生成二维码
bi = productBarcode.createBarcode(strBarCode);
outputStream = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", outputStream);
BASE64Encoder encoder = new BASE64Encoder();
// System.err.println(encoder.encode(outputStream.toByteArray()));
return encoder.encode(outputStream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
return "encodeError";
}
}
/**
* 商品条形码
* @param strBarCode
* 商品条形码:13位
* 13位条形码是有一定规则的。13位商品条码数的含义
* 1、商品条码前3位是“国别码”,“690-695”开头的,该商品一般产自中国。
* 2、厂商代码“第4-8位”,由国际物品编码协会统一分配。
* 3、条码“9-12位”数,代表厂内商品代码,由厂商自行确定。
* 4、条码最后一位数为校验码,由前面的数字通过特殊公式计算得出。
* 5、商品条码我们常见上面标注有“EAN”,这个EAN码是国际物品编码协会制定第一种商品用条码,通用语全世界。有13位数的标准版(EAN-13)和8位数的(EAN-8)两种版本。
* @param dimension
* 商品条形码:尺寸
* @param barheight
* 商品条形码:高度
* @return 图片(Base64编码)
*/
public static String generateBarCode(String strBarCode,String dimension,String barheight) {
// isNumeric 是否是数值
// 校验。。。。。
try {
ByteArrayOutputStream outputStream = null;
BufferedImage bi = null;
int len = strBarCode.length();
JBarcode productBarcode = new JBarcode(EAN13Encoder.getInstance(),
WidthCodedPainter.getInstance(),
EAN13TextPainter.getInstance());
String barCode = strBarCode.substring(0, len - 1);
String code = strBarCode.substring(len - 1, len);
//校验13位
String checkCode = productBarcode.calcCheckSum(barCode);
if (!code.equals(checkCode)) {
return "checkCodeError";
}
// 尺寸,面积,大小
productBarcode.setXDimension(Double.valueOf(dimension).doubleValue());
// 高度 10.0 = 1cm 默认1.5cm
productBarcode.setBarHeight(Double.valueOf(barheight).doubleValue());
// 宽度
productBarcode.setWideRatio(Double.valueOf(25).doubleValue());
// 是否校验13位,默认false
productBarcode.setShowCheckDigit(true);
//显示字符串内容中是否显示检查码内容
// productBarcode.setShowCheckDigit(true);
// 生成二维码
bi = productBarcode.createBarcode(barCode);
outputStream = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", outputStream);
BASE64Encoder encoder = new BASE64Encoder();
// System.err.println(encoder.encode(outputStream.toByteArray()));
return encoder.encode(outputStream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
return "encodeError";
}
}
/**
* @param args
* @throws InvalidAtributeException
*/
public static void main(String[] args) throws InvalidAtributeException {
String encode = BarcodeUtil.generateBarCode("6936983800013","0.5","30");
String encode2 = BarcodeUtil.generateBarCode128("69369833450938430579753045230800013","0.5","30");
System.out.println(encode);
}
}
有的读者想生成Code 11, Code 93类型的条码怎么生成呢?很简单
答案就在创建Jbarcode对象代码里,如果你想生成128条码
- JBarcode productBarcode = new JBarcode(Code128Encoder.getInstance(),
- WidthCodedPainter.getInstance(),
- EAN13TextPainter.getInstance());
JBarcode productBarcode = new JBarcode(Code128Encoder.getInstance(),
WidthCodedPainter.getInstance(),
EAN13TextPainter.getInstance());
如果你想生成EAN13Ender条码
- JBarcode productBarcode = new JBarcode(EAN13Encoder.getInstance(),
- WidthCodedPainter.getInstance(),
- EAN13TextPainter.getInstance());
JBarcode productBarcode = new JBarcode(EAN13Encoder.getInstance(),
WidthCodedPainter.getInstance(),
EAN13TextPainter.getInstance());
看到红色字体里了吧,想生成什么条码直接xxx.getInstance即可,简单吧
3 页面显示
生成图片后,用Base64编码后得到字符串,假如为:”123xyz”
页面jsp里<img src=”data:image/png;base64,123xyz”/>即可
注意-注意条形码的字体样式!!!
我当时就在这里栽了,不同的字体样式显示效果不一样,甚至可以用天上地下来形容他们的差距。最后没办法,只有看
祝你成功!
转载:http://blog.csdn.net/wabiaozia/article/details/52747772