GetImageWidth
Code: Select all
package com.ebasetech.ufs.customfunctions;
import com.ebasetech.ufs.function.UFSCustomFunction;
import com.ebasetech.ufs.kernel.FormException;
import com.ebasetech.ufs.kernel.UFSFormInterface;
import java.io.File;
import java.awt.image.BufferedImage;
import java.util.Stack;
import javax.imageio.ImageIO;
import org.nfunk.jep.ParseException;
public class GetImageWidth extends UFSCustomFunction
{
public GetImageWidth(UFSFormInterface formInterface)
{
super(formInterface);
numberOfParameters = 1;
}
public void run(Stack inStack)
throws ParseException, FormException
{
checkStack(inStack);
Object param1 = inStack.pop();
if(param1 instanceof String)
{
String fileName = (String)param1;
boolean exists = (new File(fileName)).exists();
if(exists)
{
try {
File f = new File(fileName);
BufferedImage image = ImageIO.read(f);
int width = image.getWidth();
inStack.push(width);
} catch (Exception e) {
inStack.push("Kan de afbeelding niet openen.");
}
} else
{
inStack.push(new String((new StringBuilder()).append("ERROR::: File [ ").append(fileName).append(" ] does not exist").toString()));
}
} else
{
throw new ParseException("Invalid parameter type");
}
}
}
e.g. getimagewidth(pathToImage)
returns an integer (amount of pixels).
GetImageHeight
Code: Select all
package com.ebasetech.ufs.customfunctions;
import com.ebasetech.ufs.function.UFSCustomFunction;
import com.ebasetech.ufs.kernel.FormException;
import com.ebasetech.ufs.kernel.UFSFormInterface;
import java.io.File;
import java.awt.image.BufferedImage;
import java.util.Stack;
import javax.imageio.ImageIO;
import org.nfunk.jep.ParseException;
public class GetImageHeight extends UFSCustomFunction
{
public GetImageHeight(UFSFormInterface formInterface)
{
super(formInterface);
numberOfParameters = 1;
}
public void run(Stack inStack)
throws ParseException, FormException
{
checkStack(inStack);
Object param1 = inStack.pop();
if(param1 instanceof String)
{
String fileName = (String)param1;
boolean exists = (new File(fileName)).exists();
if(exists)
{
try {
File f = new File(fileName);
BufferedImage image = ImageIO.read(f);
int height = image.getHeight();
inStack.push(height);
} catch (Exception e) {
inStack.push("Kan de afbeelding niet openen.");
}
} else
{
inStack.push(new String((new StringBuilder()).append("ERROR::: File [ ").append(fileName).append(" ] does not exist").toString()));
}
} else
{
throw new ParseException("Invalid parameter type");
}
}
}
e.g. getimageheight(pathToImage)
returns an integer (amount of pixels).
ResizeImage
Code: Select all
package com.ebasetech.ufs.customfunctions;
import com.ebasetech.ufs.function.UFSCustomFunction;
import com.ebasetech.ufs.kernel.FormException;
import com.ebasetech.ufs.kernel.UFSFormInterface;
import java.io.File;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.Stack;
import javax.imageio.ImageIO;
import org.nfunk.jep.ParseException;
public class ResizeImage extends UFSCustomFunction
{
public ResizeImage(UFSFormInterface formInterface)
{
super(formInterface);
numberOfParameters = 4;
}
public void run(Stack inStack)
throws ParseException, FormException
{
checkStack(inStack);
Object param4 = inStack.pop();
Object param3 = inStack.pop();
Object param2 = inStack.pop();
Object param1 = inStack.pop();
if(param1 instanceof String && param2 instanceof Double && param3 instanceof Double && param4 instanceof String)
{
String fileName = (String)param1;
int width = (int) ((Double)param2).longValue();
int height = (int) ((Double)param3).longValue();
String dest = (String)param4;
boolean exists = (new File(fileName)).exists();
if(exists)
{
try {
File f = new File(fileName);
BufferedImage image = ImageIO.read(f);
BufferedImage thumb = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = thumb.createGraphics();
AffineTransform at = AffineTransform.getScaleInstance((double)width/image.getWidth(), (double)height/image.getHeight());
g.drawRenderedImage(image,at);
ImageIO.write(thumb, "JPG", new File(dest));
inStack.push("OK");
} catch (Exception e) {
inStack.push("Kan de afbeelding niet openen.");
}
} else
{
inStack.push(new String((new StringBuilder()).append("ERROR::: File [ ").append(fileName).append(" ] does not exist").toString()));
}
} else
{
throw new ParseException("Invalid parameter type");
}
}
}
e.g. resizieimage(pathToImage, width, height, pathToResizedImage)
returns 'OK'