12

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

just a random screenshot

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"/>
  • 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?

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
iCodeLikeImDrunk
  • 17,085
  • 35
  • 108
  • 169

5 Answers5

20

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:

Result

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.

Community
  • 1
  • 1
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
6

You need to decode the image somehow, e.g. use an imageExpression:

<image scaleImage="RetainShape" hAlign="Center" vAlign="Bottom" isUsingCache="false">
  <reportElement uuid="53a340b3-7d64-4104-9e9f-0f603059579a" key="Logo_Footer" x="55" y="760" width="370" height="42"/>
    <imageExpression><![CDATA[new java.io.StringBufferInputStream(new org.w3c.tools.codec.Base64Decoder(" Base 64 String Data ").processString())]]>
  </imageExpression>
</image>

I'm using this to embed images, but it should work with a variable, field or parameter too.

Durandal
  • 19,919
  • 4
  • 36
  • 70
2

Java 8+ without external libraries:

<imageExpression><![CDATA[javax.imageio.ImageIO.read(new java.io.ByteArrayInputStream(Base64.getDecoder().decode($P{barcodeHeader})))]]></imageExpression>

If that doesn't work, this should definitely:

<imageExpression><![CDATA[javax.imageio.ImageIO.read(new java.io.ByteArrayInputStream(java.util.Base64.getDecoder().decode($P{barcodeHeader})))]]></imageExpression>
Rok T.
  • 380
  • 5
  • 11
0

In my oracle DB, I have clob datatype for the base64 image, but Jaspersoft studio supports only string format, so I added image expressions as below, Image Expressions:

new java.io.ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64(new String($F{IMAGE_FRONT}).getBytes("UTF-8"))) 

Source:

  <image>
    <reportElement x="60" y="60" width="510" height="230" uuid="a06352ef-7ebf-4691-91a1-c8bc1371f90b">
    <property name="com.jaspersoft.studio.unit.x" value="pixel"/>
    </reportElement>
    <imageExpression>
    <![CDATA[new java.io.ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64(new String($F{CLOB_DATA_FIELD}).getBytes("UTF-8")))]]></imageExpression>
    </image>

Base64 is not defined issue is cleared after added jar file into MY REPORTS -> PROPERTIES -> JAVA BUILD PATH -> ADD EXTERNAL JAR ->jasperreports-6.1.0.jar The jar file downloaded from https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports/6.1.0

0

I tried the solutions above and didn't work for me I am currently using Jasper Reports Version 6.19.1 and found this Base64Util class at net.sf.jasperreports.util. You can look at it and check the method that would help solve your problem

<image evaluationTime="Report">
                <reportElement x="400" y="50" width="154" height="84" uuid="fa0e76f4-ffe9-4090-8a5c-68683b97ac38"/>
                <imageExpression><![CDATA[net.sf.jasperreports.util.Base64Util.decode($P{photo})]]></imageExpression>
            </image>

$P{photo} it is my variable that contains the base64 string

The decode method returns a byte[] array to your ImageExpression

public static byte[] decode(String data) throws IOException {
        ByteArrayInputStream input = new ByteArrayInputStream(data.getBytes(StandardCharsets.US_ASCII));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        decode(input, out);
        return out.toByteArray();
    }