Subject: JavaAgent Exception
I’ve done this and I’ve gotten it to work in a WebQuerySave Agent. I get the original code from here and worked off of that: http://www.openntf.org/Projects/codebin/codebin.nsf/0/90A96A34D110344386257035005FC39D
I’m trying now to make it work on more than just one image if the document has multiple image attachments. I think I’m stuck making multiple RT fields for the thumbs, one per image.
Let me know if this helps.
//ImageResizeToStream.java
import lotus.domino.Session;
import lotus.domino.Stream;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.ImageFormatException;
import java.awt.image.FilteredImageSource;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import com.sun.jimi.core.filters.AreaAverageScaleFilter;
import com.sun.jimi.core.Jimi;
import java.awt.Image;
//import gnu.regexp;
public class ImageResizeToStream
{
private final boolean DEBUG = true;
public Stream createResizedImage( Session session, InputStream is, int resizedWidth, int resizedHeight) throws Exception
{
try
{
// Get the new image into the decoder
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(is);
// and decode
Image image = decoder.decodeAsBufferedImage();
FilteredImageSource filteredImage = new FilteredImageSource (image.getSource(), new AreaAverageScaleFilter (resizedWidth, resizedHeight));
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Jimi.putImage( "image/jpeg", filteredImage, bout);
// convert bytestream to bytearray
byte [] imageByteArray = bout.toByteArray();
// Populate a NotesStream with the data
Stream stream = session.createStream();
stream.write (imageByteArray);
return stream;
}
catch ( ImageFormatException ife )
{
if ( DEBUG ) ife.printStackTrace();
}
catch ( Exception e )
{
e.printStackTrace();
}
return null;
}
}
//WQS_ImageUpload.java
import lotus.domino.*;
import java.io.PrintWriter;
import java.util.Vector;
public class WQS_ImageUpload extends AgentBase
{
private final boolean DEBUG = true;
private final int THUMBPHOTOWIDTH = 60;
private final int THUMBPHOTOHEIGHT = 60;
private final String RTFIELD = "PhotoThumb";
private final String IMAGENAMEFIELD = "ThumbImageName";
private final String HASTHUMBFIELD = "HasThumb";
private Session session;
private AgentContext agentContext;
private Database db;
private Document doc;
private EmbeddedObject obj;
private String serverName;
private String dbName;
private String docid;
private Stream thumbPhotoStream;
private MIMEEntity photoThumb;
private Vector fieldUpdates = new Vector();
public void NotesMain()
{
try
{
if ( DEBUG ) Utils.debug ( "|");
if ( DEBUG ) Utils.debug ( "/-------------------------------------------------" );
if ( DEBUG ) Utils.debug ( "imageUpload agent started");
if ( DEBUG ) Utils.debug ( "|");
// Get the in-memory document
session = getSession();
agentContext = session.getAgentContext();
db = agentContext.getCurrentDatabase();
doc = agentContext.getDocumentContext();
// Get the image file from the in-memory document
Item file = doc.getFirstItem("$FILE");
if (file != null)
{
String fileName = cleanStringBuffer(file.getValueString());
Vector v = null;
Session session = getSession();
final String fileexts = ".jpg .jpeg";
final String MACRO = "@Contains(\"" + fileName.toUpperCase() + "\"; @Explode(\"" + fileexts.toUpperCase() + "\";\" \"))";
if ( DEBUG ) Utils.debug(MACRO);
boolean hasJPEG = false;
try
{
v = session.evaluate(MACRO);
hasJPEG = (((Double)v.firstElement()).doubleValue() > 0) ? true : false;
}
catch(Exception e)
{
e.printStackTrace();
}
// This is work around because getting Body calling MIMEEntity.getMIMEEntity("Body") doesn't throw an error but
// doesn't write the image out. Could be something to do with headers but haven't investigated further
// doc.removeItem(RTFIELD);
if (hasJPEG)
{
obj = doc.getAttachment(fileName);
ImageResizeToStream irts = new ImageResizeToStream();
// Call the image sizing method of the image resize class here, pass it the obj and the width, should return domino.Stream
thumbPhotoStream = irts.createResizedImage( session, obj.getInputStream(), THUMBPHOTOWIDTH, THUMBPHOTOHEIGHT);
// This is work around because getting Body calling MIMEEntity.getMIMEEntity("Body") doesn't throw an error but
// doesn't write the image out. Could be something to do with headers but haven't investigated further
doc.removeItem(RTFIELD);
// Do not convert MIME to rich text
session.setConvertMIME(false);
// Create the mime entities and set their contents
photoThumb = doc.createMIMEEntity(RTFIELD);
photoThumb.setContentFromBytes( thumbPhotoStream , "image/jpeg", MIMEEntity.ENC_IDENTITY_BINARY );
// Close down the streams
thumbPhotoStream.close();
session.setConvertMIME(true);
// Create URL to redirect back to document
serverName = doc.getItemValueString("server_name");
dbName = db.getFileName();
fieldUpdates.addElement ( new String[] { IMAGENAMEFIELD, fileName } );
fieldUpdates.addElement ( new String[] { HASTHUMBFIELD, "Yes" } );
DocUtilities.updateDocument ( doc, fieldUpdates );
// doc.save();
// Remove the obj
// obj.remove();
}
else
{
if ( DEBUG ) Utils.debug("Photo is wrong type");
doc.removeItem(RTFIELD);
doc.removeItem(IMAGENAMEFIELD);
doc.removeItem(HASTHUMBFIELD);
doc.save();
}
}
else
{
if ( DEBUG ) Utils.debug("Photo not found");
doc.removeItem(RTFIELD);
doc.removeItem(IMAGENAMEFIELD);
doc.removeItem(HASTHUMBFIELD);
doc.save();
}
}
catch ( NotesException ne )
{
ne.printStackTrace();
}
catch( Exception e)
{
e.printStackTrace();
}
finally
{
if ( DEBUG ) Utils.debug ( "|");
if ( DEBUG ) Utils.debug ( "imageUpload agent finished");
if ( DEBUG ) Utils.debug ( "-------------------------------------------------/" );
if ( DEBUG ) Utils.debug ( "|");
}
}
private static String cleanStringBuffer(String target)
{
StringBuffer buf = new StringBuffer();
for(int n=0; n<target.length(); n++)
{
char c = target.charAt(n);
if ((byte)c == 0)
return buf.toString();
else
buf.append(c);
}
return buf.toString();
}
}