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

     
  • 相关阅读:
    Unity编译Android的原理解析和apk打包分析
    TokuDB性能测试报告
    一起脱去小程序的外套和内衣
    go单元测试进阶篇
    小程序发布后最全解析!
    部署在腾讯云的公益网站遭受了一次CC攻击
    基于Http原理实现Android的图片上传和表单提交
    腾讯云TCCE培训认证 精彩的第一次
    【腾讯云的1001种玩法】在腾讯云上创建您的SQL Cluster(5)
    腾讯云容器服务的滚动升级使用简介
  • 原文地址:https://www.cnblogs.com/lvgg/p/6826562.html
Copyright © 2011-2022 走看看