Page 1 of 1

get a screenshot as servoy function?

PostPosted: Fri Dec 16, 2011 11:55 am
by tgs
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:
Code: Select all
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

Re: get a screenshot as servoy function?

PostPosted: Fri Dec 16, 2011 12:48 pm
by ngervasi
No need to reinvent the wheel: http://www.servoy-plugins.de/plugins/screenshot/screenshot-plugin.html
Anyway executing java code in a servoy method is possible.

Re: get a screenshot as servoy function?

PostPosted: Fri Dec 16, 2011 1:11 pm
by tgs
Thank you Nicola!

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


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

Regards

Re: get a screenshot as servoy function?

PostPosted: Fri Dec 16, 2011 1:27 pm
by ngervasi
Just write it in the method, example:

Code: Select all
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

Re: get a screenshot as servoy function?

PostPosted: Tue Dec 20, 2011 10:43 am
by tgs
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!

Re: get a screenshot as servoy function?

PostPosted: Tue Dec 20, 2011 11:13 am
by 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

Re: get a screenshot as servoy function?

PostPosted: Tue Dec 20, 2011 11:38 am
by Kahuna
sovanm wrote: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:
Code: Select all
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:
Code: Select all
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

Re: get a screenshot as servoy function?

PostPosted: Tue Dec 20, 2011 1:28 pm
by sovanm
Thanks Ian for the reply.

That Helped....



Sovan