Skip to content

Instantly share code, notes, and snippets.

@marchbold
Last active February 19, 2016 04:58
Show Gist options
  • Save marchbold/9798165 to your computer and use it in GitHub Desktop.
Save marchbold/9798165 to your computer and use it in GitHub Desktop.
A simple example adapted to use Camera extension. This demonstrates initialising the extension, setting up listeners, adding the video frames and updating, and capturing an image on click.
/**
* __ __ __
* ____/ /_ ____/ /______ _ ___ / /_
* / __ / / ___/ __/ ___/ / __ `/ __/
* / /_/ / (__ ) / / / / / /_/ / /
* \__,_/_/____/_/ /_/ /_/\__, /_/
* / /
* \/
* http://distriqt.com
* -------------------------------------
*
* brief: Camera ANE example for timeline usage
* tag: distriqt.extension.camera
*
*/
import com.distriqt.extension.camera.Camera;
import com.distriqt.extension.camera.CameraMode;
import com.distriqt.extension.camera.CameraParameters;
import com.distriqt.extension.camera.CaptureDevice;
import com.distriqt.extension.camera.events.CameraDataEvent;
import com.distriqt.extension.camera.events.CameraEvent;
var lastFrameProcessed:Number = -1;
var videoData:ByteArray = new ByteArray();
var bitmapData:BitmapData = new BitmapData( 640, 480, false );
// This bitmap will hold the view from the camera
var bitmap:Bitmap = new Bitmap( bitmapData );
addChild( bitmap );
var dev_key:String = "";
//
// You'll need to create some interface to take the photo - for demonstration purposes we'll just add a click listener
stage.addEventListener( MouseEvent.CLICK, clickHandler, false, 0, true );
try
{
com.distriqt.extension.camera.Camera.init( dev_key );
if (com.distriqt.extension.camera.Camera.isSupported)
{
var options:CameraParameters = new CameraParameters();
options.enableFrameBuffer = true;
options.frameBufferWidth = 800;
options.frameBufferHeight = 600;
options.cameraMode = new CameraMode( CameraMode.PRESET_MEDIUM );
com.distriqt.extension.camera.Camera.instance.addEventListener( CameraEvent.VIDEO_FRAME, camera_videoFrameHandler, false, 0, true );
com.distriqt.extension.camera.Camera.instance.addEventListener( CameraDataEvent.CAPTURED_IMAGE, camera_capturedImageHandler, false, 0, true );
com.distriqt.extension.camera.Camera.instance.initialise( options );
}
}
catch (e:Error)
{
}
function camera_videoFrameHandler( event:CameraEvent ):void
{
var frame:Number = com.distriqt.extension.camera.Camera.instance.receivedFrames;
if (frame != lastFrameProcessed)
{
com.distriqt.extension.camera.Camera.instance.getFrameBuffer( videoData );
var rect:Rectangle = new Rectangle( 0, 0, com.distriqt.extension.camera.Camera.instance.width, com.distriqt.extension.camera.Camera.instance.height );
if (bitmapData.width != com.distriqt.extension.camera.Camera.instance.width || bitmapData.height != com.distriqt.extension.camera.Camera.instance.height)
{
bitmapData = new BitmapData( com.distriqt.extension.camera.Camera.instance.width, com.distriqt.extension.camera.Camera.instance.height, false );
bitmap.bitmapData.dispose();
bitmap.bitmapData = bitmapData;
}
try
{
bitmapData.setPixels( rect, videoData );
}
catch (e:Error)
{
trace( "ERROR::setPixels: " + e.message );
}
videoData.clear();
lastFrameProcessed = frame;
}
}
function clickHandler( event:MouseEvent ):void
{
// Start the image capture
com.distriqt.extension.camera.Camera.instance.captureImage( false );
}
function camera_capturedImageHandler( event:CameraDataEvent ):void
{
// Here the event.data property will hold the BitmapData from the image capture.
// You can add this to your interface as you see fit
}
// com.distriqt.Camera
@micha01
Copy link

micha01 commented Aug 8, 2015

I use this example to take photos on iPhone 6 and save them in camera roll. No problem so far. But how to start video recording and how to save video on iPhone's camera roll? And how to switch front / back camera? Can you please give (timeline) examples for those not so used in AS3? Thanks in advance.

@anujk006
Copy link

I have created another .fla file & i am calling timeline.as
// 1st line in my .fla
import timeline;

The target is Air 19.0.0.147 for iOS

as per the above example clicking anywhere on the stage should take a picture & display on stage, but when i publish this .ipa to my iPad & i click on the screen, nothing happens. Kindly help.

in timeline.as i have added the dev_key that was generated for my app
var dev_key:String = "xxxxxxxxxxxxxx";

@marchbold
Copy link
Author

Hi,

We don't get notifications on comments on gists. Please post your questions in the support forum: https://github.com/distriqt/ANE-Camera/issues

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment