2

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 ?

Alex K
  • 22,315
  • 19
  • 108
  • 236
derOtterDieb
  • 545
  • 4
  • 12
  • 35
  • What's the type of imageAsByteArray? If you have the image as byte array or input stream, you should directly use it as image expression. You don't need to do any Base64 encoding or decoding. – dada67 Jul 27 '18 at 11:14
  • The imagesAsByArray comes from a get() method on this field from another class : ```private byte[] logoContent;```. Do you mean I should just use ```$P{logo}``` ? – derOtterDieb Jul 27 '18 at 11:33
  • 1
    Your reports lists "logo" as a field, so you should use $F{logo}. – dada67 Jul 27 '18 at 11:46
  • That also. I did what you asked about the Base64 decoding, works fine now. Thanks. Do you want to post your answer or should I do it ? – derOtterDieb Jul 27 '18 at 11:59
  • See also: https://stackoverflow.com/a/39320863/59087 – Dave Jarvis Jul 27 '18 at 16:39

1 Answers1

6

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.

derOtterDieb
  • 545
  • 4
  • 12
  • 35