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的更多相关文章

随机推荐

  1. linux常用命令 print格式输出

    格式化输出命令 printf '输出类型 输出格式' 输出内容 输出类型: %ns 输出字符串,n是数字指代输出的几个字符 %ni 输出整数,n是数字指代输出几个数字 %m.nf 输出浮点数.m和n是 ...

  2. RuntimeError: Model class app_anme.models.User doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.---python学习错误记录

    untimeError: Model class app_anme.models.User doesn't declare an explicit app_label and isn't in an ...

  3. linux网络编程基础--(转自网络)

    转自 http://www.cnblogs.com/MyLove-Summer/p/5215287.html Linux下的网络编程指的是socket套接字编程,入门比较简单. 1. socket套接 ...

  4. MySQL拓展操作

    MySQL除了基本的增删该查功能,还有以下拓展功能: create table t1( id int ...., num int, xx int, unique 唯一索引名称 (列名,列名), con ...

  5. Linux关机总结

    立刻关机 root@ubuntu17:~# shutdown -h now 100分钟后关机 root@ubuntu17:~# shutdown -h + Shutdown scheduled -- ...

  6. 与Recommender System相关的会议及期刊

      会议 We refer specifically to ACM Recommender Systems (RecSys), established in 2007 and now the prem ...

  7. oralce执行计划

    看懂Oracle执行计划   最近一直在跟Oracle打交道,从最初的一脸懵逼到现在的略有所知,也来总结一下自己最近所学,不定时更新ing… 一:什么是Oracle执行计划? 执行计划是一条查询语句在 ...

  8. linux 普通用户批量创建账户与密码

    #!/bin/bash # add user imp - # by imp # ) do useradd imp$i echo "$i" |passwd --stdin imp$i ...

  9. while循环与 for循环,函数定义与调用

    import turtle turtle.setup(600,400,0,0) turtle.bgcolor('red') turtle.color('yellow') turtle.fillcolo ...

  10. 使用spring框架,用xml方式进行bean装配出现“The fully qualified name of the bean's class, except if it serves...”

    使用spring框架,用xml方式进行bean装配出现“The fully qualified name of the bean's class, except if it serves...”. 原 ...