ACE

Calendar

April 2008
S M T W T F S
  1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30  

Tag Cloud

360Flex actionscript air air 2 android apple as3 books conference contest cookbook deeplinking Dreamweaver F3 v. F4 FileReference flash flash builder Flash Player flashplayer flashplayer10 Flex flex3 flex4 flexbuilder flickr fotobooth google gumbo ImageDropR InsideRIA iphone java load() localconnection max max 2009 merapi microsoft mobile nexus one pixel bender save() SilverLight training updatemanager

Categories

Archives

Recent Posts

Recent Comments


« iSpy motion detection AIR application | Main | Project ion+ »

Do a Screen Capture with Adobe AIR

By Rich Tretola | April 24, 2008
11,198 views

I have created a sample AIR application that takes screen shots and saves them to disc. Impossible? Well yes it would be impossible if I were only using AIR. However, thanks to the Merapi Java bridge, we have new functionality exposed to AIR. Yes, it is true that currently there is no shell execute functionality from AIR to start up the Java service. Hopefully, this will change with future versions of AIR.

For now in my test environment, I just manually start the Java service which will then listen for messages from AIR.

The first image shows the Merapi Screen Capture application right after it has been launched.

Merapi Screen Capture AIR application in initial state

The next image shows the AIR application after it has taken a screen shot. You can see that the new screen shot is now included within the application as a thumbnail.

Merapi Screen Capture after screen shot

Clicking on the thumbnail launches a preview of the new screen shot.

Merapi Screen Capture preview window

The screen shots are being stored in a directory under the user directory /Pictures/merapi/screenshots.

Mac Finder shows new image file

So how does this all work? In addition to the required jar files, there are 2 custom Java classes. The first class is the Main class which is runnable. This class has just one method which is the main method. It gets an instance of the merapi.Bridge class and then registers a message handler to react to incoming messages from AIR.

Main.class – main method

1
2
3
4
5
6
7
8
9
10
public static void main( String[] args ) throws Exception {
    /*
     * get an instance of the Bridge
     * Create a handler for incoming Flex message
     * register the handler and listen for the takeScreenShot message
     */

    Bridge bridge = Bridge.getInstance();
    ScreenCaptureMessageHandler handler = new ScreenCaptureMessageHandler();
    bridge.registerMessageHandler("takeScreenShot", handler);
}

The handler is a class named ScreenCaptureMessageHandler. This class implements the merapi.messages.IMessageHandler which must implement the abstract method handleMessage. This method accepts a merapi.messages.IMessage. If you take a look at the handleMessage method below, you can see that it simply calls another method named takeShot() and passes along a piece of data which contains the storage directory path from the incoming message.

ScreenCaptureMessageHandler.class – handleMessage method

1
2
3
4
5
6
7
8
9
10
public void handleMessage( IMessage message ){
    try{
        /*
         * read the storage directory from the message data object
         */

        takeShot(message.getData().toString());
    } catch (Exception e){
        System.out.print(e);
    }
}

The takeShot() method does the work of taking the screen shot, it then creates a unique file name and saves the png file to the storage directory that was passed in from AIR. Finally, it creates a new merapi.messages.Message object, sets the new absolute path of the saved png as data on the message, gets an instance of the merapi.Bridge and finally sends the message back to AIR.

ScreenCaptureMessageHandler.class – takeShot method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public void takeShot(String storageDirectory) throws
    AWTException, IOException, Exception {
    /*
     * capture the whole screen
     */

    BufferedImage screencapture = new Robot().createScreenCapture(
            new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
    /*
     * Create a new file using the incoming storage directory
     * and a unique time stamp
     * Save as a png
     */

    Date d = new Date();
    File file = new File(storageDirectory + "sc_" + d.getTime() + ".png");
    ImageIO.write(screencapture, "png", file);
    /*
     * Create a message
     * pass the file path to the merapi bridge
     * send the message to Flex
     */

    Message message = new Message();
        message.setData( file.getAbsolutePath() );
        Bridge bridge = Bridge.getInstance();
        bridge.sendMessage( message );
}

On the AIR side which in this case is a Flex application, the first thing that occurs is that the Merapi Bridge is instantiated through an MXML tag as shown below.

1
2
<!-- Initialize the connection to the merapi bridge -->
<merapi:BridgeInstance id="bridge" />

Next, upon applicationComplete an event listener is added to the bridge to listed for the completed message coming back from Java after the takeShot() method has completed.

1
bridge.addEventListener(ResultEvent.RESULT,completed);

The “Take Screen Shot” button within the AIR application calls the takeShot() ActionScript function shown below. This function first sets the application to not be visible (avoid appearing in the screen capture) and then creates a new Message sets the type to “takeScreenShot”, sets the data to the storage directory, and then sends the message to Java which if you remember from above already has a handler setup listening for the “takeScreenShot” message.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private function takeShot():void{
    /*
     * Hide application so it doesn't appear in screen shot
     */

    this.visible = false;
    /*
     * Create new message, set type to takeScreenShot
     * which matches the listener created on the java side of the merapi bridge,
     * pass the storage directory to the merapi bridge,
     * send the message on to java
     */

    var message:Message = new Message();
         message.type = "takeScreenShot";
         message.data = storageDirectory.nativePath + File.separator;
         bridge.sendMessage(message);
  }

Finally, Java completes its work it sends back the “completed” message which the AIR application is already setup to listen for. The listener calls the completed() ActionScript function shown below. This function simply returns the application to a visible state and adds the image thumbnail.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private function completed( event : ResultEvent ) : void {
    this.currentState = "withshots";
    this.visible = true;
    if(tile.numChildren > 9) {
            tile.removeAllChildren();
        }
    var message : IMessage = event.result as IMessage;

    var image:Image = new Image();
         image.addEventListener(MouseEvent.CLICK,openPreview);
         image.toolTip = "Click for preview";
             image.width =  45;
             image.height = 35;

    if(Capabilities.os.search("Mac") >= 0){
        image.source = "file://" + message.data as String;
    } else {
        image.source = message.data as String;
    }
        tile.addChild(image);
}

For more information on Merapi, please visit http://merapiproject.com

Topics: Adobe AIR, Artemis/Merapi, Flex | 25 Comments »

25 Responses to “Do a Screen Capture with Adobe AIR”

jeanphilippe Says:
April 25th, 2008 at 12:40 am

Hi,
It’s really an interesting project ! and a good job !
Where can we download the framework Merapi ? i don’t find it on merapiproject ?
Thanks
JP

Reply to this comment

everythingflex Says:
April 25th, 2008 at 4:40 am

The public beta is not available yet.

Reply to this comment

jeanphilippe Says:
April 25th, 2008 at 5:16 am

ok…
so, wait and see :)

Reply to this comment

Merapi Project Alpha Register Now! | EverythingFlex Says:
May 22nd, 2008 at 9:00 am

[...] up for the alpha and you will be able to run my screen capture sample as soon as the jars are [...]

Kinetic13 Says:
July 4th, 2008 at 5:29 am

Hi, is there another way to do this purely from Flex/Flash? Adobe Connect (which I believe is a Flash or Flex app) can share your screen? Is there a way to replicate this functionality for my own use?

Thanks!

Reply to this comment

everythingflex Says:
July 6th, 2008 at 8:06 am

No, you can not interact with the operating system from a web based flex or flash application and Adobe AIR does not have the ability to screen cap naively in AIR 1 either.

Reply to this comment

Denis Says:
July 10th, 2008 at 2:25 pm

Then how is Adobe doing it with Connect Now?? Does anybody know…

Reply to this comment

Santosh Says:
July 23rd, 2008 at 5:58 am

hi
i am register on merapi but i m unable to login there. i want to download merapi so please help me.

Reply to this comment

Santosh Says:
July 23rd, 2008 at 5:59 am

i am register on merapi but i m unable to login there. i want to download merapi so please help me.

Reply to this comment

Dave Says:
July 23rd, 2008 at 10:05 am

Santosh,

I can help you get logged in to the Merapi Project Web site. Please contact me at meekerd@gmail.com with your account name and I will fix things for you.

dave

Reply to this comment

Alberto Says:
August 22nd, 2008 at 6:04 pm

Do not try to replace what Java can do with AIR (AS). A screen capture application requires access to the OS environment and for sure and Adobe’s sake they will not get into that. That’s why I love AIR and love more Java.

Java will take care of the biz while AIR (AS) will take care of the presentation -and this is what Adobe AIR can do better.

Reply to this comment

Rich Tretola Reply:

That was precisely the point of this demo. AIR is handling the UI and bridging to Java through Merapi to do the work of taking the screen shot.

Reply to this comment

Tadeu Says:
September 9th, 2008 at 3:06 pm

Hello, friend would like to know how to make the communication with a JavaScript.

Reply to this comment

Rich Tretola Reply:

Are you referring to a JavaScript based AIR application?

Reply to this comment

Tadeu Says:
September 10th, 2008 at 6:44 am

Yes, iam developing a AJAX/AIR application.

Reply to this comment

Velocedge Says:
September 18th, 2008 at 7:50 am

Rich, Alberto… I don’t think Denis or Kinetic13 are trying to take anything away from this (very good) demo. But they have raised a very interesting question.

Rich stated basically it’s impossible to capture screenshots with AIR unless you use Merapi. However, Adobe can with Connect but they are not using Merapi. So, it begs the question, what technology exists that we don’t know about?

Reply to this comment

Rich Tretola Reply:

I guess I should have said that it is impossible with the public version of the Flash Player and AIR 1.

When you use Connect and launch a window to share your screen for the first time you will see that the Flash Player will ask you to install a Flash Player plugin that give the Flash Player additional capabilities.

Reply to this comment

Tadeu Says:
September 22nd, 2008 at 1:26 pm

Rich, the site of Merapi is written that there is the possibility to establish the communication of a Javascript application / AIR, but for now I see that we have only examples using Flex. There is some documentation on Javascript / AIR and Merapi?

Reply to this comment

mike Says:
October 3rd, 2008 at 3:28 pm

Can a bridge be built between a Flash Air application and the Merapi code?

Reply to this comment

N Seshagiri Rao Says:
October 10th, 2008 at 2:28 am

I have to download merapi Flex

Reply to this comment

Saravanan Says:
November 5th, 2008 at 9:23 am

Can we do the same with Flash+AIR+Merapi instead of flex…

I really need in my project.

-sara
http://www.designscripting.com/

Reply to this comment

John Says:
April 16th, 2009 at 7:10 pm

I am developing a small application that needs to take a screenshot of the user’s window.
When will merapi be available for download?
Can I use your code for my app?

Reply to this comment

Rich Tretola Reply:

Yes, you can certainly use my code samples. Merapi is available by registering at http://www.merapiproject.net/

Reply to this comment

1985 » airæ¡Œé¢æˆªå±çš„间接方法 Says:
April 27th, 2009 at 1:29 am

[...] 1.在airä¸­å®žçŽ°æ¡Œé¢æˆªå±åˆ°çŽ°åœ¨ä¸ºæ­¢è¿˜æ˜¯ä¸æ”¯æŒçš„。 2.如果éžè¦åœ¨air里实现截å±åŠŸèƒ½é‚£å°±æ˜¯æœ‰ä¸¤ç§æ–¹æ³•: 2.1  这个方法貌似很好,è¦ä¸‹Merapi[一个与java沟通的一个桥],坿˜¯æˆ‘æš‚æ—¶è¿˜æ²¡æœ‰æžæ˜Žç™½åˆ°åº•怎么弄,网站打n次终于打开,先注册,å†ä¸‹è½½ ï¼Œæœ€åŽæˆ‘çœ‹ä¸æ‡‚ï¼Œæ®æˆ‘看æ¥ï¼Œè²Œä¼¼å°±ç®—实现了,也è¦ç”¨æˆ·è£…java runtime,所以ä¸å¤ªçŽ°å®žã€‚ 2.2 在air中我们å¯ä»¥èŽ·å–到剪贴æ¿é‡Œçš„å†…å®¹ï¼Œæ‰€ä»¥äº§ç”Ÿäº†ç¬¬äºŒç§æ–¹æ³•ï¼Œæ— è®ºæ€Žä¹ˆå¼„éƒ½è¦æŒ‰PressScreen键。 2.2.1 åŠè‡ªåŠ¨ç±»åž‹ï¼š 实时监视剪贴æ¿çš„æ•°æ®ç±»åž‹ï¼ŒæŒ‰PrintScreen键盘就å¯ä»¥æˆªå±ï¼Œä½†æ˜¯å¦‚果你用其他截图软件(如果是ä¿å­˜åˆ°å‰ªè´´æ¿é‡Œçš„è¯)就会走光了 ,哪怕是qq截图。 2.2.2 全手动: 先按PressScreen 冿‰‹åŠ¨è§¦å‘ä¿å­˜ã€‚这个也容易走光,就是一ä¸å°å¿ƒå‰ªè´´æ¿é‡Œæœ‰å¼ å›¾ç‰‡ï¼Œç”¨æˆ·ç›´æŽ¥ç‚¹ä¿å­˜ï¼Œè¿˜æ˜¯å­˜åœ¨é—®é¢˜çš„。这个在googlecode上有开æºã€‚ [...]

JMP Says:
September 13th, 2009 at 12:54 pm

Not impossible using AIR. Just wrote an app which does so using the Bitmap Format from the Clipboard Explorer and then using JPEGAsyncEncoder to encode and upload to server via PHP

Reply to this comment

Comments



You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser