生成一维码的方法(用这个的原因是因为它所用的依赖是可以在maven中央仓库能找到的,不用付费的)
来源:互联网 发布:win8磁盘优化无法打开 编辑:程序博客网 时间:2024/05/13 03:11
- import java.awt.image.BufferedImage;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import org.apache.avalon.framework.configuration.Configuration;
- import org.apache.avalon.framework.configuration.DefaultConfiguration;
- import org.apache.avalon.framework.logger.ConsoleLogger;
- import org.apache.avalon.framework.logger.Logger;
- import org.krysalis.barcode4j.BarcodeGenerator;
- import org.krysalis.barcode4j.BarcodeUtil;
- import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
- import org.krysalis.barcode4j.tools.MimeTypes;
- public class Test1
- {
- private transient Logger log =new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
- public staticvoid main(String[] args)
- {
- Test1 t =new Test1();
- t.getBarcode();
- System.out.println("success");
- }
- protected void getBarcode()
- {
- String format = MimeTypes.MIME_JPEG;// MimeTypes.MIME_JPEG
- String text = "ABCDE0123456789";
- ByteArrayOutputStream bout = null;
- try
- {
- BarcodeUtil util = BarcodeUtil.getInstance();
- Configuration cfg = buildCfg();
- BarcodeGenerator gen = util.createBarcodeGenerator(cfg);
- bout = new ByteArrayOutputStream(4096);
- int dpi = 300;// 分辨率
- int orientation = 0;
- BitmapCanvasProvider bitmap = new BitmapCanvasProvider(bout,
- format, dpi, BufferedImage.TYPE_BYTE_BINARY, false,
- orientation);
- gen.generateBarcode(bitmap, text);
- bitmap.finish();
- File file = new File("c://"+text+".jpg");
- FileOutputStream fos = new FileOutputStream(file);
- fos.write(bout.toByteArray(), 0, bout.size());
- // response.setContentType(format);
- // response.setContentLength(bout.size());
- // response.getOutputStream().write(bout.toByteArray());
- // response.getOutputStream().write(bout.toByteArray());
- // response.getOutputStream().flush();
- }
- catch (Exception e)
- {
- log.error("Error while generating barcode", e);
- }
- finally
- {
- if (bout != null)
- {
- try
- {
- bout.close();
- }
- catch (IOException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- /**
- * Build an Avalon Configuration object from the request.
- *
- * @return the newly built COnfiguration object
- * @todo Change to bean API
- */
- protected Configuration buildCfg()
- {
- DefaultConfiguration cfg = new DefaultConfiguration("barcode");
- // Get type
- String type = "code128";
- DefaultConfiguration child = new DefaultConfiguration(type);
- cfg.addChild(child);
- // DefaultConfiguration attr;
- // // height
- // String height = "100px";
- // attr = new DefaultConfiguration("height");
- // attr.setValue(height);
- // child.addChild(attr);
- //
- // // width
- // String moduleWidth = "300px";
- // attr = new DefaultConfiguration("module-width");
- // attr.setValue(moduleWidth);
- // child.addChild(attr);
- return cfg;
- }
- }
0 0
- 生成一维码的方法(用这个的原因是因为它所用的依赖是可以在maven中央仓库能找到的,不用付费的)
- 在国内访问Maven仓库,连接速度太慢。下面是将中央仓库替换成阿里云的中央仓库的方法。
- IDEA中maven项目导入依赖包太慢,因为你的中央仓库配置的国外....
- CSDN的BLOG可以正常使用了,原来是因为这个原因啊!
- maven 国内的中央仓库
- maven常用的中央仓库
- 使用springtoolsuit的原因是因为可以整合flex项目
- Maven的中央仓库,可以搜索jar的坐标
- Maven依赖的是本地工程还是仓库jar包?
- Maven依赖的是本地工程还是仓库jar包?
- maven的本地仓库和中央仓库
- 速度还可以的maven中央仓库地址
- 【maven】在setting文件配置中央仓库的镜像
- Maven入门系列(二)--设置中央仓库的方法
- maven中央仓库被屏蔽的解决方案
- maven中央仓库不能访问的解决办法
- Maven阿里云的中央仓库
- maven中央仓库的地址,搜索坐标
- POJ2726 Holiday Hotel ACM解题报告(贪心)
- android app 自动更新
- android4.2 -- 蓝牙 bluetooth (二) 打开蓝牙
- 九、构造函数和析构函数(三) 初始化列表、对象成员初始化、const,引用成员初始化
- Ubuntu下视频的处理
- 生成一维码的方法(用这个的原因是因为它所用的依赖是可以在maven中央仓库能找到的,不用付费的)
- 机械硬盘内部硬件结构和工作原理详解
- Entity Framework6 with Oracle(可实现code first)
- DTD简介入门
- java远程方法调用(简单入门)
- 简单的a,b变量交换问题,不使用中间变量
- android4.3 -- 蓝牙 bluetooth (三)搜索蓝牙
- Windows平台下我使用的一些软件
- android 4.2-- 蓝牙 bluetooth (四)OPP文件传输