动态处理BIRT中的图片

本文介绍如何处理BIRT报表中的动态图片,特别是当图片以压缩的CLOB形式存于数据库时,通过Java程序读取、解压并更新到BIRT模板的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
动态处理BIRT中的图片
如果数据库中的图片是经过压缩后以CLOB的形式存放的,该如何将数据库中的图片放到BIRT中?
直接在BIRT中使用数据集是无法显示图片的。所以必须先将数据库中的图片数据取出,解压缩后转换成字节数组。通过后台程序把字节数组写入BIRT模板。具体实现如下:
1、 设计模板
在设计模板时,插入的图片应该选择Embedded image 方式。任意插入一张图片,将图片的名称修改成自己需要的名称(在 XML source中修改)。
2、 后台程序设计
       ReportEngine engine =null;
        EngineConfig config = new EngineConfig();
        String engineHome="c:/ReportEngine";
        config.setEngineHome(engineHome);
        engine = new ReportEngine(config);
        IReportRunnable design=null;
        try {
            design =engine.openReportDesign("c:/birt/test.rptdesign");
            ReportDesignHandle reportHandle = (ReportDesignHandle) design.getDesignHandle();
            List imgList=reportHandle.getAllImages();
            System.out.println(imgList.size());
            for(int i=0;i<imgList.size();i++){
                if(imgList.get(i) instanceof EmbeddedImageHandle ){
EmbeddedImageHandle
ih=(EmbeddedImageHandle)imgList.get(i);
                    String imgName=ih.getName();
                    System.out.println(imgName);
                    byte[] s=ih.getData();
                    FileOutputStream
fos = new FileOutputStream("c:/birt.jpg");
                    fos.write(s);
                    fos.flush();
                    fos.close();
                    FileInputStream
fis = new FileInputStream("c:/test.jpg");
                    ByteArrayOutputStream
baos = new ByteArrayOutputStream();
                    byte [] b = newbyte[2048];
                    int len =0;
                    while((len=fis.read(b))>0){
                        baos.write(b, 0, len);
                    }
                    baos.flush();
                    byte []imgData=baos.toByteArray();
                    ih.setData(imgData);
                }else{
                    System.out.println("CLASS NAME:"+imgList.get(i).getClass().getName());;
                }
            }
            IRunAndRenderTask task = engine.createRunAndRenderTask(design);
            HTMLRenderOption options = new HTMLRenderOption();
            options.setOutputFileName("c:/test.pdf");
            options.setOutputFormat("pdf");
            task.setRenderOption(options);
            task.run();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
找到报表模板中的EmbeddedImageHandle,用public void setData(byte data[])方法设置这个图片的数据。
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值