代码之家  ›  专栏  ›  技术社区  ›  derOtterDieb

Jasper Report从字节[]插入图像

  •  0
  • derOtterDieb  · 技术社区  · 8 年前

    我搜索了一下,发现了一个非常相似的问题,不幸的是,这个问题不能解决我的问题。

    类似的问题: here

    我使用Jaspert报告6 .0和Java 1.8。

    我的目标是在报告中插入一个图像,我不能改变很多Java代码,并且图像被存储为字节[]。

    所以,我尝试过:

    <field name="logo" class="java.io.InputStream"/>
    // ... other stuff that is displayed properly
    <image scaleImage="FillFrame" onErrorType="Blank">
        <reportElement style="Column header" x="0" y="-1" width="80" height="75" backcolor="#333333" uuid="80bcba32-4e50-4a3a-949c-39e7c22ddff4"/>
        <imageExpression><![CDATA[new java.io.ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64($P{logo}.getBytes()))]]></imageExpression>
    </image>
    

    用这个Java代码:

    //a big bunch of fileds that I managed to display properly
    
    private InputStream logo;
    
    public Constructor(some, stuff, imageAsByteArray) {
        // setting lots of things that are displayed properly
    
        this.setLogo(new ByteArrayInputStream(Base64.decodeBase64(imageAsByteArray)));
    }
    

    但是,在Jasper Studio中,当我尝试保存jrxml文件时,出现了以下错误:

    GETByTeSe()的方法对于类型输入流值= new java. IO。ByteArrayInputStream(Or.ApACH.Cuff.Bial.Basic)是未定义的。

    我对Jasper不太熟悉,我尝试了几种不同的方式来插入图像,但我发现最接近的是我上面给出的链接。我知道我不能设置 class="java.io.InputStream" 再进去,是问题吗?

    有人知道我错过了什么吗?

    1 回复  |  直到 8 年前
        1
  •  1
  •   derOtterDieb    8 年前

    好吧,解决方案其实很简单,多亏了 @dada67

    首先,我混淆了$P和$F,因为我使用的是字段,所以必须使用$F。

    然后,解码base64是个错误,我不需要它。总而言之,正确的代码应该是:

    <field name="logo" class="java.io.InputStream"/>
    // ... other stuff that is displayed properly
    <image scaleImage="FillFrame" onErrorType="Blank">
        <reportElement style="Column header" x="0" y="-1" width="80" height="75" backcolor="#333333" uuid="80bcba32-4e50-4a3a-949c-39e7c22ddff4"/>
        <imageExpression><![CDATA[$F{logo}]]></imageExpression>
    </image>
    

    还有:

    //a big bunch of fileds that I managed to display properly
    
    private InputStream logo;
    
    public Constructor(some, stuff, imageAsByteArray) {
        // setting lots of things that are displayed properly
    
        this.setLogo(new ByteArrayInputStream(imageAsByteArray));
    }
    

    P.S:我会删除这篇文章,如果 @达达67 想公布他的答案,因为所有的学分都是他的。

    推荐文章