无聊人士

搬家==》www.soapui.cn

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  32 随笔 :: 0 文章 :: 60 评论 :: 0 Trackbacks
条码显示,在birt中最常见的有两种方法:1、使用条码字体(对pdf无效);2、用barcode的开源包,生成barcode,然后在报表里用动态地址去取图片。

今晚看birt文档(第 23 章 使用 Java 编写事件处理程序),例子中用java实现了一个LabelEventAdapter的适配器,对标签元素进行事件控制。脑子里灵光一现,似乎条码有着落了。

我的测试例子很简单,继承ImageEventAdapter类,重载onCreate方法,以进行条形码处理

 1 package cn.ynzc.common.birt.test;
 2 
 3 import java.io.File;
 4 import java.io.FileOutputStream;
 5 
 6 import jbarcodebean.Code128;
 7 import jbarcodebean.JBarcodeBean;
 8 
 9 import org.apache.commons.codec.digest.DigestUtils;
10 import org.eclipse.birt.report.engine.api.script.IReportContext;
11 import org.eclipse.birt.report.engine.api.script.eventadapter.ImageEventAdapter;
12 import org.eclipse.birt.report.engine.api.script.instance.IImageInstance;
13 
14 public class MyLabelClass extends ImageEventAdapter {
15 
16   public void onCreate(IImageInstance image, IReportContext reportContext) {
17     try {
18       //实际应用中,可以使用image.getRowData().getColumnValue("columnname")获得字段值
19       String code = "ABCDEF123-2222";
20       //似乎windows文件名中不允许使用“-”等符号,干脆将code进行md5散列处理
21       File file = new File(System.getProperty("java.io.tmpdir"), DigestUtils.md5Hex(code));
22       //避免每次都进行条码文件生成
23       if (!file.exists()) {
24         JBarcodeBean bb = new JBarcodeBean();
25         bb.setCodeType(new Code128());
26         bb.setShowText(true);
27         bb.setBarcodeHeight(45); //条码高度
28         bb.setCode(code);
29         bb.gifEncode(new FileOutputStream(file));
30       }
31       image.setFile(file.getAbsolutePath());
32     }
33     catch (Exception e) {
34       e.printStackTrace();
35     }
36   }
37 
38 }
39 

测试用的birt报表文件简单得要死,就往上面扔了个image元素,设置其Event Handler Class为刚才写好的java类,最终得到的rptdesign文件内容如下:
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!-- Written by Eclipse BIRT 2.0 -->
 3 <report xmlns="http://www.eclipse.org/birt/2005/design" version="3.2.6" id="1">
 4     <property name="createdBy">Eclipse BIRT Designer Version 2.1.2.v20070205-1728 Build &lt;20070205-1728></property>
 5     <property name="units">in</property>
 6     <page-setup>
 7         <simple-master-page name="Simple MasterPage" id="2"/>
 8     </page-setup>
 9     <body>
10         <image id="4">
11             <property name="eventHandlerClass">cn.ynzc.common.birt.test.MyLabelClass</property>
12         </image>
13     </body>
14 </report>

运行测试,条形码出来了
birt.jpg

遗留问题:
这次是调用org.eclipse.birt.report.engine.api.script.instance.IImageInstance.setFile()来解决问题,从javadoc可以看到,IImageInstance有很多方法可以调用,其它方法分别有什么作用?比如我尝试了半天的setData(byte[])方法,一开始以为是用这个方法直接把图形数据set进去就ok,结果未成功。


posted on 2007-04-07 03:37 mmwy 阅读(4160) 评论(4)  编辑  收藏 所属分类: 报表(Birt、crystal。。)

评论

# re: 在birt2.1.2中显示条形码 2007-04-08 13:25 mmwy
写在birt的脚本里面也行

<image id="4">
<method name="onCreate"><![CDATA[
importPackage(java.io);
importPackage(Packages.jbarcodebean);
importPackage(Packages.org.apache.commons.codec.digest);

code="ABCDEF-01234567-ZYX";
f = new java.io.File(java.lang.System.getProperty("java.io.tmpdir"), "barcode_" + DigestUtils.md5Hex(code) + ".gif");
//条码缓存1小时
if (!f.exists() || java.lang.System.currentTimeMillis() - f.lastModified() > 1000 * 60 * 60) {
bb = new JBarcodeBean();
bb.setCodeType(new Code128());
bb.setShowText(true);
bb.setBarcodeHeight(45);
bb.setCode(code);
bb.gifEncode(new FileOutputStream(f));
}
this.file=f.getAbsolutePath();
]]>
</method>
</image>

  回复  更多评论
  

# re: 在birt2.1.2中显示条形码 2007-07-21 01:50 Sutra
写在birt的脚本里面也行:

测试过了,这样写绝对通不过,真不知道为什么,是JAR没导入吗?  回复  更多评论
  

# re: 在birt2.1.2中显示条形码 2007-09-20 16:23 TT
什么啊,根本不好用, bb.gifEncode(new FileOutputStream(file));有问题。  回复  更多评论
  

# re: 在birt2.1.2中显示条形码 2008-07-05 12:43 zhiwenyue
能不能将你的birt文档上传一份阿,我想看看,因为现在要做一个关于birt响应客户要求的东西。谢谢~
或者发到我邮箱里abcdefg_zhiwenyue@yahoo.com.cn  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: