derOtterDieb
derOtterDieb

Reputation: 555

Jasper report insert image from byte[]

I searched a bit, and found a very similar question that unfortunatly doesn't solve my problem.

The similar question : here

I'm using Jaspert Report 6.6.0 and Java 1.8.

My goal is to insert an image in the report, I can't change much of java code, and the image is stored as byte[].

So, I tried this :

<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[new java.io.ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64($P{logo}.getBytes()))]]></imageExpression>
</image>

With this Java code :

//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(Base64.decodeBase64(imageAsByteArray)));
}

But, in Jasper Studio, when I try to save my jrxml file, I have this error :

The method getBytes() is undefined for the type InputStream value = new java.io.ByteArrayInputStream(org.apache.commons.codec.binary.Base

I'm not very familiar with Jasper, and I tried few different ways to insert the image, but the closest thing I found is the link I gave above. I understood that I can't set class="java.io.InputStream" anymore in , is it the problem ?

Anyone would know what I missed here ?

Upvotes: 2

Views: 10822

Answers (1)

derOtterDieb
derOtterDieb

Reputation: 555

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.

Upvotes: 7

Related Questions