Jump to content
Changes to the Jaspersoft community edition download ×

Image as Base64 text


Recommended Posts

By: J rn Heid - jheid

Image as Base64 text

2005-05-31 01:55

Hello.

 

I have a feature request.

It would be nice if I can place an base64 encoded image just inside the .jrxml file.

 

I have had the following problem (perhaps there's a way to solve this). I've created a report which uses an image (same directory, so I just added "image.gif" as imageExpression). I have to upload this report to a webserver where my program compiles and fills it. Well, as "image.gif" is just transformed to new File("image.gif") it won't find it. There should be a way to set the base-dir for a report (so that external resources can be found (next thing I have to solve is a subreport...)). A system property wouldn't be very nice as a webserver might serve different applications... I'm thinking of something like the ResourceResolver in XML....

 

What do you think?

 

 

Regards,

JOERN_HEID

 

 

By: Franck Andriano - altern

RE: Image as Base64 text

2005-06-02 02:25

Hi!

 

I moved the problem with the resource (Xml or DB resource give images)

I've images embeded in xml encoded in base64 that work's! :)

 

public class MyImage

{

public MyImage()

{

}

 

public static final byte[] loadBinaryImage(String sBinary)

{

if (sBinary == null || "".equals(sBinary)) return null;

 

try

{

return Base64Coder.decode(sBinary.toCharArray());

}

catch (Exception e)

{

System.out.println("image null : "+e);

return null;

}

}

}

 

<imageExpression class="java.awt.Image"><![CDATA[net.sf.jasperreports.engine.util.JRImageLoader.loadImage(MyImage.loadBinaryImage($F{ItemImage}))]]></imageExpression>

 

url Base64Coder.java :

http://www.source-code.biz/snippets/java/Base64Coder.java.txt

 

Best regards,

 

/F.A

 

 

By: J rn Heid - jheid

RE: Image as Base64 text

2005-06-02 02:32

Thanks for the solution.

 

Perhaps the JRImageLoader could have a base64 loading method in the future (so hopefully iReport will support that ;).

 

JOERN

Link to comment
Share on other sites

  • 8 years later...
  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

I want to post my solution, because this answered question is dated, but it is the first hit on google if you search for "jaspersoft base64 image".

With Jaspersoft 5.6.1 this works:

Assuming that you have a String field within your JSON file (Datasource) named "image" add the field:

<field name="image" class="java.lang.String">  <fieldDescription><![CDATA[image]]></fieldDescription> </field>[/code]

Just embed your image wherever you want:

<imageExpression><![CDATA[net.sf.jasperreports.engine.util.JRImageLoader.getInstance(new SimpleJasperReportsContext()).loadAwtImageFromBytes(javax.xml.bind.DatatypeConverter.parseBase64Binary($F{image}))]]></imageExpression>[/code]

Note that the image string within the JSON file has to be plain BASE64 code, omit any Data-URL header data

"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAho..." is wrong.

"iVBORw0KGgoAAAANSUhEUgAAAho..." is the base64 code and works.

 

 

Link to comment
Share on other sites

  • 3 years later...

<imageExpression><![CDATA[new ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64($F{logo}.getBytes()))]]></imageExpression>


and yes, the image should be just content without preceding meta info: (as the previous commentator pointed out)
 

"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAho..." is wrong.

"iVBORw0KGgoAAAANSUhEUgAAAho..." is the base64 code and works.


 

 

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...