Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » BIRT » Newbie: Embed Image from URL(How to add an Embedded Image to a report )
Newbie: Embed Image from URL [message #686039] Tue, 21 June 2011 13:22 Go to next message
endiBirt  is currently offline endiBirt Friend
Messages: 13
Registered: June 2011
Junior Member
Hi all,

does anybody know a solution for this:

I try to embed an Image to a report in beforeFactory. I found that snippet via google that seems to do the trick:

//BEGIN####################################################################
var myurl = new Packages.java.net.URL("http://mysrv/mypictures/pic1.gif");
var img = ImageIO.read(myurl);
bas = new ByteArrayOutputStream();
ImageIO.write(img, "gif", bas);

image = StructureFactory.createEmbeddedImage( );
image.setType( DesignChoiceConstants.IMAGE_TYPE_IMAGE_GIF );
image.setData( bas.toByteArray());
image.setName( "Attachment" + counter2 );
reportDesignHandle.addImage( image );
//END####################################################################

The report runs without errors but the image is not visible. The webserver sends a status 200.

Thanks for any hint in advance.


Regards

Andreas
Re: Newbie: Embed Image from URL [message #686041 is a reply to message #686039] Tue, 21 June 2011 13:37 Go to previous messageGo to next message
endiBirt  is currently offline endiBirt Friend
Messages: 13
Registered: June 2011
Junior Member
Ah, stupid. Found it out:

//BEGIN ##################################################################
var myurl = new Packages.java.net.URL("http://mysrv/mypictures/pic1.gif");
var img = ImageIO.read(myurl);
bas = new ByteArrayOutputStream();
ImageIO.write(img, "gif", bas);

image = StructureFactory.createEmbeddedImage( );
image.setType( DesignChoiceConstants.IMAGE_TYPE_IMAGE_GIF );
image.setData( bas.toByteArray());
image.setName( "Attachment" + counter2 );
reportDesignHandle.addImage( image );

imageHandle = elementFactory.newImage("handle");
imageHandle.setSource( DesignChoiceConstants.IMAGE_REF_TYPE_EMBED);
imageHandle.setImageName("Attachment" + counter2);
reportDesignHandle.getBody().add(imageHandle);
//END##################################################################

Thanks for your attention.


Regards

Andreas

Re: Newbie: Embed Image from URL [message #686043 is a reply to message #686039] Tue, 21 June 2011 13:50 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Andreas,

You are adding the embedded image but you also have to add a image report item.

importPackage(Packages.javax.imageio);
importPackage(Packages.java.io);
importPackage(Packages.java.net);
importPackage(Packages.org.eclipse.birt.report.model.api);
importPackage(Packages.org.eclipse.birt.report.model.api.elements);

var myurl = URL("http://tomcat.apache.org/images/tomcat.gif");
var img = ImageIO.read(myurl);
bas = new ByteArrayOutputStream();
ImageIO.write(img, "gif", bas);

image = StructureFactory.createEmbeddedImage( );
image.setType( DesignChoiceConstants.IMAGE_TYPE_IMAGE_GIF );
image.setData( bas.toByteArray());
image.setName( "AttachmentTomcat" );
reportContext.getDesignHandle().addImage( image );
var elmfactory = reportContext.getDesignHandle().getElementFactory();

var image1 = elmfactory.newImage( "mylogo" );
image1.setImageName( "AttachmentTomcat" );
reportContext.getDesignHandle().getBody().add(image1);

Any reason you are not just using a url image?

Jason
Re: Newbie: Embed Image from URL [message #686044 is a reply to message #686043] Tue, 21 June 2011 13:55 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

You may want to read over this blog post:http://birtworld.blogspot.com/2010/09/birt-image-report-item.html

Jason
Re: Newbie: Embed Image from URL [message #686046 is a reply to message #686043] Tue, 21 June 2011 13:56 Go to previous messageGo to next message
endiBirt  is currently offline endiBirt Friend
Messages: 13
Registered: June 2011
Junior Member
Hi Jason,

thank you for your hint.

I want to use the report for html-email. The pictures are not visible from the outside, so I think I must embed them in the report.

Andreas
Re: Newbie: Embed Image from URL [message #686049 is a reply to message #686044] Tue, 21 June 2011 14:01 Go to previous messageGo to next message
endiBirt  is currently offline endiBirt Friend
Messages: 13
Registered: June 2011
Junior Member
Hi Jason,

thanks for the link. This is a wonderfull article. I have bookmarked it. From there I have the part with the ImageIO.read(myurl)...

Thank you very much.

Andreas
Re: Newbie: Embed Image from URL [message #686062 is a reply to message #686049] Tue, 21 June 2011 14:43 Go to previous messageGo to next message
endiBirt  is currently offline endiBirt Friend
Messages: 13
Registered: June 2011
Junior Member
Hi Jason,

bad news. Embed doesn't work in the way I thought. The html source shows:

<img id="AUTOGENBOOKMARK_22" src="/birt-viewer/preview?__sessionId=20110621_160446_777&amp;__imageid=design26d60713097b956201.gif" alt="" style="display: block;"></img>

I wanted something like:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />

Uhhh. So I must search further. Sad


Andreas
Re: Newbie: Embed Image from URL [message #686073 is a reply to message #686062] Tue, 21 June 2011 15:27 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

I posted an example of how to do this on birt-exchange:
http://www.birt-exchange.org/org/devshare/designing-birt-reports/1377-inline-base64-image-in-html-output/

It should allow you to do what you want.

Jason
Re: Newbie: Embed Image from URL [message #686517 is a reply to message #686073] Wed, 22 June 2011 12:06 Go to previous messageGo to next message
endiBirt  is currently offline endiBirt Friend
Messages: 13
Registered: June 2011
Junior Member
Hi Jason,

thank you very much for the link. This is exactly what I was searching for. I tried the script and it worked like a charm. Then I tried to place the script in a while loop because I have to embed two and more pictures. And here it embeds only the last picture two and more time. So you have three different pictures to embed and there are three pictures but everytime the same picture( the last one I embedded).

How can this be?

The Links for the ImageIO are OK. I think there is something with ImageIO. Please help.

Thanks for any hint in advance.


Regards

Andreas

PS: Here is my script...

importPackage(Packages.org.eclipse.birt.report.model.api);
importPackage(Packages.org.eclipse.birt.report.model.api.elements);
importPackage(Packages.java.io);
importPackage(Packages.java.lang);
importPackage(Packages.java.net);
importPackage(Packages.javax.imageio);
importPackage(Packages.org.apache.commons.codec.binary)

// call webservice - former open of dataset
counter = 0;
counter2 = 0;

paramNotesID = params["notesID"];
paramResponseType = params["previewType"];

pPreviewWSCaller = new Packages.de.makeit.notes.cvag.kma_prtpreview.java.data.pPreviewWSCaller();
responseWerte = pPreviewWSCaller.getData(paramNotesID, paramResponseType);

// end

var list = new Array(responseWerte.size());
var streamList = new Array(responseWerte.size());

if(counter2<responseWerte.size())
{

sachverhalt = responseWerte.get(counter).getSachverhalt();
reportDesignHandle = reportContext.getDesignHandle();
elementFactory = reportDesignHandle.getElementFactory();

while(counter2 < sachverhalt.length)
{

var myurl = URL("http://mysrv" + sachverhalt[counter2].getURI());
var img = ImageIO.read(myurl);

streamList[counter2] = new ByteArrayOutputStream();
ImageIO.write(img, "gif", streamList[counter]);
streamList[counter2].flush();

encodedimga = Base64.encodeBase64(streamList[counter2].toByteArray());
encodedimg = new String( encodedimga, "utf-8");
streamList[counter2].close();
imgsrc = "data:image/gif;base64,"+encodedimg+"\"";

list[counter2] = elementFactory.newTextItem(null);
list[counter2].setContentType(DesignChoiceConstants.TEXT_CONTENT_TYPE_HTML);
list[counter2].setContent("<img src=<VALUE-OF>imgsrc</VALUE-OF> +\" alt=\"attachedPic" + sachverhalt[counter2].getLink() + "\" />");
reportDesignHandle.getBody().add(list[counter2]);

counter2++
}
}
Re: Newbie: Embed Image from URL [message #686623 is a reply to message #686517] Wed, 22 June 2011 15:56 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

The issue is the imgsrc value of tag is only evaluated when the text item is created not in the middle of the beforeFactory. Look at doing something like the following:

importPackage(Packages.javax.imageio);
importPackage(Packages.java.io);
importPackage(Packages.java.net);
importPackage(Packages.java.lang);
importPackage(Packages.org.apache.commons.codec.binary)
importPackage(Packages.java.lang);
importPackage(Packages.org.eclipse.birt.report.model.api);
importPackage(Packages.org.eclipse.birt.report.model.api.elements);


var myurl0 = "http://tomcat.apache.org/images/tomcat.gif";
var myurl1 = "http://www.google.com/intl/en_ALL/images/srpr/logo1w.png";
var myurl2 = "http://www.eclipse.org/eclipse.org-common/themes/Nova/images/eclipse.png";



reportDesignHandle = reportContext.getDesignHandle();
elementFactory = reportDesignHandle.getElementFactory();
for( i=0; i< 3; i++){
var urlstr = "";
if( i == 0){
urlstr = myurl0;
}else if( i == 1 ){
urlstr = myurl1;
}else if( i == 2 ){
urlstr = myurl2;
}
var nurl = new URL( urlstr );
var img = ImageIO.read(nurl);
var bas = new ByteArrayOutputStream();
ImageIO.write(img, "png", bas);
bas.flush();

var encodedimga = Base64.encodeBase64(bas.toByteArray());
var encodedimg = new String( encodedimga, "utf-8");
bas.close();
var imgsrc = "data:image/gif;base64,"+encodedimg+"\"";


var nText = elementFactory.newTextItem(null);
nText.setContentType(DesignChoiceConstants.TEXT_CONTENT_TYPE_HTML);
nText.setContent("<img src="+ imgsrc + " />");
reportDesignHandle.getBody().add(nText);
}

Jason
Re: Newbie: Embed Image from URL [message #687952 is a reply to message #686623] Thu, 23 June 2011 15:08 Go to previous message
endiBirt  is currently offline endiBirt Friend
Messages: 13
Registered: June 2011
Junior Member
Hello Jason,

thank you very much for your help. This solved the problem. Again, thank you very much.

Andreas
Previous Topic:how to set pie chart identifier font
Next Topic:Data Set XPath Expression doesn't work
Goto Forum:
  


Current Time: Fri Mar 29 15:17:10 GMT 2024

Powered by FUDForum. Page generated in 0.02851 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top