java - 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"
了,是问题吗?
有人会知道我在这里错过了什么吗?
解决方案
好的,感谢@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想发布他的答案,我会删除这篇文章,因为所有学分都是他的。
推荐阅读
- python - 从视频/图像中提取检测到的对象
- python - 如何通过 tkinter 在 Python 中检查多项选择(复选框)?
- python - 在python中循环并将数据写入csv文件
- c++ - 生产者消费者程序中的段错误
- sql-server - SQL Server:如何删除架构级联其所有对象?
- assembly - 汇编中的浮点异常 [NASM]
- flutter - 带有颤振块的滚动控制器
- ubuntu - 安装 Certbot XAMPP Ubuntu
- python - 在 vscode 中使用 python anaconda 作为我的解释器时无法安装格式化程序
- c# - IFormFile 在 ASP.NET Core 3.0 MVC 中始终为 NULL