zoukankan      html  css  js  c++  java
  • 生成商品条形码代码事例

    jar包引入:

    <!-- 商品条形码-->
    <dependency>
    <groupId>net.sf.barcode4j</groupId>
    <artifactId>barcode4j-light</artifactId>
    <version>2.0</version>
    </dependency>

    工具类:
    package com.inborn.inshop.utils;

    import java.awt.image.BufferedImage;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    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;

    /**
    * 条形码工具类
    */
    public class BarcodeUtil {
    /**
    * 生成文件
    *
    * @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 = "1234567893124124321";
    // String path = "c:/barcode.png";
    // generateFile(msg, path);
    // }
    }
    Controller类:
    /**
    * 入库商品条形码下载
    * @param request
    * @param resp
    * @param depotId
    */
    @RequestMapping(value = "/depotCode")
    @ResponseBody
    public void exportList(HttpServletRequest request,
    HttpServletResponse resp, Long depotId) throws IOException {
    // 使用流将数据导出
    OutputStream outStream = resp.getOutputStream();
    try{
    String picName= toUtf8String(DateUtils.dateToString(new Date(),"yyyy-MM-dd")+"入库码"+".png");
    resp.setContentType("application/zip");
    resp.setHeader("Content-Disposition", "attachment; filename="+picName);
    // 禁止图像缓存。
    resp.setHeader("Cache-Control", "no-cache");
    resp.setDateHeader("Expires", 0);
    BarcodeUtil.generate(depotId+"",outStream);
    outStream.flush();
    }catch (Exception e){
    outStream.close();
    }finally {
    outStream.close();
    }
    }
    public static String toUtf8String(String s){
    StringBuffer sb = new StringBuffer();
    for (int i=0;i<s.length();i++){
    char c = s.charAt(i);
    if (c >= 0 && c <= 255){sb.append(c);}
    else{
    byte[] b;
    try { b = Character.toString(c).getBytes("utf-8");}
    catch (Exception ex) {
    System.out.println(ex);
    b = new byte[0];
    }
    for (int j = 0; j < b.length; j++) {
    int k = b[j];
    if (k < 0) k += 256;
    sb.append("%" + Integer.toHexString(k).toUpperCase());
    }
    }
    }
    return sb.toString();
    }
    效果:

     
  • 相关阅读:
    团队冲刺第一阶段第三天
    团队冲刺第一阶段第二天
    团队冲刺第一阶段第一天
    学习进度07
    学习进度06
    结对编程之子数组最大的和(由于一直登不上我的账号,在规定日期内只在我搭档的博客上交了)
    小学四则运算网页版
    团队介绍
    学习进度条05
    111
  • 原文地址:https://www.cnblogs.com/lvgg/p/6826562.html
Copyright © 2011-2022 走看看