Jasper report insert image from byte[]
Ok, solution was actually very simple, thanks to @dada67.
First, I confused $P and $F, as I was using a Field, I had to use $F.
Then, decoding base64 was a mistake, I didn't need it. To sum it up, right code should be :
<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>
And :
//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));
}
P.S : I'll remove this post if @dada67 wants to post his answer, since all credits are his.
JasperReports - Embedding chart as SVG into HTML report
What you need is the following two properties in your jasperreports.properties (if you don't already have that file, create it and put it to a jar or folder that's on the application classpath):
net.sf.jasperreports.chart.render.type=svg
net.sf.jasperreports.export.html.embed.image=true
How to show an image on jasper report?
Here was the problem:
As I said previously I have in the same directory the .jrxml, the logo.jpg and the .java that uses the .jrxml.
The thing is that the fileResolver
FileResolver fileResolver = new FileResolver() {
@Override
public File resolveFile(String fileName) {
return new File(fileName);
}
};
didn't returned the image file. I found out it mapped on to a different directory and not the one I was expecting. So, I changed it to:
FileResolver fileResolver = new FileResolver() {
@Override
public File resolveFile(String fileName) {
URI uri;
try {
uri = new URI(this.getClass().getResource(fileName).getPath());
return new File(uri.getPath());
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
};
And that worked out. I forgot the fact that:
A relative pathname, in contrast,
must be interpreted in terms of
information taken from some other
pathname. By default the classes in
the java.io package always resolve
relative pathnames against the current
user directory. This directory is
named by the system property user.dir,
and is typically the directory in
which the Java virtual machine was
invoked.
(taken from the java api - File (Java Platform SE 6))
The directory in which the JVM is invoked is not the one I have all this data.
Thanks!
How/Can I use base64 as image source in a Jasper Report template?
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.
Related Topics
How to Make My Swingworker Example Work Properly
What Is the Class Object (Java.Lang.Class)
How to Change an Eclipse Default Project into a Java Project
How to Do Union, Intersect, Difference and Reverse Data in Java
Replace All Occurrences of a String Using Stringbuilder
Using a Jfilechooser with Swing Gui Classes and Listeners
Selenium Ie Webdriver Only Works While Debugging
Should I Use String.Isempty() or "".Equals(String)
How to Write a Basic Swap Function in Java
Immutable VS Unmodifiable Collection
How to Combine Two Hashmap Objects Containing the Same Types
Java:Parse Java Source Code, Extract Methods
Sending Mail Attachment Using Java
How to Have a Jtabbedpane with a Jmenubar
Generic Return Type Upper Bound - Interface VS. Class - Surprisingly Valid Code