Auto Thumbnail Images using LotusScript in Notes/Domino R6

Mindwatering Incorporated

Author: Tripp W Black

Created: 07/05/2005 at 03:21 PM

 

Category:
Notes Developer Tips
LotusScript

Tip is originally from a SearchDomino posting which previously was contained in a picture database template on OpenNTF. (Min-Chi Huang)
The code below uses LS2J to create thumbnail images in LotusScript. LotusScript does not natively have any methods for manipulating images, but Java does. LS2J (which is a part of Notes/Domino on the Win32 platform) makes it possible to expose Java functionality to LotusScript.

This tip employs an agent that is called in the WebQuerySave event of a Domino form. The agent, written in LotusScript, checks the document for image files, and if any are found, creates thumbnail files for them and attaches them to the document. You could create an agent for the Notes client that does the same thing. The central part of the agent is the line that calls the method that creates the thumbnail file. This method (thumbnailObject.ThumbnailThis in the code) receives four parameters:

  • The filename of the image that is to be used as the source for the thumbnail file.
  • The filename that the thumbnail image should have.
  • The maximum width of the thumbnail image.
  • The maximum height of the thumbnail image.

I have a form that holds a Body-field and an upload control to allow Web users to attach image files. The form has the following in the WebQuerySave event: @Command([ToolsRunMacro]; "SaveWeb")

In the database I have an agent with the following code:
'Options
Option Public
Option Declare
Uselsx "*javacon" 'Which lets you use Java from Lotusscript
Use "ThumbNail" ' A Java library that holds a function to do Thumbnailing

'Initialize
Sub Initialize
Dim session As New NotesSession
Dim CurDB As NotesDatabase
Dim curDoc As NotesDocument
Dim fileList As Variant
Dim expression As String, serverName As String, thumbnailPrefix As String
Dim fileType As String, fileNameExcludingType As String
Dim sourceFilePath As String, thumbFilePath As String
Dim notesEmbeddedObject As NotesEmbeddedObject
Dim bodyItem As NotesRichTextItem

Dim js As JAVASESSION
Dim thumbnailClass As JAVACLASS
Dim thumbnailObject As JavaObject
Set js = New JAVASESSION
Set thumbnailClass = js.GetClass("ThumbNail")
Set thumbnailObject = thumbnailClass.CreateObject

Dim workingDir As String
Dim maxX As Integer, maxY As Integer
Dim returnCode As String
workingDir = "c:\temp\" 'Hard coding - you should change this
maxX = 100 'Maximum width of thumbnail in pixels
maxY = 100 'Maximum height of thumbnail in pixels
thumbnailPrefix = "t_" 'The prefix we will use for thumbnails

Set curDb = session.CurrentDatabase
servername = curDb.Server

On Error Goto ErrorHandling

expression = "@AttachmentNames"
Set curDoc = session.DocumentContext
Set bodyItem = curDoc.GetFirstItem("Body")
If curDoc.HasEmbedded Then
fileList = Evaluate(expression, curDoc) 'Contains an array of attachmentnames
Forall fileName In fileList
fileType = Lcase(Strrightback(Cstr(fileName), "."))
If (fileType = "jpeg" Or fileType = "jpg" Or fileType = "gif") And (Left$(fileName, 2) <> thumbnailPrefix)
Then 'The code only works with these image types, and we exclude old thumbnails
fileNameExcludingType = Strleft(fileName, ".")
Set notesEmbeddedObject = curDoc.GetAttachment( fileName ) 'We get a handle to the file that is to be the
source of a thumbnail
sourceFilePath = workingDir & fileName 'The file name of the file - on disk
thumbFilePath = workingDir & thumbnailPrefix & fileNameExcludingType & "." & fileType 'The file name for
the thumbnail
Call notesEmbeddedObject.ExtractFile(workingDir & fileName) 'Writing the source file to disk
returnCode = thumbnailObject.ThumbnailThis(sourceFilePath, ThumbFilePath , maxX, maxY) 'Calling the
thumbnailfunction
If returnCode = "OK" Then 'If thumbnail creation was OK
Set notesEmbeddedObject = bodyItem.EmbedObject(EMBED_ATTACHMENT, "",thumbFilePath) 'Attaching
the thumbnail
Call curDoc.Save(True, True, True) 'We only save if we modify the document
End If 'If returnCode = "OK" Then
Kill sourceFilePath 'Deleting temporary files
Kill thumbFilePath
End If 'If fileType = "jpeg" Or fileType = "jpg" Or fileType = "gif" Then
End Forall 'Forall fileName In fileList
End If 'If curDoc.HasEmbedded Then

Exit Sub

ErrorHandling:
Print " Error (" & Err & ") - line: " & Erl
Exit Sub
End Sub

Below is the Java Library that makes it possible to create the thumbnail – when we make the call for thumbnailObject.ThumbnailThis above.

You create a Java Library in the Designer in Shared code->Script Libraries, click New Java Library, and then remove the few lines of code that you get as a help for getting started, and paste the code below. Save and call the library "ThumbNail".
import com.sun.image.codec.jpeg.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;

public class ThumbNail {
public String thumbNailThis(String inputFilePath, String outputFilePath, int maxX, int maxY) {

try {
int thumbHeight;
int thumbWidth;

// load source image file
Image image = Toolkit.getDefaultToolkit().getImage(inputFilePath);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForAll();

int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);

double imageRatio = (double)imageWidth / (double)imageHeight;

if (imageRatio<1) {
thumbHeight = maxY;
thumbWidth = (int)(maxY*imageRatio);
} else {
thumbWidth = maxX;
thumbHeight = (int)(maxX/imageRatio);
}

resizeImage(image,outputFilePath,thumbWidth,thumbHeight,100);

} catch(Exception e) {
e.printStackTrace();
}

return "OK";
}

private void resizeImage(Image pImage, String pstFileName, int piWidth, int piHeight, int piQuality) {
try {
// draw original image to thumbnail image object and
// scale it to the new size on-the-fly
BufferedImage thumbImage = new BufferedImage(piWidth, piHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(pImage, 0, 0, piWidth, piHeight, null);

// save thumbnail image to OUTFILE
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(pstFileName));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);

param.setQuality((float)piQuality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);

out.close();
} catch(Exception e) {
e.printStackTrace();
}
}

}


previous page