Jump to content
We've recently updated our Privacy Statement, available here ×

Base64 images from xml datasource


PixM

Recommended Posts

Hi,

 

We are working with complex jasperreports using xml as a datasource and we are including images. Currently we are using a parameter map for inserting the images, but we wish to use base64 images from the xml datasource.

 

Is there any chance that this functionality will be supported by jasper/ireport in the future?

Or is there a way to construct some sort of scriptlet so that the image field can be set to retrieve the xml data and convert it into an image object?

 

Tnx & Regards,

pixm

 

Link to comment
Share on other sites

  • Replies 6
  • Created
  • Last Reply

Top Posters In This Topic

T M
Wrote:

Hi,

 

We are working with complex jasperreports using xml as a datasource and we are including images. Currently we are using a parameter map for inserting the images, but we wish to use base64 images from the xml datasource.

 

Is there any chance that this functionality will be supported by jasper/ireport in the future?

Or is there a way to construct some sort of scriptlet so that the image field can be set to retrieve the xml data and convert it into an image object?

 

Tnx & Regards,

pixm

 

Well,

 

Found a solution to this:

Insert the following import directive in the report you want to use the images in:

- org.apache.commons.codec.binary.Base64

Next declare a variable (to be fancy)

new ByteArrayInputStream(new Base64().decodeBase64($F{image}.getBytes("UTF-8")))
 
This puts the string in a byte array, decodes it from base64 and puts it in an inputstream.

Where the $F{image} represents the value of the image tag in a base64 string.

Now use this variable in a picture object a inputstream and voila, image appear in your report.

 

Regards,

pixm

Link to comment
Share on other sites

  • 6 months later...
  • 1 year later...
  • 2 years later...

In regards to this;

I've been having trouble with adding multiple images encoded as base64, and have managed to work out that all image data must be loaded first before any reading is done, otherwise ireport simply shows a blank image. I've acheived this by ordering the jrxml to have all base64 data before any variable operations to load the data from it.

It seems quite straightforward now i think of it, but it may help someone hopefully.

Link to comment
Share on other sites

  • 1 year later...

For a purely static image (repeated throughout the report on say the title and page headers)

I declared a variable of type String and set it to the Base64 encoded image data.

I added the commons-codec-1.9.jar to the project's build path as an external jar.

Then for the image Expression - I set it to 

new ByteArrayInputStream(new org.apache.commons.codec.binary.Base64().decodeBase64($V{ImageVariable}.getBytes("UTF-8")))

Note that I had to fully qualify the apache commons Base64 class.

I set the evaluation time to 'Report' and 'Using Cache' to true.

 

 

 

 

 

 

Link to comment
Share on other sites

  • 5 years later...

If you want decode image from string.

First you should drag "Image" from Palette, then go to the properties of Image paste this expression  (new java.io.ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64($F{String}.getBytes()))) in the Image expression editor.

Note: In my case Field name is "$F{String}".

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...