[AIR] 利用SnapShot.exe实现QQ截屏功能

主类(可作文档类):

package
{
    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    
    /**
     * @author: Frost.Yen
     * @E-mail: 871979853@qq.com
     * @create: 2016-5-6 上午9:57:53
     *
     */
    [SWF(width="800",height="600")]
    public class ScreenShot extends Sprite
    {
        private var _snapShot:SnapShot;
        private var _button:TextField;
        private var _text:TextField;
        private var _container:Sprite;
        public function ScreenShot()
        {
            _container = new Sprite();
            _button = textButton("   click me to shot!   ");
            _text = textButton("   CTRL+ALT+A 也可以截屏   ");
            _snapShot = new SnapShot();
            _button.x = stage.stageWidth-_button.width-10;
            _button.y = stage.stageHeight-_button.height-10;
            _text.x = _button.x - _text.width - 10;
            _text.y = _button.y;
            _snapShot.addEventListener(Event.COMPLETE,onShotComplete);
            _button.addEventListener(MouseEvent.CLICK,onShot);
            this.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
            this.addChild(_container);
            this.addChild(_button);
            this.addChild(_text);
        }
        private function onShot(e:MouseEvent):void
        {
            _snapShot.start();
        }
        private function onShotComplete(e:Event):void
        {
            if(_container.numChildren){
                (_container.getChildAt(0) as Bitmap).bitmapData.dispose();
                _container.removeChildAt(0);
            }
            _container.addChild(_snapShot.bitmap);
        }
        private function onKeyDown(e:KeyboardEvent):void
        {
            if (e.altKey && e.ctrlKey && e.keyCode == 65) {
                _snapShot.start();
            }
        }
        private function textButton(text:String=""):TextField
        {
            var button:TextField = new TextField();
            button.autoSize = "center";
            button.text = text;
            button.selectable = false;
            button.border = true;
            return button;
        }
    }
}

 

用于截屏的类

package
{
    import flash.desktop.Clipboard;
    import flash.desktop.ClipboardFormats;
    import flash.desktop.NativeProcess;
    import flash.desktop.NativeProcessStartupInfo;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.IEventDispatcher;
    import flash.events.NativeProcessExitEvent;
    import flash.filesystem.File;
    
    /**
     * @author: Frost.Yen
     * @E-mail: 871979853@qq.com
     * @create: 2016-5-6 上午10:00:29
     * 截屏后生成图片
     */
    public class SnapShot extends EventDispatcher
    {
        private var _file:File;
        private var _nativeProcessStartupInfo:NativeProcessStartupInfo;
        private var _nativeProgress:NativeProcess;
        private var _bitmapData:BitmapData;
        private var _bitmap:Bitmap;
        private var _completeEvent:Event;
        public function SnapShot(target:IEventDispatcher=null)
        {
            super(target);
            _file = File.applicationDirectory.resolvePath("ExtendApplication/SnapShot.exe");
            
            /**
             * 此类提供了用于启动主机操作系统上的进程的基本信息。构建该类并将其传递给 NativeProcess 对象的 start() 方法。
             */
            _nativeProcessStartupInfo = new NativeProcessStartupInfo();
            
            /**
             * 如果指定的值是 null、如果它引用一个目录或引用一个不存在的文件。
             */
            _nativeProcessStartupInfo.executable = _file;
            
            /**
             * NativeProcess 类提供命令行集成和常规启动功能。
             * NativeProcess 类允许 AIR 应用程序在主机操作系统上执行本机进程。
             * AIR 应用程序可以监视进程的标准输入 (stdin) 和标准输出 (stdout) 流以及进程的标准错误 (stderr) 流。
             */
            _nativeProgress = new NativeProcess();
            _completeEvent = new Event(Event.COMPLETE);
        }
        public function start():void
        {
            /**
             *启动由指定的启动信息标识的本机进程。进程启动后,所有输入流和输出流将打开。
             * 在请求操作系统启动指定的进程后,此方法将立即返回。如果进程当前正在运行,
             * NativeProcess 对象会引发 IllegalOperationError 异常。如果 NativeProcess 
             * 对象的 running 属性返回 true,则表示此进程正在运行。如果操作系统无法启动进程,
             * 会引发 Error。 
             */
            _nativeProgress.start(_nativeProcessStartupInfo);
            /**
             * 退出进程时,由 NativeProcess 对象调度此事件。可能会永不调度此事件。例如,
             * 如果子进程的运行时间比创建它的 AIR 应用程序运行时间长,则不会调度此事件。
             */
            _nativeProgress.addEventListener(NativeProcessExitEvent.EXIT,onExit); 
            
        }
        private function onExit(e:NativeProcessExitEvent):void
        {
            if (Clipboard.generalClipboard.hasFormat(ClipboardFormats.BITMAP_FORMAT))
            {
                _bitmapData = Clipboard.generalClipboard.getData(ClipboardFormats.BITMAP_FORMAT) as BitmapData;
                _bitmap = new Bitmap(_bitmapData);
                dispatchEvent(_completeEvent);
            }
        }
        public function get bitmap():Bitmap
        {
            return _bitmap;
        }
    }
}

 

 

注意:

在工程的配置 工程名称-app.xml上加上如下代码
<supportedProfiles>extendedDesktop</supportedProfiles>
添加的位置可以为如下:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<application xmlns="http://ns.adobe.com/air/application/17.0">
<supportedProfiles>extendedDesktop</supportedProfiles>

posted on 2016-05-06 11:03  晏过留痕  阅读(1882)  评论(0编辑  收藏  举报