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?
You need to decode the image somehow, e.g. use an imageExpression:
I'm using this to embed images, but it should work with a variable, field or parameter too.
Passing parameter as
String
makes jasper report believe its a absolute file path, so you need another class. The most obvious would bejava.awt.Image
orjava.io.InputStream
.I choose
java.io.InputStream
since this will require less code, so the first thing we need to do now is todecode
thebase64
imageString
.There are several Base64 class that will do the job, I choose the
org.apache.commons.codec.binary.Base64
since apachecommons-codec.jar
is already distributed with jasper report (dependencies). The decode will give us a byte arraybyte[]
, so now we need only to add aByteArrayInputStream
The java code would be:
Time to pass it into the jasper report
imageExpression
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
@seeclass="java.io.InputStream"
the post has been update consequently since this works also in 6.0.