Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Simple AIR for iOS Package for selecting a cameraroll photo or taking a photo and processing it.
- Copyright 2012 FIZIX Digital Agency
- http://www.fizixstudios.com
- For more information see the tutorial at:
- http://www.fizixstudios.com/labs/do/view/id/air-ios-camera-and-uploading-photos
- Notes:
- This is a barebones script and is as generic as possible. The upload process is very basic,
- the tutorial linked above gives information on how to post the image along with data to
- your PHP script.
- The PHP script will collect as $_FILES['Filedata'];
- */
- package
- {
- import flash.display.MovieClip;
- import flash.events.MouseEvent;
- import flash.events.TouchEvent;
- import flash.ui.Multitouch;
- import flash.ui.MultitouchInputMode;
- import flash.media.Camera;
- import flash.media.CameraUI;
- import flash.media.CameraRoll;
- import flash.media.MediaPromise;
- import flash.media.MediaType;
- import flash.events.MediaEvent;
- import flash.events.Event;
- import flash.events.ErrorEvent;
- import flash.utils.IDataInput;
- import flash.events.IEventDispatcher;
- import flash.events.IOErrorEvent;
- import flash.utils.ByteArray;
- import flash.filesystem.File;
- import flash.filesystem.FileMode;
- import flash.filesystem.FileStream;
- import flash.errors.EOFError;
- import flash.net.URLRequest;
- import flash.net.URLVariables;
- import flash.net.URLRequestMethod;
- public class CameraTest extends MovieClip
- {
- // Define properties
- var cameraRoll:CameraRoll = new CameraRoll(); // For Camera Roll
- var cameraUI:CameraUI = new CameraUI(); // For Taking a Photo
- var dataSource:IDataInput; // Data Source
- var tempDir; // Our temporary directory
- public function CameraTest()
- {
- Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
- // Start the home screen
- startHomeScreen();
- }
- // =================================================================================
- // startHomeScreen
- // =================================================================================
- public function startHomeScreen()
- {
- trace("Main Screen Initialized");
- // Add main screen event listeners
- if(Multitouch.supportsGestureEvents)
- {
- mainScreen.startCamera.addEventListener(TouchEvent.TOUCH_TAP, initCamera);
- mainScreen.startCameraRoll.addEventListener(TouchEvent.TOUCH_TAP, initCameraRoll);
- }
- else
- {
- mainScreen.startCamera.addEventListener(MouseEvent.CLICK, initCamera);
- mainScreen.startCameraRoll.addEventListener(MouseEvent.CLICK, initCameraRoll);
- }
- }
- // =================================================================================
- // initCamera
- // =================================================================================
- private function initCamera(evt:Event):void
- {
- trace("Starting Camera");
- if( CameraUI.isSupported )
- {
- cameraUI.addEventListener(MediaEvent.COMPLETE, imageSelected);
- cameraUI.addEventListener(Event.CANCEL, browseCancelled);
- cameraUI.addEventListener(ErrorEvent.ERROR, mediaError);
- cameraUI.launch(MediaType.IMAGE);
- }
- else
- {
- mainScreen.feedbackText.text = "This device does not support Camera functions.";
- }
- }
- // =================================================================================
- // initCameraRoll
- // =================================================================================
- private function initCameraRoll(evt:Event):void
- {
- trace("Opening Camera Roll");
- if(CameraRoll.supportsBrowseForImage)
- {
- mainScreen.feedbackText.text = "Opening Camera Roll.";
- // Add event listeners for camera roll events
- cameraRoll.addEventListener(MediaEvent.SELECT, imageSelected);
- cameraRoll.addEventListener(Event.CANCEL, browseCancelled);
- cameraRoll.addEventListener(ErrorEvent.ERROR, mediaError);
- // Open up the camera roll
- cameraRoll.browseForImage();
- }
- else
- {
- mainScreen.feedbackText.text = "This device does not support CameraRoll functions.";
- }
- }
- // =================================================================================
- // imageSelected
- // =================================================================================
- private function imageSelected(evt:MediaEvent):void
- {
- mainScreen.feedbackText.text = "Image Selected";
- // Create a new imagePromise
- var imagePromise:MediaPromise = evt.data;
- // Open our data source
- dataSource = imagePromise.open();
- if(imagePromise.isAsync )
- {
- mainScreen.feedbackText.text += "Asynchronous Mode Media Promise.";
- var eventSource:IEventDispatcher = dataSource as IEventDispatcher;
- eventSource.addEventListener( Event.COMPLETE, onMediaLoaded );
- }
- else
- {
- mainScreen.feedbackText.text += "Synchronous Mode Media Promise.";
- readMediaData();
- }
- }
- // =================================================================================
- // browseCancelled
- // =================================================================================
- private function browseCancelled(event:Event):void
- {
- mainScreen.feedbackText.text = "Browse CameraRoll Cancelled";
- }
- // =================================================================================
- // mediaError
- // =================================================================================
- private function mediaError(event:Event):void
- {
- mainScreen.feedbackText.text = "There was an error";
- }
- // =================================================================================
- // onMediaLoaded
- // =================================================================================
- function onMediaLoaded( event:Event ):void
- {
- mainScreen.feedbackText.text += "Image Loaded.";
- readMediaData();
- }
- // =================================================================================
- // readMediaData
- // =================================================================================
- function readMediaData():void
- {
- mainScreen.feedbackText.text += "Reading Image Data.";
- var imageBytes:ByteArray = new ByteArray();
- dataSource.readBytes( imageBytes );
- tempDir = File.createTempDirectory();
- // Set the userURL
- var serverURL:String = "http://www.example.com/upload.php";
- // Get the date and create an image name
- var now:Date = new Date();
- var filename:String = "IMG" + now.fullYear + now.month + now.day + now.hours + now.minutes + now.seconds;
- // Create the temp file
- var temp:File = tempDir.resolvePath(filename);
- // Create a new FileStream
- var stream:FileStream = new FileStream();
- stream.open(temp, FileMode.WRITE);
- stream.writeBytes(imageBytes);
- stream.close();
- // Add event listeners for progress
- temp.addEventListener(Event.COMPLETE, uploadComplete);
- temp.addEventListener(IOErrorEvent.IO_ERROR, ioError);
- // Try to upload the file
- try
- {
- mainScreen.feedbackText.text += "Uploading File";
- //temp.upload(new URLRequest(serverURL));
- // We need to use URLVariables
- var params:URLVariables = new URLVariables();
- // Set the parameters that we will be posting alongside the image
- params.userid = "1234567";
- // Create a new URLRequest
- var request:URLRequest = new URLRequest(serverURL);
- // Set the request method to POST (as opposed to GET)
- request.method = URLRequestMethod.POST;
- // Put our parameters into request.data
- request.data = params;
- // Perform the upload
- temp.upload(request);
- }
- catch( e:Error )
- {
- trace(e);
- mainScreen.feedbackText.text += "Error Uploading File: " + e;
- removeTempDir();
- }
- }
- // =================================================================================
- // removeTempDir
- // =================================================================================
- function removeTempDir():void
- {
- tempDir.deleteDirectory(true);
- tempDir = null;
- }
- // ==================================================================================
- // uploadComplete()
- // ==================================================================================
- function uploadComplete(event:Event):void
- {
- mainScreen.feedbackText.text += "Upload Complete";
- }
- // ==================================================================================
- // ioError()
- // ==================================================================================
- function ioError(event:Event):void
- {
- mainScreen.feedbackText.text += "Unable to process photo";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement