[AS3]Adobe AIR 移动设备调用摄像头

话说之前在用AIR做一个自己用的iOS APP时,需要用到手机自带的摄像头。但是我也知道AIR在移动设备上的表现,特别是与iOS设备并不算配合的太好,所以很纠结,不知道是否和桌面版的AIR一样。

查阅了一下Adobe官方的API,了解到了移动设备上专门有一个CameraUI类,和Camera类用法大致相同。

详细过程(来自Adobe http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/media/CameraUI.html):

package  {
 import flash.desktop.NativeApplication;
 import flash.display.Loader;
 import flash.display.MovieClip;
 import flash.display.StageAlign;
 import flash.display.StageScaleMode;
 import flash.events.ErrorEvent;
 import flash.events.Event;
 import flash.events.IOErrorEvent;
 import flash.events.MediaEvent;
 import flash.media.CameraUI;
 import flash.media.MediaPromise;
 import flash.media.MediaType;
 
     public class CameraUIStillImage extends MovieClip{

          private var deviceCameraApp:CameraUI = new CameraUI();
          private var imageLoader:Loader; 
    
          public function CameraUIStillImage() {
               this.stage.align = StageAlign.TOP_LEFT;
               this.stage.scaleMode = StageScaleMode.NO_SCALE;
       
               if( CameraUI.isSupported )
               {
                trace( "Initializing camera..." );
    
                deviceCameraApp.addEventListener( MediaEvent.COMPLETE, imageCaptured );
                deviceCameraApp.addEventListener( Event.CANCEL, captureCanceled );
                deviceCameraApp.addEventListener( ErrorEvent.ERROR, cameraError );
                deviceCameraApp.launch( MediaType.IMAGE );
               }
               else
               {
                trace( "Camera interface is not supported.");
               }
          }
    
          private function imageCaptured( event:MediaEvent ):void
          {
               trace( "Media captured..." );
       
               var imagePromise:MediaPromise = event.data;
       
               if( imagePromise.isAsync )
               {
                trace( "Asynchronous media promise." );
                imageLoader = new Loader();
                imageLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, asyncImageLoaded );
                imageLoader.addEventListener( IOErrorEvent.IO_ERROR, cameraError );
                
                imageLoader.loadFilePromise( imagePromise );
               }
               else
               {
                trace( "Synchronous media promise." );
                imageLoader.loadFilePromise( imagePromise );
                showMedia( imageLoader );
               }
          }
    
          private function captureCanceled( event:Event ):void
          {
               trace( "Media capture canceled." );
               NativeApplication.nativeApplication.exit();
          }
    
          private function asyncImageLoaded( event:Event ):void
          {
               trace( "Media loaded in memory." );
               showMedia( imageLoader );    
          }
    
          private function showMedia( loader:Loader ):void
          {
               this.addChild( loader );
          }
      
          private function cameraError( error:ErrorEvent ):void
          {
               trace( "Error:" + error.text );
               NativeApplication.nativeApplication.exit();
          }
     }

可见各项方法和属性都是非常齐全的,在iPad2上真机试了一下,效果也是非常好,调用了原生的拍照功能,很赞!laugh



喜欢这篇文章?为什么不打赏一下呢?

爱发电

0 条评论

 

昵称

快来评论~