package com.northglass.util;
|
|
import java.awt.image.BufferedImage;
|
|
import javax.imageio.ImageIO;
|
|
import org.apache.commons.io.output.ByteArrayOutputStream;
|
import org.jbarcode.JBarcode;
|
import org.jbarcode.encode.Code128Encoder;
|
import org.jbarcode.encode.EAN13Encoder;
|
import org.jbarcode.paint.BaseLineTextPainter;
|
import org.jbarcode.paint.EAN13TextPainter;
|
import org.jbarcode.paint.WidthCodedPainter;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import sun.misc.BASE64Encoder;
|
|
@SuppressWarnings("restriction")
|
public class JbarcodeUtil {
|
private static final Logger LOGGER = LoggerFactory.getLogger(JbarcodeUtil.class);
|
|
/**
|
* 商品条形码
|
*
|
* @param strBarCode
|
* 商品条形码:13位
|
* @param dimension
|
* 商品条形码:尺寸 ,必须设置成2,否则打出的一维码扫描不了
|
* @param barheight
|
* 商品条形码:高度
|
* @return 图片(Base64编码)
|
*/
|
public BufferedImage getimage(String str, String dimension, String barheight) {
|
LOGGER.debug("JbarcodeUtil:" + str);
|
|
try {
|
int length = str.length();
|
JBarcode localJBarcode = new JBarcode(EAN13Encoder.getInstance(), WidthCodedPainter.getInstance(),
|
EAN13TextPainter.getInstance());
|
|
String barCode = str.substring(0, length - 1);
|
LOGGER.debug("str:" + barCode);
|
// 尺寸,面积,大小
|
localJBarcode.setXDimension(Double.valueOf(dimension).doubleValue());
|
// 高度 10.0 = 1cm 默认1.5cm
|
localJBarcode.setBarHeight(Double.valueOf(barheight).doubleValue());
|
// 宽度
|
localJBarcode.setWideRatio(Double.valueOf(25).doubleValue());
|
// localJBarcode.setShowText(true);// 显示图片下字符串内容
|
// localJBarcode.setShowCheckDigit(true);// 显示字符串内容中是否显示检查码内容
|
localJBarcode.setCheckDigit(false);// 不生成检查码
|
// 校验13位
|
BufferedImage localBufferedImage = localJBarcode.createBarcode(barCode);
|
return localBufferedImage;
|
} catch (Exception localException) {
|
localException.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
*128条形码
|
* @param strBarCode
|
*条形码:0-100位
|
* @param dimension
|
* 商品条形码:尺寸
|
* @param barheight
|
* 商品条形码:高度
|
* @return 图片(Base64编码)
|
*/
|
public BufferedImage 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()));
|
BufferedImage localBufferedImage = productBarcode.createBarcode(strBarCode);
|
return localBufferedImage;
|
} catch (Exception e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
}
|