java生成条形码
这里直接贴代码,会在代码里面给详解
使用前要先引用 jbbarcode的jar包
<dependency>
<groupId>org.jbarcode</groupId>
<artifactId>JBarcode</artifactId>
<version>0.2.8</version>
</dependency>
代码示例
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());
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();
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) {
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);
String checkCode = productBarcode.calcCheckSum(barCode);
if (!code.equals(checkCode)) {
return "checkCodeError";
}
productBarcode.setXDimension(Double.valueOf(dimension).doubleValue());
productBarcode.setBarHeight(Double.valueOf(barheight).doubleValue());
productBarcode.setWideRatio(Double.valueOf(25).doubleValue());
productBarcode.setShowCheckDigit(true);
bi = productBarcode.createBarcode(barCode);
outputStream = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", outputStream);
BASE64Encoder encoder = new BASE64Encoder();
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);
System.out.println(encode2);
}
}
生成图片后,用Base64编码后得到字符串,假如为:"123xyz"
页面jsp里<img src="https://img-blog.csdnimg.cn/2022010612325048213.png"/>即可
以下方法是生成二维码图片并返回图片地址
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import org.jbarcode.JBarcode;
import org.jbarcode.encode.Code128Encoder;
import org.jbarcode.paint.EAN13TextPainter;
import org.jbarcode.paint.WidthCodedPainter;
import org.jbarcode.util.ImageUtil;
public class Util {
/**
* 生成商品条形码
*
* @param filePath
* 商品条形码图片存放路径:../xxx/yyy/
* @param jbarCode
* 商品条形码:8位、13位
* @param format
* 商品条形码图片格式:.gif/.png/.jpg/.jpeg
* @return 图片存放路径+图片名称+图片文件类型
*/
public String createBarCode(String filePath, String jbarCode, String format) {
String barCodeName = jbarCode + format;
try {
BufferedImage bi = null;
JBarcode productBarcode = new JBarcode(Code128Encoder.getInstance(),
WidthCodedPainter.getInstance(),
EAN13TextPainter.getInstance());
bi = productBarcode.createBarcode(jbarCode);
saveToJPG(bi, filePath, barCodeName);
productBarcode.setXDimension(Double.valueOf("0.5").doubleValue());
productBarcode.setBarHeight(Double.valueOf("30").doubleValue());
productBarcode.setWideRatio(Double.valueOf(30).doubleValue());
productBarcode.setShowText(true);
return filePath + barCodeName;
} catch (Exception localException) {
localException.printStackTrace();
return null;
}
}
/**
* 生成JPEG图片
*
* @param paramBufferedImage
* @param paramString
*/
@SuppressWarnings("unused")
private void saveToJPG(BufferedImage paramBufferedImage, String filePath,
String fileName) {
saveToFile(paramBufferedImage, filePath, fileName, "jpeg");
}
/**
* 生成PNG图片
*
* @param paramBufferedImage
* @param paramString
*/
@SuppressWarnings("unused")
private void saveToPNG(BufferedImage paramBufferedImage, String filePath,
String fileName) {
saveToFile(paramBufferedImage, filePath, fileName, "png");
}
/**
* 保存图片文件
*
* @param paramBufferedImage
* 图片流
* @param filePath
* 文件路径
* @param imgName
* 图片参数
* @param imgFormat
* 图片格式
*/
private void saveToFile(BufferedImage paramBufferedImage, String filePath,
String imgName, String imgFormat) {
try {
System.out.println("saveToFile");
FileOutputStream fileOutputStream = null;
try {
String dirPath = filePath;
File dirFile = new File(dirPath);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
String imgPath = dirPath + imgName;
fileOutputStream = new FileOutputStream(imgPath);
} catch (Exception e) {
System.out.println("Create Img File Error:" + e.toString());
}
ImageUtil.encodeAndWrite(paramBufferedImage, imgFormat,
fileOutputStream, 96, 96);
fileOutputStream.close();
} catch (Exception localException) {
System.out.println("Save Img File Error:" + localException);
localException.printStackTrace();
}
}
}