Custom functions GetImageHeight/GetImageWidth/resizeImage

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

Vircos
Ebase User
Posts: 97
Joined: Thu Sep 13, 2007 6:07 am
Location: The Netherlands

Custom functions GetImageHeight/GetImageWidth/resizeImage

#1

Postby Vircos » Fri Dec 09, 2011 12:15 pm

The following three Java classes can be used to get the image height or width and to resize an image.

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");
        }
    }
}
Usage: set STATUS = getimagewidth('image.jpg');
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");
        }
    }
}
Usage: set STATUS = getimageheight('image.jpg');
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");
        }
    }
}
Usage: set STATUS = resizeimage('image.jpg', 80, 80, 'thumb.jpg');
e.g. resizieimage(pathToImage, width, height, pathToResizedImage)

returns 'OK'
0 x
What's the meaning of Justice...

llyr
Ebase User
Posts: 7
Joined: Fri Dec 17, 2010 2:34 pm

#2

Postby llyr » Tue Feb 14, 2012 12:15 pm

Hi Vircos,

Is it possible that I could have the .class file for these functions? I'm having problems compiling - or perhaps you can show me how I'm meant to compile these?

Thanks,
Llyr
0 x

llyr
Ebase User
Posts: 7
Joined: Fri Dec 17, 2010 2:34 pm

#3

Postby llyr » Tue Feb 14, 2012 4:42 pm

No problem now, managed to complile.

Thanks very much for the functions - they could be very useful
0 x


Who is online

Users browsing this forum: No registered users and 122 guests