Il codice riportato qui sotto utilizza LS2J
per creare le immagini thumbnail, poiche' in LotusScript non esiste nativamente
alcun metodo per manipolare le immagini, ma in Java si. Con LS2J e' possibile
sfruttare la potenza di Java direttamente da LS.
In questo esempio viene utilizzato un agente richiamato
nella WebQuerySave di una FORM. L'agente e' scritto in LS, controlla se
il documento contiene file immagini ed in caso venga trovato, crea un file
thumbnail e lo allega al documento. E' possibile creare un agente per il
client Notes che fa esattamente la stessa cosa.
La parte piu' importante dell'agente e' la chiamata al
metodo che genera il file thumbnail. Questo metodo (thumbnailObject.ThumbnailThis
nell'esempio sotto riportato) riceve i seguenti parametri:
- Il nome del file sorgente sul quale verra' applicato il
filtro thumbnail.
- Il nome del file di uscita che si vorra' ottenere.
- La larghezza massima dell'immagine.
- L'altezza massima dell'immagine.
Nel
nostro esempio esso viene applicato in una FORM che ha un FIELD body
e un UPLOAD control per fare in modo che l'utente da WEB alleghi
un immagine. Nella WebQuerySave della FORM e' stato inserito il seguente
codice @Command([ToolsRunMacro]; "SaveWeb")
Nel database e' stato inserito un agente con il seguente
codice:
*****************************
'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
*****************************
Ora e' necessario creare la Java Library con il metodo
per generare la thumbnail.
Per fare cio' e' necessario andare Shared code->Script
Libraries, cliccare su New Java Library e sostituire il codice generato
con il seguente:
*****************************
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();
}
}
}
*****************************
Questo articolo e' stato tradotto da
SearchDomino |
0 Commenti:
Nessun Commento Trovato