博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA生成条码(jbarcode)
阅读量:6591 次
发布时间:2019-06-24

本文共 8571 字,大约阅读时间需要 28 分钟。

条码技术在现在的应用非常广泛,几乎所有的商品包装上都会印有条形码,这一篇将通过实例演示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 }}

转载地址:http://ueuio.baihongyu.com/

你可能感兴趣的文章
JQuery——实现Ajax应用
查看>>
前端05.js入门之BOM对象与DOM对象。
查看>>
oracle kill所有plsql developer进程
查看>>
keepalived双机热备原理及实例部署LVS+keepalived
查看>>
曲线学习PyQt5方案一
查看>>
企业私有云之rabbitmq高可用
查看>>
OpenCV学习】矩阵运算和操作2
查看>>
nginx+ffmpeg搭建rtmp转播rtsp流的flash服务器
查看>>
Win10 IoT C#开发 1 - Raspberry安装IoT系统及搭建开发环境
查看>>
关于在arm裸板编程时使用printf问题的解决方法
查看>>
开源人工智能技术将改变一切
查看>>
2015 上半年 JavaScript 使用统计数据
查看>>
《Python算法教程》——1.6 如果您感兴趣
查看>>
深度解析Java8 – AbstractQueuedSynchronizer的实现分析(下)
查看>>
SSH原理与运用(一):远程登录
查看>>
Spring Framework 4.2 中的新功能和增强功能
查看>>
动态代理解决网站字符集编码
查看>>
我所想的GIX4的权限
查看>>
Hbuilder--让手爽,飞一般的编码(二)
查看>>
后台统计
查看>>