Skip to main content
DJ Gecko
Inspiring
December 18, 2013
Question

Using a devices camera and adding the image to the display list

  • December 18, 2013
  • 1 reply
  • 2006 views

Hi,

My students and I have not been able to make an AIR app that can take a picture using the devices camera and then add the image to the display list. We are able to open the devices camera and of course take a picture, but that's it.

We've been using these two tutorials/examples:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/CameraUI.html

and

http://tv.adobe.com/watch/adc-presents/input-for-mobile-devices-camera/

I've uploaded our project: http://www.dayvid.com/professor/camera.zip

Can someone help us out?

Thanks!

Below is the main document class:

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;

import flash.events.MouseEvent;

     public class Main extends MovieClip{

          private var deviceCameraApp:CameraUI = new CameraUI();

          private var imageLoader:Loader;

   

          public function Main()

                     {

               this.stage.align = StageAlign.TOP_LEFT;

               this.stage.scaleMode = StageScaleMode.NO_SCALE;

      

              

                                

                                 camera_btn.addEventListener(MouseEvent.CLICK, cameraBtnClicked);

          }

                     

                      private function cameraBtnClicked(event:MouseEvent):void

                      {

                                if( CameraUI.isSupported )

               {

                                                  result_txt.text = "Initializing camera...";

 

                                                  deviceCameraApp.addEventListener( MediaEvent.COMPLETE, imageCaptured );

                                                  deviceCameraApp.addEventListener( Event.CANCEL, captureCanceled );

                                                  deviceCameraApp.addEventListener( ErrorEvent.ERROR, cameraError );

                                                  deviceCameraApp.launch( MediaType.IMAGE );

               }

               else

               {

                                                  result_txt.text = "Camera interface is not supported.";

               }

                      }

                     

          private function imageCaptured( event:MediaEvent ):void

          {

               result_txt.text = "Media captured...";

      

               var imagePromise:MediaPromise = event.data;

      

               if( imagePromise.isAsync )

               {

                result_txt.text = "Asynchronous media promise.";

                imageLoader = new Loader();

                imageLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, asyncImageLoaded );

                imageLoader.addEventListener( IOErrorEvent.IO_ERROR, cameraError );

               

                imageLoader.loadFilePromise( imagePromise );

               }

               else

               {

                result_txt.text = "Synchronous media promise.";

                imageLoader.loadFilePromise( imagePromise );

                showMedia( imageLoader );

               }

          }

   

          private function captureCanceled( event:Event ):void

          {

               result_txt.text = "Media capture canceled.";

               NativeApplication.nativeApplication.exit();

          }

   

          private function asyncImageLoaded( event:Event ):void

          {

               result_txt.text = "Media loaded in memory.";

               showMedia( imageLoader );   

          }

   

          private function showMedia( loader:Loader ):void

          {

               this.addChild( loader );

          }

     

          private function cameraError( error:ErrorEvent ):void

          {

               result_txt.text = "Error:" + error.text;

               NativeApplication.nativeApplication.exit();

          }

     }

}

This topic has been closed for replies.

1 reply

Lars Laborious
Legend
December 18, 2013

Not sure if this will help you, but you might want to attach a camera object to a video object. Then you can draw the stage (or movieclip container) to a bitmap and add it to the cameraroll.

DJ Gecko
DJ GeckoAuthor
Inspiring
December 18, 2013

Hi,

Do I have to add the picture to the cameraroll in order to add it to the AIR apps display list?

Both examples from Adobe claim that their examples work. Do they not?

In the example, the event handler asyncImageLoaded is never called. The output text field shows -   result_txt.text = "Asynchronous media promise."; So the Event.COMPLETE is being added. But I don't think it's being dispatched.

Any ideas?

Lars Laborious
Legend
December 18, 2013

No, you don't have to add it to the cameraroll in order to add it to the displaylist. You would just draw the bitmap, then add it to the stage or a sprite / movieclip.

I haven't tried the examples you refer to. Does the text  "Media loaded in memory" not show up at all? And also, which version AIR do you use?