Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » BIRT » Displaying dynamic image in a report
Displaying dynamic image in a report [message #1186857] Thu, 14 November 2013 22:50 Go to next message
Laurent Le Moux is currently offline Laurent Le MouxFriend
Messages: 184
Registered: September 2011
Senior Member
Hi All,

I defined a report design to generate an open office document with a title and a dynamic image.
I found several posts on how to render an image read from a file by providing the corresponding code in the script image 'onRender' event.

importPackage(Packages.java.io);
importPackage(Packages.java.lang);
 
var file = new File("D:/mypath/test.png");
var ist = new FileInputStream(file);
var lengthi = file.length();
 
bytesa = new ByteArrayOutputStream( lengthi);
var c;
while((c=ist.read()) != -1){
 		bytesa.write(c);
}	  
ist.close();
this.data = bytesa.toByteArray();


It works fine but I now want to read my image from my RCP application and pass the corresponding byte array to 'onRender' as follow :

this.data = reportContext.getAppContext().get("Generated diagram image");


On the java side, I have :
...
        // Create task to run and render the report:
        final IRunAndRenderTask task = engine.createRunAndRenderTask(design);

        // Image example...
        File file = new File("D:/mypath/test.png");
        FileInputStream fis = null;
        try {
			fis = new FileInputStream(file);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        
        ByteArrayOutputStream bais = new ByteArrayOutputStream((int) file.length());
        int c = -1;
        try {
			while ((c=fis.read()) != -1)
				bais.write(c);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        
        try {
			fis.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        
        // Passing the generated diagram image as byte array to the report
        task.getAppContext().put("Generated diagram image", bais.toByteArray());
...


Unfortunately, the resulting open office document doesn't contain my test image anymore Sad.

Could somebody tell me what's wrong ?

Kind regards,

Laurent
Re: Displaying dynamic image in a report [message #1196865 is a reply to message #1186857] Tue, 19 November 2013 16:36 Go to previous messageGo to next message
Laurent Le Moux is currently offline Laurent Le MouxFriend
Messages: 184
Registered: September 2011
Senior Member
Hi all,

I added some traces and it appears that my byte array is correctly passed (same size on both RCP Application and Report script sides).
I'm a complete newbie with BIRT. I guess it's trivial but I'm really stucked.
Could somebody give me a clue ? Any help would be highly appreciated.

Regards,

Laurent
Re: Displaying dynamic image in a report [message #1198364 is a reply to message #1196865] Wed, 20 November 2013 09:30 Go to previous messageGo to next message
Laurent Le Moux is currently offline Laurent Le MouxFriend
Messages: 184
Registered: September 2011
Senior Member
Hi again,

I replaced my onRender script by the following java event handler which now gets invoked in my rptdesign file :
public class BIRTImageEventHandler extends ImageEventAdapter {
	@Override
	public void onRender( IImageInstance image, IReportContext reportContext )
			throws ScriptException
	{
		image.setData((byte[]) reportContext.getAppContext().get("image"));
	}
}


The problem remains. Loading the image file from inside the java event handler or overriding onCreate instead of onRender also fails...
Please find attached my rptdesign file if this can help figure out what's wrong...
Re: Displaying dynamic image in a report [message #1198805 is a reply to message #1198364] Wed, 20 November 2013 14:17 Go to previous messageGo to next message
donino donino is currently offline donino doninoFriend
Messages: 183
Registered: July 2011
Senior Member
Hi Laurent,

Firstly i think it is a better practice here to use the "onCreate" event rather "onRender". You mentionned you tried it as well, as you describe every step it should work but we don't see the java code section running the task.

With this line of code:
task.getAppContext().put("Generated diagram image", bais.toByteArray());

Keep in mind the byte Array is passed by reference, thus if your calling RCP application is clearing the inputstream before it is actually used in the report the image will be empty. It is just a suggestion, but i don't see any other reason why it would not work.

[Updated on: Wed, 20 November 2013 14:19]

Report message to a moderator

Re: Displaying dynamic image in a report [message #1200689 is a reply to message #1198805] Thu, 21 November 2013 11:06 Go to previous messageGo to next message
Laurent Le Moux is currently offline Laurent Le MouxFriend
Messages: 184
Registered: September 2011
Senior Member
Hi,

Thank you for your answer. I agree "onCreate" should be used instead of "onRender" but several posts on the Web indicate "onCreate" doesn't execute the provided examples properly whereas "onRender" does... And I encountered the problem while running my first test.

Concerning the byte array passed by reference, I tried to use an explicit copy but the problem is still there. Moreover the ByteArrayOutputStream javadoc is quite clear : toByteArray() creates a newly allocated byte array.

Please find hereafter, the code section running the task :
        // Passing the generated diagram image as byte array to the report
        task.getAppContext().put("image", bais.toByteArray());

        // Set Libre Office writer render option
        final RenderOption renderOption = new RenderOption();
        renderOption.setEmitterID("org.eclipse.birt.report.engine.emitter.odt");
        final String outputFilePath = ResourcesPlugin.getWorkspace().getRoot().getLocationURI().getPath()
        		+ "/" + d.getName() + ".odt";
        renderOption.setOutputFileName(outputFilePath);
        renderOption.setOutputFormat("odt");
        task.setRenderOption(renderOption);
        
        try {
			task.run();
		} catch (EngineException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        task.close();
        engine.destroy();
        Platform.shutdown();


In case you find something wrong...

Regards,
Re: Displaying dynamic image in a report [message #1201170 is a reply to message #1186857] Thu, 21 November 2013 16:10 Go to previous messageGo to next message
Laurent Le Moux is currently offline Laurent Le MouxFriend
Messages: 184
Registered: September 2011
Senior Member
Hi again,

I tried another solution. After reading my image from the file, I convert it as a Base64 String and pass it as report parameter.

        String imageAsString = Base64.encodeBase64URLSafeString(bais.toByteArray());
        task.setParameterValue("Image", imageAsString);


In the "onCreate" method (I tried also "onRender"), I simply decode the string back to the original image byte array.

importPackage(Packages.org.apache.commons.codec.binary);

this.data=Base64.decodeBase64(params["Image"]);


Adding some traces, it appears - once again - that the image datat is correctly passed.
But there's still nothing in the generated report.

Should I fill a request in Bugzilla ?
Re: Displaying dynamic image in a report [message #1203311 is a reply to message #1201170] Fri, 22 November 2013 15:44 Go to previous message
Laurent Le Moux is currently offline Laurent Le MouxFriend
Messages: 184
Registered: September 2011
Senior Member
Hi,

I finally found what the problem was...
In the report desgin, the image 'source' and 'Value expression' properties must be set to 'Expression' and an (even empty) string ('').
Now the image data is correctly processed and displayed.

Regards,

Laurent
Previous Topic:Passing parameter to BIRT in Eclipse 3.8.2
Next Topic:Defining a variable for use in aggregation filters
Goto Forum:
  


Current Time: Tue Mar 19 09:40:04 GMT 2024

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

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

Back to the top