java生产条形码

 
一、通过JBarcode(此种方式可以隐藏掉条形码下面的字符串
 
1.下载jar包  jbarcode-0.2.8.jar
 目前maven中央仓库并没有jbarcode的坐标
 如果是maven项目,需要我们自己上传jar包到自己的私服中并配置maven坐标
        <!--jbarcode-->
        <dependency>
            <groupId>org.jbarcode</groupId>
            <artifactId>JBarcode</artifactId>
            <version>0.2.8</version>
        </dependency>
 
2.工具代码:
package com.beiquan.tts.utils;
 
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 javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
 
/**
* Created by zhangboqing on 2017/5/24.
*/
public class JBarCodeUtils {
 
 
    /**
     * 128条形码
     *
     * @param strBarCode
     *            条形码:0-100位
     * @param dimension
     *            商品条形码:尺寸
     * @param barheight
     *            商品条形码:高度
     * @return 图片(Base64编码)
     */
    public static void generateBarCode128(String strBarCode,String dimension,String barheight,OutputStream ous) {
 
 
        try {
            BufferedImage bi ;
            int len = strBarCode.length();
            JBarcode productBarcode = new JBarcode(Code128Encoder.getInstance(),
                    WidthCodedPainter.getInstance(),
                    EAN13TextPainter.getInstance());
 
            // 尺寸,面积,大小 密集程度
            productBarcode.setXDimension(Double.valueOf(dimension).doubleValue());
            // 高度 10.0 = 1cm 默认1.5cm
            productBarcode.setBarHeight(Double.valueOf(barheight).doubleValue());
            // 宽度
            productBarcode.setWideRatio(Double.valueOf(30).doubleValue());
//                  是否显示字体
            productBarcode.setShowText(false);
//                 显示字体样式
            productBarcode.setTextPainter(BaseLineTextPainter.getInstance());
 
            // 生成二维码
            bi = productBarcode.createBarcode(strBarCode);
 
            ImageIO.write(bi, "jpg", ous);
 
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
 
 
 
 
 
    /**
     * 商品条形码
     * @param strBarCode
     *            商品条形码:13位
     * @param dimension
     *            商品条形码:尺寸
     * @param barheight
     *            商品条形码:高度
     * @return 图片(Base64编码)
     */
    public static void generateBarCode(String strBarCode,String dimension,String barheight,OutputStream ous) {
//      isNumeric 是否是数值
//      校验。。。。。
 
 
        try {
            BufferedImage bi ;
            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);
 
            //校验13位
            String checkCode = productBarcode.calcCheckSum(barCode);
            if (!code.equals(checkCode)) {
            }
 
 
            // 尺寸,面积,大小
            productBarcode.setXDimension(Double.valueOf(dimension).doubleValue());
            // 高度 10.0 = 1cm 默认1.5cm
            productBarcode.setBarHeight(Double.valueOf(barheight).doubleValue());
            // 宽度
            productBarcode.setWideRatio(Double.valueOf(25).doubleValue());
 
            // 是否校验13位,默认false
            productBarcode.setShowCheckDigit(true);
 
            //显示字符串内容中是否显示检查码内容
//          productBarcode.setShowCheckDigit(true);
 
            // 生成二维码
            bi = productBarcode.createBarcode(barCode);
 
            ImageIO.write(bi, "jpg", ous);
 
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
 
    /**
     * @param args
     * @throws InvalidAtributeException
     */
    public static void main(String[] args) throws InvalidAtributeException {
 
    }
}
 
 
 
二、通过barcode4j
1.配置maven 坐标
        <!--barcode4j-->
        <dependency>
            <groupId>net.sf.barcode4j</groupId>
            <artifactId>barcode4j-light</artifactId>
            <version>2.0</version>
        </dependency>
2.工具代码:
package com.beiquan.tts.utils;
 
import org.apache.commons.lang.StringUtils;
import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;
 
import java.awt.image.BufferedImage;
import java.io.*;
 
/**
* Created by zhangboqing on 2017/5/23.
*/
public class CreateBarCodeUtils {
 
 
    /**
     * 生成文件
     *
     * @param msg
     * @param path
     * @return
     */
    public static File generateFile(String msg, String path) {
        File file = new File(path);
        try {
            generate(msg, new FileOutputStream(file));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        return file;
    }
 
    /**
     * 生成字节
     *
     * @param msg
     * @return
     */
    public static byte[] generate(String msg) {
        ByteArrayOutputStream ous = new ByteArrayOutputStream();
        generate(msg, ous);
        return ous.toByteArray();
    }
 
    /**
     * 生成到流
     *
     * @param msg
     * @param ous
     */
    public static void generate(String msg, OutputStream ous) {
        if (StringUtils.isEmpty(msg) || ous == null) {
            return;
        }
 
        Code39Bean bean = new Code39Bean();
 
        // 精细度
        final int dpi = 150;
        // module宽度
        final double moduleWidth = UnitConv.in2mm(1.0f / dpi);
 
        // 配置对象
        bean.setModuleWidth(moduleWidth);
        bean.setWideFactor(3);
        bean.doQuietZone(false);
 
        String format = "image/png";
        try {
 
            // 输出到流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, dpi,
                    BufferedImage.TYPE_BYTE_BINARY, false, 0);
 
            // 生成条形码
            bean.generateBarcode(canvas, msg);
 
            // 结束绘制
            canvas.finish();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
    public static void main(String[] args) {
        String msg = "123456789";
        String path = "barcode.png";
        generateFile(msg, path);
    }
}
 
 
 
posted @ 2017-05-25 08:30  N!CE波  阅读(3615)  评论(0编辑  收藏  举报