• 生成商品条形码代码事例


    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();
    }
    效果:

     
  • 相关阅读:
    个人-GIT使用方法
    结对贪吃蛇游戏-结对编项目设计文档
    结对编程项目作业2-开发环境搭建过程
    团队编程项目作业2-团队编程项目设计文档
    结对编程项目作业5
    课后作业-阅读任务-阅读提问-3
    团队编程项目作业3-模块测试过程
    团队编程项目作业3-模块开发过程
    结对编程项目作业4
    课后作业-团队编程项目进度
  • 原文地址:https://www.cnblogs.com/lvgg/p/6826562.html
Copyright © 2020-2023  润新知