I've saw several posts lately about problems linking to images from reports. I thought I'd share this tip:
I created a report that uses a couple of images to indicate the status of some automated services (running or not running) for a dashboard. I didn't want to mess with handling the images separate from the report, so I used this method to embed the images in the jrxml itself:
1) Base64 encode the image. I used the online utility at http://www.motobit.com/util/base64-decoder-encoder.asp Note that I have no ties to this site whatsoever, it's just something I found to be useful and it works.
2) In your report, create a variable of type String and set the variable expression to the Base64 encoded string from step 1
3) Put the image element in your report with class java.io.InputStream I also set scaling to retain shape.
4) For the image expression use:
new java.io.StringBufferInputStream( new org.w3c.tools.codec.Base64Decoder($V{MY_IMAGE}).processString() )
The Base64 decoder class is included with JasperServer and is already on the classpath. I tested this successfully with image formats GIF, JPEG, PNG, and BMP. (I ended up using PNG by the way)
I think it should also be possible to use class java.awt.Image (BufferedImage) created by ImageIO.load() from a byte array input stream, but this didn't work for me, even though using InputStream directly does.
Thanks,
Josh
2 Answers:
Hello.
I have to display object as image inside a list component in iReport based on a particular count(here using intNote ). I have tried this way...But no image is dispalyed...while using image path from specific location images are displayed...how can i dispaly object as images inside a list component ..please any one help me as soon as possible.......Thanks.........
<componentElement>
<reportElement x="130" y="72" width="25" height="35"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Horizontal" ignoreWidth="true">
<datasetRun subDataset="Q2">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource($F{intNote})]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="35" width="25">
<image isUsingCache="false">
<reportElement x="3" y="10" width="16" height="17" forecolor="#FFFFFF"/>
<graphicElement>
<pen lineWidth="1.25"/>
</graphicElement>
<imageExpression class="java.awt.Image"><![CDATA[$F{strVert}]]></imageExpression>
</image>
</jr:listContents>
</jr:list>
</componentElement>
This doesn't work now, if it worked ever before. It gives compilation error with base64 data in jrxml.
I just tried the original solution under Studio 6.2.0 and it worked fine. Have not tested on the report server yet.
I am using Studio 6.3.0final I am getting compile errors - Tried all the suggestions.
Is there anyone else who has the same issue?
This doesn't work in JR 6.0.3, even though it did in 5.6.1.
I tested additionally with this code:
**************************************************
public static void main(String[] args) {
System.out.println("Starting img test");
String b64img = "her goes image encoded in base64";
try (
java.io.InputStream is = new java.io.ByteArrayInputStream(new sun.misc.BASE64Decoder().decodeBuffer(b64img));
) {
java.awt.image.BufferedImage bi = javax.imageio.ImageIO.read(is);
System.out.println("Read image: "+bi);
} catch (Throwable t) {
System.err.println("Error: "+t);
}
}
**************************************************
And it works fine.
I have Java 8 x86_64 running on Windows 7 x86_64.