How/Can I use base64 as image source in a Jasper Report template?(如何/我可以在 Jasper 报告模板中使用 base64 作为图像源吗?)
问题描述
所以在我的 jrxml 文件中,我有以下内容:
<参数名="smileyfaceimage" class="java.lang.String"/>
然后我引用它:
这不正确吗?
base64 有无我都试过了:
data:image/png;base64,
这是我正在使用的图像
然后我使用
重要提示:smileyfaceimage
需无:data:image/png;base64,
EDIT:OP(评论)的问题是,对于旧的 jasper 报告库(3.0),您需要在 imageExpression
中指定类 @see class="java.io.InputStream"
该帖子因此已更新,因为这也适用于 6.0.
So in my jrxml file I have the following:
<parameter name="smileyfaceimage" class="java.lang.String"/>
Then I reference it in:
<image scaleImage="FillFrame" onErrorType="Blank">
<reportElement x="167" y="62" width="363" height="171" backcolor="#333333"/>
<imageExpression class="java.lang.String"><![CDATA[$P{smileyfaceimage}]]></imageExpression>
</image>
Is this not correct?
I've tried the base64 both with and without:
data:image/png;base64,
Here's the image im working with
Then I used https://www.base64-image.de/ or any random site to get the base64 string. I tested the string it produces and it's valid.
Now in my code;
- set the value of a variable to the based64 string
- on the template
- set the parameter:
<parameter name="smileyfaceimage" class="java.lang.String"/>
- set the parameter:
then add the image data to the page:
<image scaleImage="FillFrame" onErrorType="Blank"> <reportElement x="167" y="62" width="363" height="171" backcolor="#333333"/> <imageExpression><![CDATA[new java.io.ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64($P{smileyfaceimage}.getBytes()))]]></imageExpression> </image>
Am I missing a step?
Passing parameter as String
makes jasper report believe its a absolute file path, so you need another class. The most obvious would be java.awt.Image
or java.io.InputStream
.
I choose java.io.InputStream
since this will require less code, so the first thing we need to do now is to decode
the base64
image String
.
There are several Base64 class that will do the job, I choose the org.apache.commons.codec.binary.Base64
since apache commons-codec.jar
is already distributed with jasper report (dependencies). The decode will give us a byte array byte[]
, so now we need only to add a ByteArrayInputStream
The java code would be:
InputStream stream = new ByteArrayInputStream(Base64.decodeBase64(smileyfaceimage.getBytes()));
Time to pass it into the jasper report imageExpression
<image scaleImage="FillFrame" onErrorType="Blank">
<reportElement x="167" y="62" width="363" height="171" backcolor="#333333"/>
<imageExpression class="java.io.InputStream"><![CDATA[new java.io.ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64($P{smileyfaceimage}.getBytes()))]]></imageExpression>
</image>
Hope for the best and press the preview:
Important notice: The smileyfaceimage
needs to be without:data:image/png;base64,
EDIT: The problem of the OP (comments) was that with old jasper report lib (3.0) you need to specify the class in the imageExpression
@see class="java.io.InputStream"
the post has been update consequently since this works also in 6.0.
这篇关于如何/我可以在 Jasper 报告模板中使用 base64 作为图像源吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何/我可以在 Jasper 报告模板中使用 base64 作为图像源吗?
基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01