首页 > 解决方案 > Jasper 报告从 byte[] 插入图像

问题描述

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

类似的问题:这里

我正在使用 Jaspert Report 6.6.0 和 Java 1.8。

我的目标是在报表中插入一张图片,我不能更改太多java代码,并且图片存储为byte[]。

所以,我尝试了这个:

<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 文件时,出现以下错误:

方法 getBytes() 未定义 InputStream value = new java.io.ByteArrayInputStream(org.apache.commons.codec.binary.Base

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

有人会知道我在这里错过了什么吗?

标签: javaarraysimagejasper-reports

解决方案


好的,感谢@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));
}

PS:如果@dada67想发布他的答案,我会删除这篇文章,因为所有学分都是他的。


推荐阅读