get a screenshot as servoy function?

I would like to get a screenshot by executing a servoy function.
I couldn’t find a plugin or anything else to get this running, but I found a pure java code for that:

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
 
...
 
public void captureScreen(String fileName) throws Exception {
 
   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
   Rectangle screenRectangle = new Rectangle(screenSize);
   Robot robot = new Robot();
   BufferedImage image = robot.createScreenCapture(screenRectangle);
   ImageIO.write(image, "png", new File(fileName));
 
}

Is it possible to execute a pur java code from a servoy method, or do I have to create a plugin for that?

Regards

No need to reinvent the wheel: Servoy-Plugins.de - Screenshot plugin
Anyway executing java code in a servoy method is possible.

Thank you Nicola!

ngervasi:
Anyway executing java code in a servoy method is possible.

How do I get java code in a servoy method running?

Regards

Just write it in the method, example:

var fr = new Packages.java.io.FileReader('/tmp/xyz.log'); 
var br = new Packages.java.io.BufferedReader(fr); 
global.my3lines += br.readLine(); 
global.my3lines += br.readLine(); 
global.my3lines += br.readLine(); 
br.close();//do NOT forget this close! to prevent mem leaks, even better to use try/finally here

Hi Nicola,

sorry for the delay. The sreenshot function by Patrick’s plugin is working well. Now I will try executing Java in a Servoy method like your example. Hope I get it working…

Merry Christmas!

Hi Thomas,

I also tried to execute java codes from servoy method but didn’t succeed … :(
It would be nice if you could share a sample method to execute the same…

Sovan

sovanm:
Hi Thomas,

I also tried to execute java codes from servoy method but didn’t succeed … :(
It would be nice if you could share a sample method to execute the same…

Sovan

We use the screenshot code from Arup (was with Mindfire but not sure now)

To call it:

function onTakeScreenSnapshot(event) 
{
	var desktopImage = globals.takeScreenShot("png");
	var saveFileName = plugins.file.showFileSaveDialog('RISCm_screen_dump.png','Save Image')
	var createdFile = plugins.file.writeFile(saveFileName,desktopImage)
	if(!createdFile)
	{	globals.DIALOGS.showErrorDialog('Flie Save Error','Your File Was NOT Saved', 'OK')
	}
	else {globals.DIALOGS.showInfoDialog('Flie Saved','Your File Was Saved to '+saveFileName, 'OK')}
}

The function:

function takeScreenShot()

{          /*
            method name : takeScreenShot
            usage: takeScreenShot(imageFormat);
            input: imageFormat: text representing the image mime type.
            Ex. "png", "jpg", "jpeg" etc.
            output: The script will take a screen shot of your desktop
            and returns byte array of the specified image format.
            * IRJC Modified this to grab just the current Servoy Window.
            note:
                Change history:
                04/14/09             Arup Ranjan Sahoo            Created method
 *********************************************************************/
         //Get the Image Format
         var imageFormat = arguments[0];
         //Get the Desktop Screen Size
         screenWidth = application.getWindowWidth()
         screenHeight = application.getWindowHeight()
         screenX = application.getWindowX()
         screenY =application.getWindowY()
         var screenRect = new java.awt.Rectangle(screenX, screenY, screenWidth, screenHeight);
         //Capture the Screen as a Buffered Image
         var screenImage = new java.awt.Robot().createScreenCapture(screenRect);
         //Convert the Buffered Image to Byte Array
         var baos = new java.io.ByteArrayOutputStream();
         Packages.javax.imageio.ImageIO.write(screenImage, imageFormat, baos);
         return baos.toByteArray();
}

HTH

Ian

Thanks Ian for the reply.

That Helped…

Sovan