一维条码生成器

 本代码利用了jbarcode的jar包生成一维条码,代码如下,如有问题,还请见谅

package f;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
 
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import org.jbarcode.JBarcode;
import org.jbarcode.encode.EAN13Encoder;
import org.jbarcode.encode.InvalidAtributeException;
import org.jbarcode.paint.EAN13TextPainter;
import org.jbarcode.paint.WidthCodedPainter;
import org.jbarcode.util.ImageUtil;

 
public class KKK {
 
    /**
     * 生成商品条形码
     *
     * @param filePath
     *            商品条形码图片存放路径:C://barcode//images//
     *
     * @param barCode
     *            商品条形码:13位
     * @param imgFormat
     *            图片格式
     *           
     * @return 图片存放路径+图片名称+图片文件类型
     */
    public static String createBarCode(String savePath, String jbarCode,
            String imgFormat) {
 
        // 校验全部省略……
        // if(StringUtils.isNotEmpty(savePath)){
        //
 
        // return null;
        // }
        // if(StringUtils.isNotEmpty(jbarCode)){
        // return null;
        // }
        // if(StringUtils.isNotEmpty
 
        // (imgFormat)){
        // return null;
        // }
        // if( jbarCode.length()!=13){
        // return null;
        // }
 
        try {
 
            BufferedImage bi = null;
 
            int len = jbarCode.length();
 
            // 实例化JBarcode
            // 这里三个参数,必要填写
            JBarcode jbarcode13 = new JBarcode(EAN13Encoder.getInstance(),
                    WidthCodedPainter.getInstance(),
                    EAN13TextPainter.getInstance());
 
            // 获取到前12位
            String barCode = jbarCode.substring(0, len - 1);
 
            // 获取到校验位
            String code = jbarCode.substring(len - 1, len);
            String checkCode = jbarcode13.calcCheckSum(barCode);
 
            if (!code.equals(checkCode)) {
                return "EN-13 条形码最后一位校验码 不对,应该是: " + checkCode;
            }
 
            /*
             * 最重要的是这里的设置,如果明白了这里的设置就没有问题 如果是默认设置,
             * 那么设置就是生成一般的条形码 如果不是默认
             * 设置,那么就可以根据自己需要设置
             */
 
            // 尺寸,面积,大小
            jbarcode13.setXDimension(Double.valueOf(0.8).doubleValue());
            // 条形码高度
            jbarcode13.setBarHeight(Double.valueOf(30).doubleValue());
            // 宽度率
            jbarcode13.setWideRatio(Double.valueOf(20).doubleValue());
            // 是否校验最后一位,默认是false
            jbarcode13.setShowCheckDigit(true);
 
            // 生成二维码
            bi = jbarcode13.createBarcode(barCode);
 
            // 定义图片名称
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
            String imgName = sdf.format(new Date()) + "_" + jbarCode;
 
            // 保存二维码图片
 
            FileOutputStream fileOutputStream = null;
            String imgPath = savePath + imgName + "." + imgFormat;
            try {
                try {
                    savePath = URLDecoder.decode(savePath, "UTF-8");
                } catch (UnsupportedEncodingException uee) {
                    uee.printStackTrace();
                    savePath = "d://test//";
                }
                File dirFile = new File(savePath);
 
                if (!dirFile.exists()) {
                    dirFile.mkdirs();
                }
 
                fileOutputStream = new FileOutputStream(imgPath);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            ImageUtil.encodeAndWrite(bi, imgFormat, fileOutputStream, 96, 96);
            fileOutputStream.close();
 
            // 返回路径
            return imgPath;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * @param args
     * @throws InvalidAtributeException
     */
    public static void main(String[] args) throws InvalidAtributeException {
 
        String path1 = KKK.createBarCode("D://test//", "6937748304340", ImageUtil.JPEG);
 
        String path2 = KKK.createBarCode("D://test//", "6920177930938", ImageUtil.JPEG);
        
        System.out.println(path1);
        System.out.println(path2);
 
    }
 
}

 生成的截图如下:

 

 

jar包建议去maven仓库内寻找

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值