無聊人士

          搬家==》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 閱讀(4232) 評論(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  回復  更多評論
            

          主站蜘蛛池模板: 南漳县| 遵化市| 石渠县| 凭祥市| 蒙阴县| 金门县| 临高县| 绵阳市| 抚顺市| 长阳| 电白县| 鱼台县| 丹东市| 正宁县| 江华| 连城县| 乌拉特前旗| 阿勒泰市| 宜良县| 新巴尔虎左旗| 马龙县| 平谷区| 交口县| 石城县| 南充市| 沾益县| 吴堡县| 花莲市| 集贤县| 德化县| 通河县| 濉溪县| 上犹县| 静安区| 柏乡县| 彩票| 蓬溪县| 政和县| 富蕴县| 崇左市| 榆树市|