web项目生成条形码

工具:jdk1.7+tomcat7.0+mysql+Maven

框架:spring+spring MVC+Mybatis

条形码架包从Maven中央库下载:barcode4j-light。或者在pom.xml里面添加

1.写一个生成条形码的demo。类名为BarcodeUtil

我用的是Code-128编码方式。

public class BarcodeUtil {
   //是否有文件夹
    public static String getPath(String path){
        File srcFile = new File(path);
        if(!srcFile.exists()){
            srcFile.mkdirs();
        }
        String fileName = formatDate();
        String paths = fileName+".png";
        return paths;
    }
     /**
     * 生成文件
     *
     * @param msg
     * @param path
     * @return
     */
    public static File generateFile(String msg,String path) {
        String paths = path+File.separator+getPath(path);
        File file = new File(paths);
        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;

        }

        //

        Code128Bean bean = new Code128Bean();
        // 精细度
        final int dpi = 140;
        // module宽度
        final double moduleWidth = UnitConv.in2mm(1.0f / dpi);
        // 配置对象
        bean.setModuleWidth(moduleWidth);
        bean.setFontSize(3);
        bean.setQuietZone(2);
        bean.doQuietZone(true);
        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 String formatDate(){
        String fileName = null;
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        fileName = sdf.format(date);
        return fileName;

    }

public static void main(String[] args) {
        String code = "(8003)012300000000110000000032";
        String fileName = formatDate();
        String path = "D:"+File.separator+"barcode"+File.separator+fileName+".png";
        generateFile(code, path);

    }

测试结果如下:


2.Contrroler类中的添加方法。说明:我是在添加商品时生成条形码。

@RequestMapping(value = "save")

    public String save(PalletLease palletLease,HttpServletRequest request, HttpServletResponse response, Model model, RedirectAttributes redirectAttributes) {

        //获取服务器的物理路劲

         String configPath = request.getSession().getServletContext().getRealPath( "/barcode/" );

        //生成的条形码写到服务器的物理路劲里

         BarcodeUtil.generateFile(palletLeaseCode, configPath);

         String barcode = BarcodeUtil.getPath(configPath);

        //获取服务器的绝对路劲并保存到数据库。

         String path = request.getContextPath()+"/barcode/"+barcode;
         palletLease.setBarcode(path);
         palletLeaseService.save(palletLease);

}

到这里条形码就生成完成,谢谢大家观看,写的不好的地方请指出。


猜你喜欢

转载自blog.csdn.net/demo_gsl/article/details/80826155