Does anyone know how to make a Java agent write image data directly to a browser?
I’ve been trying to read an image file (stored in the server’s file system) into a byte array, then write that array to the agent output. The problem with this is that the agent output is a PrintWriter, that class doesn’t accept byte arrays, and I can’t figure out how to convert the data in such a way that it will appear as an image in the browser.
The ultimate purpose of this is to be able to call an agent via a URL, have the agent read from Google’s Chart API, convert the PNG image to a different format (JPEG or GIF), then display the converted image. This is needed so we can display Google charts using pass-through HTML in the Notes client (as Notes doesn’t support PNG).
A simple URL redirection to the image won’t work because, to support multiple users, the agent needs to create a new unique filename each time it runs.
Subject: Thanks for the responses…
I would have preferred being able to output a data-stream directly from an agent, but it appears that’s impossible.
I’m going with Simon’s solution. It sounds simpler, and also seems to work based on my testing so far.
Dave: there’s no need for command-line utilities to convert the file format. A java agent can use the ImageIO class to do such conversions.
Subject: Java agent writing image data to browser?
I have succeeded in a similar job when I designed a Java agent that creates and displays a CAPTCHA. What I do is generate an image in the agent. Then save it to the local file system. Then attach it to a temporary document. Finally, display it to the user. Each version of the CAPTCHA is unique.
Once I’ve finished with the document, I delete it.
As far as I know, you can’t send a stream in the way you seem to describe.
Subject: Java agent writing image data to browser?
Scott,
This is easier for a web app than a Notes client app, but theoretically possible on both.
The web app is easy, just get the image stream from Google and save it to a uniquely named file (use the user name + a random number) where the web server can see it. Then just reference the file in an
tag. You do not have to bother with the format conversion because any modern web browser supports PNG files.
The Notes client app is much more difficult because you have to use MIME. The first part of the process is the same as the web app - get the PNG file onto the local file system. Then you have to convert the image to JPG format so that you can import it to Notes. You will have to resort to command line utilities such as ImageMagick to do this. The rest of the process is:
-
Create a rich text item on your document
-
Call CreateMimeEntity to create somwehere to put the image
-
Call the SetContentFromBytes method on the MIME entity to fill 'er up with your jpg file.
NOTE: This is all theory - I have not actually done this so I am not making any kind of guarantees that this will work - but it should work.