Resize Image with Lotus Script and Java on Lotus Notes
Wer in Lotus Notes Anwendungen entwickelt ist sicherlich auch bereits über die Problematik mit Bilddateien gestolpert. Wer per Code ein Bild in bestimmter grösse einfügen will muss dies normalerweise über ein Addon DLL erledigen. Da meine Anwendung jedoch ohne Zusatzsoftware auskommen soll habe ich mich entschieden eine entsprechende Java/Lotus Script Klasse zu schreiben.
1. Schritt: Lotus Script Bibliotheke „LSJavaImageResizer“ erstellen
Nachfolgend der Code für die Lotus Script Klasse:
[sourcecode language=“plain“ collapse=“true“ title=“Lotus Script Class 'LSJavaImageResizer'“]
%REM
Code and Library written by Yves Menge (yves.menge@gmail.com)
For updates / news visit http://blog.y-m-e.net
09.08.2011
%END REM
Option Public
Option Declare
UseLSX "*javacon"
Use "JavaLibrary"
%REM
Sample Code for use in Lotus Script:
***
Use "LSJavaImageResizer"
Dim lsImageResizer As New LSJavaImageResizer([PathToSourceImage], [PathToTempDir],[MaxImageHeight],[MaxImageWidth],[UpscaleSmallImages])
‚Parameters:
‚[PathToSourceImage] => Path to source image as string (eg. C:Test.jpg)
‚[PathToTempDir] => Path to Tempfile as string (eg. C:Temp
‚[MaxImageHeight] => Max image height in pixels as int or long (eg. 200)
‚[MaxImageWidth] => Max image width in pixelx as int or long (eg. 200)
‚[UpscaleSmallImages] => 1 (true) or 0 (false)
Dim strResizedImage as String = lsImageResizer.StartResizePicture()
‚Possible Response Strings:
‚OK => Path to resized image as string (eg. C:Tempimg123456789.jpg)
‚ERROR => "ERROR" as string, no error description is suplied!
‚REMARK: This class does only the resizing stuff, it’s on your own to delete the probably unused generated files
‚ in the temp directory!
%END REM
Public Class LSJavaImageResizer
‚Define Vars
jSession As JAVASESSION
jObject As JAVAOBJECT
jClass As JAVACLASS
‚Resizer Setup Vars
strSourceFile As String
strTempDir As String
intHeight As Integer
intWidth As Integer
intMakeImageLarger As Integer
‚SUB New -> Cunstructor
Sub New (ByVal sFile As String, ByVal sTempDir As String, ByVal iHEIGHT As Integer, ByVal iWIDHT As Integer, ByVal iMakeImageLarger As Integer)
‚Initialize JAVA Class
Set jSession = New JAVASESSION
Set jClass=jSession.GetClass("JavaImageResizer")
Set jObject = jClass.CreateObject
strSourceFile = sFile
strTempDir = sTempDir
intHeight = iHeight
intWidth = iWidht
intMakeImageLarger = iMakeImageLarger
End Sub
‚Resize Image
Function StartResizePicture ()
Dim strReturnCode As String
strReturnCode=jObject.Resize(strSourceFile, strTempDir, intHeight, intWidth, intMakeImageLarger)
‚Return Path for resized Image
StartResizePicture = strReturnCode
End Function
End Class
[/sourcecode]
2. Schritt: Java Bibliotheke „JavaLibrary“ erstellen
Nachfolgend der Code für die Java Klasse „JavaImageResizer.java“
[sourcecode language=“java“ collapse=“true“ title=“Java Library JavaImageResizer.java for use in JavaLibrary library“]
/*
*
*Code and Library written by Yves Menge (yves.menge@gmail.com)
*For updates / news visit http://blog.y-m-e.net
*09.08.2011
*
*/
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.util.Calendar;
public class JavaImageResizer {
public String Resize(String strSourceFile, String strTempDir, int intMaxHeight, int intMaxWidth, int intMakeImageLarger){
float imgResizedWidth;
float imgResizedHeight;
String strOutputFile = "";
try{
//Get milliseconds (for random filename)
Calendar cal = Calendar.getInstance();
String strRandomNumber = String.valueOf(cal.getTimeInMillis());
//Construct destination path and get image type
strOutputFile = strTempDir + "img" + strRandomNumber + ".jpg";
BufferedImage originalImage = ImageIO.read(new File(strSourceFile));
int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
//Get original size
imgResizedWidth = originalImage.getWidth();
imgResizedHeight = originalImage.getHeight();
//Change dimensions to fit maximum width and height
float imgAspectRatio = 0;
// Calculate image dimension for destination
if (imgResizedWidth > intMaxWidth || imgResizedWidth < intMaxWidth && imgResizedWidth >= imgResizedHeight && intMakeImageLarger==1){
imgAspectRatio = intMaxWidth / imgResizedWidth;
imgResizedWidth = java.lang.Math.round(imgAspectRatio * imgResizedWidth);
imgResizedHeight = java.lang.Math.round(imgAspectRatio * imgResizedHeight);
}
if (imgResizedHeight > intMaxHeight || imgResizedHeight < intMaxHeight && imgResizedHeight >= imgResizedWidth && intMakeImageLarger==1) {
imgAspectRatio = intMaxHeight / imgResizedHeight;
imgResizedWidth = java.lang.Math.round(imgAspectRatio * imgResizedWidth);
imgResizedHeight = java.lang.Math.round(imgAspectRatio * imgResizedHeight);
}
//Resize Image
BufferedImage resizeImageHint = reSizeImage(originalImage, type, (int)imgResizedHeight, (int)imgResizedWidth);
ImageIO.write(resizeImageHint, "jpg", new File(strOutputFile));
}
catch(Exception e) {
e.printStackTrace();
strOutputFile="ERROR";
}
// Return path to resized image or ERROR in case of a failure
return strOutputFile;
}
private static BufferedImage reSizeImage(BufferedImage originalImage, int type, int imgHeight, int imgWidth){
BufferedImage resizedImage = new BufferedImage(imgWidth, imgHeight, type);
Graphics2D g = resizedImage.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(originalImage, 0, 0, imgWidth, imgHeight, null);
g.dispose();
g.setComposite(AlphaComposite.Src);
return resizedImage;
}
}
[/sourcecode]
Etwas zur Verwendung des Codes
Der Code darf mit einem Vermerk auf seine Herkunft gerne verwendet werden. Ich übernehme keine Garantie für mögliche Schäden oder Fehler welche durch meinen Code entstehen. Verbesserungen oder Kritik? Ab in die Kommentare damit :)