BarCodeUtile
package com.rscode.credits.util; 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.code128.Code128Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv; /**
* @author 作者 TN
* @version 创建时间:2018年10月24日
* 类说明 条形码工具类
*/
public class BarCodeUtile {
/**
* 生成文件
*
* @param msg 条形码信息
* @param barCodePath 条形码图片存储地址 +图片名
* @return 返回生成的文件
*/
public static File generateFile(String msg, String barCodePath) {
File file = new File(barCodePath);
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();
// EAN13Bean bean=new EAN13Bean();//条形码类型 // 精细度
final int dpi = 150;
// module宽度
final double moduleWidth = UnitConv.in2mm(2.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);
// 生成条形码
System.err.println("条形码msg:"+msg);
bean.generateBarcode(canvas, msg); // 结束绘制
canvas.finish();
} catch (IOException e) {
throw new RuntimeException(e);
}
} // public static void main(String[] args) {
// String msg ="123456789123"; //一时间加ID生成图片 内容
// String imageName = msg+".png"; //图片名字
// String path = "C:\\Users\\13320\\Desktop\\WORK\\Image\\barcode\\"+imageName;
// File file = generateFile(msg, path);
// String name = file.getName();
// System.err.println(name);
// } }
BarCodeUtile的更多相关文章
随机推荐
- PROJ.4学习——坐标系转换
PROJ.4学习——坐标系转换 前言 PROJ可以做任从最简单的投影到许多参考数据非常复杂的转换.PROJ最初是作为地图投影工具开发的,但随着时间的推移,它已经发展成为一个强大的通用坐标转换引擎,可以 ...
- HTML、CSS(小笔记)
这是我自己在学习html.css时觉得要记的东西太多总结一些较为常用的标签. HTML <p></p>段落标签 <hn></hn>标题标签n数值为1~6 ...
- python format(格式化)
自 python 2.6 开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱.#语法它通过{ ...
- HFun.快速开发平台(二)=》自定义列表实例
应用系统中数据列表的展现是开发内容之一,实现的方式基本是通过编号具体的访问列表页实现,通过检索条件进行数据源的获取,列字段的描述,还可能会有检索条件的实现,列表数据的导出等功能. 为了将重复工作进行简 ...
- Python语言:Day9练习题及其答案
1.使用while循环输出1,2,3,4,5,6, 8,9,10 #!/usr/bin/python3 n = 1 while n <= 10: if n != 7: print(n) else ...
- 分页 工具类 前后台代码 Java JavaScript (ajax) 实现 讲解
[博客园cnblogs笔者m-yb原创, 转载请加本文博客链接,笔者github: https://github.com/mayangbo666,公众号aandb7,QQ群927113708]http ...
- content-type的几种取值
四种常见的 POST 提交数据方式 我们知道,HTTP 协议是以 ASCII 码传输,建立在 TCP/IP 协议之上的应用层规范.规范把 HTTP 请求分为三个部分:状态行.请求头.消息主体.类似于下 ...
- 运行python脚本时,报错InsecurePlatformWarning: A true SSLContext object is not available,解决方法
今天,要在新环境里运行一个python脚本,遇到下面的报错: /usr/lib/python2.7/site-packages/urllib3/util/ssl_.py:160: InsecurePl ...
- tar: Exiting with failure status due to previous errors
发生在tar压缩或者解压缩的过程中,原因是压缩包在建立的时候是用了sudo的,所以你解压的时候也要加上sudo,问题就很好解决了的
- shell脚本学习总结(不断更新中)
前言:自从大学毕业参加工作以来,接触的开发工作都是在服务端完成,于是接触了比较多的Linux当做开发机使用,或多或少有一些重复性的工作,于是开始琢磨学习一些shell脚本的知识,以便处理这些繁琐的事情 ...