润乾报表单元格显示二维码

需求描述

需要能在单元格内通过传入的字符串转化成二维码显示在单元格内。

通过自定义函数实现上述需求。

 

实现方法

1. 接受字符串,生成BufferedImage对象

2.  通过润乾报表提供的ImageUtils.writePNG(bi),返回byte[]给单元格

3. 单元格类型设置为图片字段

实现代码

1.         设计报表

 

2.         实现代码

1)         接受字符串为参数,生成BufferedImage对象

public BufferedImage CreateQrcode(String testString){

            Qrcode qrcode=new Qrcode();

            BufferedImage bi = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB);

            Graphics2D g = bi.createGraphics();

            try{

                qrcode.setQrcodeErrorCorrect('M');

                qrcode.setQrcodeEncodeMode('B');

                qrcode.setQrcodeVersion(7);

                byte[] d =testString.getBytes("GBK");

                g.setBackground(Color.WHITE);

                g.clearRect(0, 0, 139, 139);

                g.setColor(Color.BLACK);

                if (d.length>0 && d.length <123){

                    boolean[][] b = qrcode.calQrcode(d);

                    for (int i=0;i<b.length;i++){

                        for (int j=0;j<b.length;j++){

                            if (b[j][i]) {

                                g.fillRect(j*3+2,i*3+2,3,3);

                            }

                        }

 

                    }

                   

                }

              }catch(Exception e){

                e.printStackTrace();

            }finally{

                g.dispose();

                bi.flush();

            }

            return bi;

        }

2)         通过润乾报表提供的ImageUtils.writePNG(bi),返回byte[]给单元格

    public Object calculate(Context arg0, boolean arg1) {  

           //报表中调用该函数传递过来的参数列表

           byte[] want=null;

           if ( this.paramList.size() == 0 ) {

              throw new ReportError( "二维码自定义函数参数列表为空" );

           }

           //取得计算表达式(得到传递给报表的参数)

           Expression param1 = ( Expression )this.paramList.get( 0 );

           if ( param1 == null ) {

              throw new ReportError( "二维码自定义函数出现无效参数" );

           }

           //运算表达式,并取得运算结果(Object)

           Object result1 = Variant2.getValue( param1.calculate(arg0,arg1),false,arg1 );

            if( result1==null ) return null;

            if(result1 instanceof String){

            String str=result1.toString();

            System.out.println(str);

            BufferedImage bi=CreateQrcode(str);

            try {

                   want=ImageUtils.writePNG(bi);

              } catch (IOException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

              }

            }

           return want ;

          

       }

实现效果

 

总结:

润乾报表的可扩展性一直以来都起着非常重要的作用,通过二次开发,可以灵活实现很多特殊需求,只要通过java代码可以实现的需求,我们都可以集成到润乾报表中。

posted @ 2012-04-16 18:22  报表技术  阅读(3030)  评论(0编辑  收藏  举报