How hard could it be to download an
image from a web cam and resize it via a scheduled Notes agent? Since Java
1.4 there is the Java
Image I/O package with all the
necessary classes. After some trials the agent works really fine. At least
with a Domino server running under Windows.
If the server is a Linux machine an
ugly error message is the output:
java.lang.NoClassDefFoundError: sun/awt/X11GraphicsEnvironment
After a little investigation on the
web the problem was found. Java Image I/O uses functions from the operating
system to download the image and resize it. If the Linux server was setup
with no graphical user interface these functions are not available.
The solution for this problem is JIMI.
Originally JIMI was developed to process images under Java 1.1.x. There
is a free download from Sun:
http://java.sun.com/products/jimi/
After the download the archive JimiProClasses.zip
has to added to the agent with "Edit Project".
The core of the agent has three tasks.
First download the image with the java.net
package to the local hard disk.
int pos = this.urlString.lastIndexOf("/");
String filename = this.urlString.substring(pos
+ 1);
URLConnection con = null;
URL url = new URL(urlString);
con = url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("file-name",
filename);
con.setRequestProperty("content-type",
"binary/data");
InputStream in = con.getInputStream();
int count;
byte[] buffer = new byte[8192]; // or more
...
while ((count = in.read(buffer)) > 0)
out.write(buffer,
0, count);
out.close();
out = null;
Then create an image object with the
JPEGImageDecoder.
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(new
FileInputStream(filename));
image = decoder.decodeAsBufferedImage();
Finally create with JIMI AreaAverageScaleFilter
and a JimiWriter a resized version on the local hard disk.
FilteredImageSource filteredImage = new FilteredImageSource
(image.getSource (), new AreaAverageScaleFilter (thumbWidth, thumbHeight));
JimiWriter writer = Jimi.createJimiWriter(filePath);
writer.setSource(filteredImage);
JPGOptions options = new JPGOptions();
options.setQuality(75);
writer.setOptions(options);
writer.putImage(filePath);
Because it is always easier to see a
working sample I created a little demo database.
JIMIDemo.zip