Anonymous
Domino 2.0 Rich Internet Applications with IBM Lotus Notes/Domino
You are here: Today » Domino picture resizer
« Domino RSS feeds
DC 2 - Working with page templates and stylesheets »

Domino picture resizer

I found this code purely by accident in one of the databases we developed for a customer. Thank you, Peter, my collegue who wrote the Java code for it. I've made a LotusScript class wrapper around the code for more flexible use. The temporary directory and the formats should be retrieved from a setup document (which is not done in this POC).

The Java code

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();
}
}
}

The ImageResizer class (LotusScript)

Class ImageResizer
    doc As notesdocument
    bodyItem As NotesRichTextItem
    notesEmbeddedObject As NotesEmbeddedObject
    sDir As String
    sSourceImg As String
    js As JAVASESSION
    imgClass As JAVACLASS
    imgObject As JavaObject
    toDelete List As String
    
    Sub New(contextDoc As NotesDocument, Byval sTempDir As String)
        Set doc=contextDoc
        Set bodyItem=doc.GetFirstItem("Body")
        sDir=sTempDir
        Set js=New JAVASESSION
        Set imgClass=js.GetClass("ThumbNail")
        Set imgObject = imgClass.CreateObject
    End Sub
    
    Sub setSource(Byval sFileName As String)
        Set notesEmbeddedObject=doc.GetAttachment(sFileName)
        Call notesEmbeddedObject.ExtractFile(sDir & sFileName)
        toDelete(sSourceImg)=sDir & sFileName
        sSourceImg=sFileName
    End Sub
    
    Sub resize(Byval sPrefix As String, maxX As Integer, maxY As Integer)
        Dim sReturn As String, sNewPath As String
        
        sNewPath=sDir & sPrefix & sSourceImg
        sReturn=imgObject.ThumbnailThis(sDir & sSourceImg,sNewPath,maxX,maxY)
        If sReturn="OK" Then
            Set notesEmbeddedObject=bodyItem.EmbedObject(EMBED_ATTACHMENT,"",sNewPath)
            toDelete(sNewPath)=sNewPath
        End If
    End Sub
    
    Sub close
        Call doc.Save(True,False,True)
        Forall v In toDelete
            Kill v
        End Forall
    End Sub
End Class

The QuerySave agent

Sub Initialize
    On Error Goto catch
    Dim s As New NotesSession
    Dim doc As NotesDocument
    Dim configDoc As NotesDocument
    Dim fileList As Variant
    Dim resizer As ImageResizer
    
    Set doc=s.DocumentContext
    If doc.HasEmbedded Then
        Set resizer=New ImageResizer(doc, "c:\temp\")
        fileList=Evaluate("@AttachmentNames", doc)
        Forall sFile In fileList
            resizer.setSource(sFile)
            resizer.resize "t-", 150, 150
            resizer.resize "s-", 300, 300
            resizer.resize "m-", 500, 400
        End Forall
        resizer.close
    End If
    
    Print |[| & doc.wPath(0) & |vAll/| & doc.UniversalID & |]|
    
    Goto finally
catch:
    Print " Error (" & Err & ") " & Error & " - line: " & Erl
    Resume finally
finally:
End Sub

Please note

... that the temporary directory has to be created first and the agent signer has to have access to the temporary directory.

Download

image-resize.nsf (2560 kB)

Star rating

94%

Comments

To add a comment, log in or register as new user. It's free and safe.