JAVA生成条码(jbarcode)

简介:

条码技术在现在的应用非常广泛,几乎所有的商品包装上都会印有条形码,这一篇将通过实例演示JAVA如何生成条码,需要使用的第三方包为:jbarcode

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import org.jbarcode.JBarcode;
import org.jbarcode.encode.CodabarEncoder;
import org.jbarcode.encode.Code11Encoder;
import org.jbarcode.encode.Code128Encoder;
import org.jbarcode.encode.Code39Encoder;
import org.jbarcode.encode.Code39ExtEncoder;
import org.jbarcode.encode.Code93Encoder;
import org.jbarcode.encode.Code93ExtEncoder;
import org.jbarcode.encode.EAN13Encoder;
import org.jbarcode.encode.EAN8Encoder;
import org.jbarcode.encode.Interleaved2of5Encoder;
import org.jbarcode.encode.InvalidAtributeException;
import org.jbarcode.encode.MSIPlesseyEncoder;
import org.jbarcode.encode.PostNetEncoder;
import org.jbarcode.encode.Standard2of5Encoder;
import org.jbarcode.encode.UPCAEncoder;
import org.jbarcode.encode.UPCEEncoder;
import org.jbarcode.paint.BaseLineTextPainter;
import org.jbarcode.paint.EAN13TextPainter;
import org.jbarcode.paint.EAN8TextPainter;
import org.jbarcode.paint.HeightCodedPainter;
import org.jbarcode.paint.UPCATextPainter;
import org.jbarcode.paint.UPCETextPainter;
import org.jbarcode.paint.WideRatioCodedPainter;
import org.jbarcode.paint.WidthCodedPainter;

/**
 * 条形码创建,需添加jar包:jbarcode-0.2.8.jar
 * 
 * @author jianggujin
 * 
 */
public class BarcodeCreater
{
   /** 用于生成条形码的对象 **/
   private JBarcode barcode = null;

   /**
    * 构造方法
    */
   public BarcodeCreater()
   {
      barcode = new JBarcode(EAN13Encoder.getInstance(),
            WidthCodedPainter.getInstance(), EAN13TextPainter.getInstance());
      barcode.setBarHeight(17);
      barcode.setShowText(true);
      barcode.setCheckDigit(true);
      barcode.setShowCheckDigit(true);
   }

   /**
    * 生成条形码文件
    * 
    * @param code
    *           条形码内容
    * @param file
    *           生成文件
    * @throws InvalidAtributeException
    * @throws IOException
    */
   public void write(String code, File file) throws IOException,
         InvalidAtributeException
   {
      ImageIO.write(toBufferedImage(code), "JPEG", file);
   }

   /**
    * 生成条形码并写入指定输出流
    * 
    * @param code
    *           条形码内容
    * @param os
    *           输出流
    * @throws IOException
    * @throws InvalidAtributeException
    */
   public void write(String code, OutputStream os) throws IOException,
         InvalidAtributeException
   {
      ImageIO.write(toBufferedImage(code), "JPEG", os);
   }

   /**
    * 创建条形码的BufferedImage图像
    * 
    * @param code
    *           条形码内容
    * @return image
    * @throws InvalidAtributeException
    */
   public BufferedImage toBufferedImage(String code)
         throws InvalidAtributeException
   {
      return barcode.createBarcode(code);
   }

   /**
    * 设置编码
    * 
    * @param encoder
    */
   public void setEncoder(BarcodeEncoder encoder)
   {
      int val = encoder.ordinal();
      switch (val)
      {
      case 0:
         barcode.setEncoder(EAN13Encoder.getInstance());
         barcode.setPainter(WidthCodedPainter.getInstance());
         barcode.setTextPainter(EAN13TextPainter.getInstance());
         barcode.setBarHeight(17);
         barcode.setShowText(true);
         barcode.setCheckDigit(true);
         barcode.setShowCheckDigit(true);
         break;
      case 1:
         barcode.setEncoder(UPCAEncoder.getInstance());
         barcode.setPainter(WidthCodedPainter.getInstance());
         barcode.setTextPainter(UPCATextPainter.getInstance());
         barcode.setBarHeight(17);
         barcode.setShowText(true);
         barcode.setCheckDigit(true);
         barcode.setShowCheckDigit(true);
         break;
      case 2:
         barcode.setEncoder(EAN8Encoder.getInstance());
         barcode.setPainter(WidthCodedPainter.getInstance());
         barcode.setTextPainter(EAN8TextPainter.getInstance());
         barcode.setBarHeight(17);
         barcode.setShowText(true);
         barcode.setCheckDigit(true);
         barcode.setShowCheckDigit(true);
         break;
      case 3:
         barcode.setEncoder(UPCEEncoder.getInstance());
         barcode.setTextPainter(UPCETextPainter.getInstance());
         barcode.setPainter(WidthCodedPainter.getInstance());
         barcode.setBarHeight(17);
         barcode.setShowText(true);
         barcode.setCheckDigit(true);
         barcode.setShowCheckDigit(true);
         break;
      case 4:
         barcode.setEncoder(CodabarEncoder.getInstance());
         barcode.setPainter(WideRatioCodedPainter.getInstance());
         barcode.setTextPainter(BaseLineTextPainter.getInstance());
         barcode.setBarHeight(17);
         barcode.setShowText(true);
         barcode.setCheckDigit(true);
         barcode.setShowCheckDigit(true);
         break;
      case 5:
         barcode.setEncoder(Code11Encoder.getInstance());
         barcode.setPainter(WidthCodedPainter.getInstance());
         barcode.setTextPainter(BaseLineTextPainter.getInstance());
         barcode.setBarHeight(17);
         barcode.setShowText(true);
         barcode.setCheckDigit(true);
         barcode.setShowCheckDigit(true);
         break;
      case 6:
         barcode.setEncoder(Code39Encoder.getInstance());
         barcode.setPainter(WideRatioCodedPainter.getInstance());
         barcode.setTextPainter(BaseLineTextPainter.getInstance());
         barcode.setBarHeight(17);
         barcode.setShowText(true);
         barcode.setCheckDigit(false);
         barcode.setShowCheckDigit(false);
         break;
      case 7:
         barcode.setEncoder(Code39ExtEncoder.getInstance());
         barcode.setPainter(WideRatioCodedPainter.getInstance());
         barcode.setTextPainter(BaseLineTextPainter.getInstance());
         barcode.setBarHeight(17);
         barcode.setShowText(true);
         barcode.setCheckDigit(false);
         barcode.setShowCheckDigit(false);
         break;
      case 8:
         barcode.setEncoder(Code93Encoder.getInstance());
         barcode.setPainter(WidthCodedPainter.getInstance());
         barcode.setTextPainter(BaseLineTextPainter.getInstance());
         barcode.setBarHeight(17);
         barcode.setShowText(true);
         barcode.setCheckDigit(true);
         barcode.setShowCheckDigit(false);
         break;
      case 9:
         barcode.setEncoder(Code93ExtEncoder.getInstance());
         barcode.setPainter(WidthCodedPainter.getInstance());
         barcode.setTextPainter(BaseLineTextPainter.getInstance());
         barcode.setBarHeight(17);
         barcode.setShowText(true);
         barcode.setCheckDigit(true);
         barcode.setShowCheckDigit(false);
         break;
      case 10:
         barcode.setEncoder(Code128Encoder.getInstance());
         barcode.setPainter(WidthCodedPainter.getInstance());
         barcode.setTextPainter(BaseLineTextPainter.getInstance());
         barcode.setBarHeight(17);
         barcode.setShowText(true);
         barcode.setCheckDigit(true);
         barcode.setShowCheckDigit(false);
         break;
      case 11:
         barcode.setEncoder(MSIPlesseyEncoder.getInstance());
         barcode.setPainter(WidthCodedPainter.getInstance());
         barcode.setTextPainter(BaseLineTextPainter.getInstance());
         barcode.setBarHeight(17);
         barcode.setShowText(true);
         barcode.setCheckDigit(true);
         barcode.setShowCheckDigit(true);
         break;
      case 12:
         barcode.setEncoder(Standard2of5Encoder.getInstance());
         barcode.setPainter(WideRatioCodedPainter.getInstance());
         barcode.setTextPainter(BaseLineTextPainter.getInstance());
         barcode.setBarHeight(17);
         barcode.setShowText(true);
         barcode.setCheckDigit(true);
         barcode.setShowCheckDigit(false);
         break;
      case 13:
         barcode.setEncoder(Interleaved2of5Encoder.getInstance());
         barcode.setPainter(WideRatioCodedPainter.getInstance());
         barcode.setTextPainter(BaseLineTextPainter.getInstance());
         barcode.setBarHeight(17);
         barcode.setShowText(true);
         barcode.setCheckDigit(true);
         barcode.setShowCheckDigit(true);
         break;
      case 14:
         barcode.setEncoder(PostNetEncoder.getInstance());
         barcode.setPainter(HeightCodedPainter.getInstance());
         barcode.setTextPainter(BaseLineTextPainter.getInstance());
         barcode.setBarHeight(6);
         barcode.setShowText(false);
         barcode.setCheckDigit(true);
         barcode.setShowCheckDigit(false);
         break;
      }
   }

   /**
    * 条形码编码方式
    * 
    * @author jianggujin
    * 
    */
   public enum BarcodeEncoder
   {
      EAN13, UPCA, EAN8, UPCE, Codabar, Code11, Code39, Code39Ext, Code93, Code93Ext, Code128, MSIPlessey, Standard2of5, Interleaved2of5, PostNet
   }
}
目录
相关文章
|
1月前
|
小程序 前端开发 Java
java 生成小程序二维码
java 生成小程序二维码
14 0
|
6月前
|
Java
【Java】生成条形码工具类
【Java】生成条形码工具类
80 0
|
11月前
|
JSON Java API
Java获取未来60天的天气预报
Java获取未来60天的天气预报,
93 3
|
机器学习/深度学习 Java 数据安全/隐私保护
你有没有使用java生成过二维码?(二)
你有没有使用java生成过二维码?(二)
|
机器学习/深度学习 Java 数据安全/隐私保护
你有没有使用java生成过二维码?(一)
你有没有使用java生成过二维码?(一)
使用JAVA完成万年历
使用JAVA完成万年历
221 0
java202302java学习笔记第十四天-身份证信息查看
java202302java学习笔记第十四天-身份证信息查看
39 0
java202302java学习笔记第十四天-身份证信息查看
java202302java学习笔记第十四天-金额转换3
java202302java学习笔记第十四天-金额转换3
44 0
java202302java学习笔记第十四天-金额转换3
java202302java学习笔记第十四天-金额转换2
java202302java学习笔记第十四天-金额转换2
45 0
java202302java学习笔记第十四天-金额转换2
java202302java学习笔记第十四天-金额转换1
java202302java学习笔记第十四天-金额转换1
48 0
java202302java学习笔记第十四天-金额转换1