Vous êtes sur la page 1sur 5

Creating thumbnail images using LS2J in LotusScript

1 of 5

http://searchdomino.techtarget.com/tip/Creating-thumbnail-images-usi...

23-09-2014 18:20

Creating thumbnail images using LS2J in LotusScript

http://searchdomino.techtarget.com/tip/Creating-thumbnail-images-usi...

'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
Dim
Dim
Set
Set
Set

js As JAVASESSION
thumbnailClass As JAVACLASS
thumbnailObject As JavaObject
js = New JAVASESSION
thumbnailClass = js.GetClass("ThumbNail")
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

2 of 5

23-09-2014 18:20

Creating thumbnail images using LS2J in LotusScript

http://searchdomino.techtarget.com/tip/Creating-thumbnail-images-usi...

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

import
import
import
import
import

com.sun.image.codec.jpeg.*;
java.awt.*;
java.awt.image.*;
java.io.*;
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();
}
}
}

3 of 5

23-09-2014 18:20

Creating thumbnail images using LS2J in LotusScript

4 of 5

http://searchdomino.techtarget.com/tip/Creating-thumbnail-images-usi...

23-09-2014 18:20

Creating thumbnail images using LS2J in LotusScript

5 of 5

http://searchdomino.techtarget.com/tip/Creating-thumbnail-images-usi...

23-09-2014 18:20

Vous aimerez peut-être aussi