java使用jbarcode生成条形码
来源:互联网 发布:新浪短域名 编辑:程序博客网 时间:2024/05/21 15:47
转载:点击打开链接
准备工作:
开发工具:eclipse4.5+jdk1.7
所需jar包:jbarcode-0.2.8.jar
案例:
- package jbarcode;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.ArrayList;
- import java.util.List;
- import org.jbarcode.JBarcode;
- import org.jbarcode.JBarcodeFactory;
- import org.jbarcode.encode.Code128Encoder;
- import org.jbarcode.encode.InvalidAtributeException;
- import org.jbarcode.paint.TextPainter;
- import org.jbarcode.util.ImageUtil;
-
-
-
-
-
-
-
-
-
-
- public class JbarcodeUtil {
-
-
- private static final int BARCODE_HEIGHT = 40;
-
- private static final int BARCODE_DPI = ImageUtil.DEFAULT_DPI;
-
- private static final String FONT_FAMILY = "console";
-
- private static final int FONT_SIZE = 15;
-
- public static String TEXT = "";
-
- private static JBarcode jbc = null;
-
- static JBarcode getJBarcode() throws InvalidAtributeException {
-
-
-
-
-
-
-
-
-
-
-
-
- if (jbc == null) {
-
- jbc = JBarcodeFactory.getInstance().createCode128();
- jbc.setEncoder(Code128Encoder.getInstance());
- jbc.setTextPainter(CustomTextPainter.getInstance());
- jbc.setBarHeight(BARCODE_HEIGHT);
- jbc.setXDimension(Double.valueOf(0.8).doubleValue());
- jbc.setShowText(true);
- }
- return jbc;
- }
-
-
-
-
-
-
- public static void createBarcode(String message, File file,String text) {
- try {
- FileOutputStream fos = new FileOutputStream(file);
- createBarcode(message, fos,text);
- fos.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
-
-
-
-
-
- public static void createBarcode(String message, OutputStream os,String text) {
- try {
-
- TEXT=text;
-
- BufferedImage image = getJBarcode().createBarcode(message);
- ImageUtil.encodeAndWrite(image, ImageUtil.PNG, os, BARCODE_DPI, BARCODE_DPI);
- os.flush();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
-
-
-
-
-
- protected static class CustomTextPainter implements TextPainter {
- private static CustomTextPainter instance =new CustomTextPainter();
- public static CustomTextPainter getInstance() {
- return instance;
- }
- public void paintText(BufferedImage barCodeImage, String text, int width) {
-
- Graphics g2d = barCodeImage.getGraphics();
-
- Font font = new Font(FONT_FAMILY, Font.PLAIN, FONT_SIZE * width);
- g2d.setFont(font);
- FontMetrics fm = g2d.getFontMetrics();
- int height = fm.getHeight();
- int center = (barCodeImage.getWidth() - fm.stringWidth(text)) / 2;
- g2d.setColor(Color.WHITE);
- g2d.fillRect(0, 0, barCodeImage.getWidth(), barCodeImage.getHeight() * 1 / 20);
- g2d.fillRect(0, barCodeImage.getHeight() - (height * 9 / 10), barCodeImage.getWidth(), (height * 9 / 10));
- g2d.setColor(Color.BLACK);
-
- g2d.drawString(TEXT, 0, 145);
-
- g2d.drawString(text, center, barCodeImage.getHeight() - (height / 10) - 2);
- }
- }
-
-
- public static void main(String[] args) throws FileNotFoundException, IOException {
- List<String> list=new ArrayList<String>();
- list.add("KJ4.1-0127-0001");
- list.add("KJ4.1-0128-0001");
- list.add("KJ4.1-0129-0001");
- list.add("KJ4.1-0130-0001");
- if(list!=null && list.size()>0){
- for(String message:list){
- JbarcodeUtil.createBarcode(message, new File("D:\\codeImg\\"+message+".png"),"苏交科");
- }
- }
-
- }
- }
效果图:
条形码样式:
备注:
1.//设置条形码高度
private static final int BARCODE_HEIGHT = 20;
//设置条形码字体大小
private static final int FONT_SIZE = 15;
这2个设置大小很重要,若是设置值小了则看不到文件如“苏薇”,自己可以把值修改为12运行下会发现文本"苏微"看不到,这是由于高度太小,字体无法显示
2.生成的条形码用扫码枪可以扫描,但是有时候扫描不了,原因是生成的条形码密度太厚,故"jbc.setXDimension(Double.valueOf(0.8).doubleValue());"设置很重要,值越小密度越细,条形码宽度越宽。
3.案例中message="KJ4.1-0130-0001",若message="KJ4.1-0130-0001(001)"则扫描不了,原因识别不了括号
4.该案例生成的条形码扫描反应慢