java 我如何/可以在 Jasper 报告模板中使用 base64 作为图像源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33571520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How/Can I use base64 as image source in a Jasper Report template?
提问by iCodeLikeImDrunk
So in my jrxml file I have the following:
所以在我的 jrxml 文件中,我有以下内容:
<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:
我已经尝试过使用和不使用 base64:
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.
然后我使用https://www.base64-image.de/或任何随机站点来获取 base64 字符串。我测试了它产生的字符串,它是有效的。
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>
- 将变量的值设置为 based64 字符串
- 在模板上
- 设置参数:
<parameter name="smileyfaceimage" class="java.lang.String"/>
- 设置参数:
然后将图像数据添加到页面:
<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?
我错过了一步吗?
回答by Petter Friberg
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
.
传递参数 asString
使 jasper report 相信它是绝对文件路径,因此您需要另一个类。最明显的是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
.
我选择java.io.InputStream
,因为这将需要更少的代码,所以我们现在需要做的第一件事就是到decode
了base64
图像String
。
There are severalBase64 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
有几个Base64 类可以完成这项工作,我选择了org.apache.commons.codec.binary.Base64
因为 apachecommons-codec.jar
已经与 jasper 报告(依赖项)一起分发。解码会给我们一个字节数组byte[]
,所以现在我们只需要添加一个ByteArrayInputStream
The java code would be:
Java代码将是:
InputStream stream = new ByteArrayInputStream(Base64.decodeBase64(smileyfaceimage.getBytes()));
Time to pass it into the jasper report imageExpression
是时候将它传递到 jasper 报告中了 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,
重要通知:smileyfaceimage
需要没有: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.
编辑:OP(评论)的问题是,对于旧的 jasper 报告库(3.0),您需要在imageExpression
@see 中指定类,因此class="java.io.InputStream"
该帖子已更新,因为这也适用于 6.0。
回答by Durandal
You need to decode the image somehow, e.g. use an imageExpression:
您需要以某种方式解码图像,例如使用 imageExpression:
<image scaleImage="RetainShape" hAlign="Center" vAlign="Bottom" isUsingCache="false">
<reportElement uuid="53a340b3-7d64-4104-9e9f-0f603059579a" key="Logo_Footer" x="55" y="760" width="370" height="42"/>
<imageExpression><![CDATA[new java.io.StringBufferInputStream(new org.w3c.tools.codec.Base64Decoder(" Base 64 String Data ").processString())]]>
</imageExpression>
</image>
I'm using this to embed images, but it should work with a variable, field or parameter too.
我正在使用它来嵌入图像,但它也应该与变量、字段或参数一起使用。
回答by Rok T.
Java 8+ without external libraries:
没有外部库的 Java 8+:
<imageExpression><![CDATA[javax.imageio.ImageIO.read(new java.io.ByteArrayInputStream(Base64.getDecoder().decode($P{barcodeHeader})))]]></imageExpression>
If that doesn't work, this should definitely:
如果这不起作用,这绝对应该:
<imageExpression><![CDATA[javax.imageio.ImageIO.read(new java.io.ByteArrayInputStream(java.util.Base64.getDecoder().decode($P{barcodeHeader})))]]></imageExpression>