Advertisement
FIZIXAgency

AIR for iOS - take photo, select from cameraroll and upload

Jul 13th, 2012
2,150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Simple AIR for iOS Package for selecting a cameraroll photo or taking a photo and processing it.
  3.    
  4.     Copyright 2012 FIZIX Digital Agency
  5.     http://www.fizixstudios.com
  6.    
  7.     For more information see the tutorial at:
  8.     http://www.fizixstudios.com/labs/do/view/id/air-ios-camera-and-uploading-photos
  9.    
  10.    
  11.     Notes:
  12.     This is a barebones script and is as generic as possible. The upload process is very basic,
  13.     the tutorial linked above gives information on how to post the image along with data to
  14.     your PHP script.
  15.    
  16.     The PHP script will collect as $_FILES['Filedata'];
  17. */
  18.  
  19. package  
  20. {  
  21.     import flash.display.MovieClip;
  22.     import flash.events.MouseEvent;
  23.     import flash.events.TouchEvent;
  24.    
  25.     import flash.ui.Multitouch;
  26.     import flash.ui.MultitouchInputMode;
  27.    
  28.     import flash.media.Camera;
  29.     import flash.media.CameraUI;
  30.     import flash.media.CameraRoll;
  31.     import flash.media.MediaPromise;
  32.     import flash.media.MediaType;
  33.     import flash.events.MediaEvent;
  34.     import flash.events.Event; 
  35.     import flash.events.ErrorEvent;
  36.    
  37.     import flash.utils.IDataInput;
  38.     import flash.events.IEventDispatcher;
  39.     import flash.events.IOErrorEvent;
  40.    
  41.     import flash.utils.ByteArray;
  42.     import flash.filesystem.File;
  43.     import flash.filesystem.FileMode;
  44.     import flash.filesystem.FileStream;
  45.    
  46.     import flash.errors.EOFError;
  47.     import flash.net.URLRequest;
  48.     import flash.net.URLVariables;
  49.     import flash.net.URLRequestMethod;
  50.  
  51.    
  52.    
  53.    
  54.     public class CameraTest extends MovieClip
  55.     {      
  56.         // Define properties
  57.         var cameraRoll:CameraRoll = new CameraRoll();               // For Camera Roll
  58.         var cameraUI:CameraUI = new CameraUI();                     // For Taking a Photo
  59.         var dataSource:IDataInput;                                  // Data Source
  60.         var tempDir;                                                // Our temporary directory
  61.        
  62.        
  63.         public function CameraTest()
  64.         {
  65.             Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
  66.            
  67.             // Start the home screen
  68.             startHomeScreen();         
  69.         }
  70.        
  71.        
  72.        
  73.         // =================================================================================
  74.         // startHomeScreen
  75.         // =================================================================================
  76.         public function startHomeScreen()
  77.         {
  78.             trace("Main Screen Initialized");          
  79.            
  80.            
  81.             // Add main screen event listeners
  82.             if(Multitouch.supportsGestureEvents)
  83.             {
  84.                 mainScreen.startCamera.addEventListener(TouchEvent.TOUCH_TAP, initCamera);
  85.                 mainScreen.startCameraRoll.addEventListener(TouchEvent.TOUCH_TAP, initCameraRoll);
  86.             }
  87.             else
  88.             {
  89.                 mainScreen.startCamera.addEventListener(MouseEvent.CLICK, initCamera);
  90.                 mainScreen.startCameraRoll.addEventListener(MouseEvent.CLICK, initCameraRoll);
  91.             }
  92.         }
  93.        
  94.        
  95.        
  96.         // =================================================================================
  97.         // initCamera
  98.         // =================================================================================
  99.         private function initCamera(evt:Event):void
  100.         {
  101.             trace("Starting Camera");
  102.            
  103.             if( CameraUI.isSupported )
  104.             {
  105.                 cameraUI.addEventListener(MediaEvent.COMPLETE, imageSelected);
  106.                 cameraUI.addEventListener(Event.CANCEL, browseCancelled);
  107.                 cameraUI.addEventListener(ErrorEvent.ERROR, mediaError);
  108.                
  109.                 cameraUI.launch(MediaType.IMAGE);
  110.             }
  111.             else
  112.             {
  113.                 mainScreen.feedbackText.text = "This device does not support Camera functions.";
  114.             }
  115.         }  
  116.        
  117.        
  118.        
  119.         // =================================================================================
  120.         // initCameraRoll
  121.         // =================================================================================
  122.         private function initCameraRoll(evt:Event):void
  123.         {
  124.             trace("Opening Camera Roll");
  125.            
  126.             if(CameraRoll.supportsBrowseForImage)
  127.             {
  128.                 mainScreen.feedbackText.text = "Opening Camera Roll.";
  129.                
  130.                 // Add event listeners for camera roll events
  131.                 cameraRoll.addEventListener(MediaEvent.SELECT, imageSelected);
  132.                 cameraRoll.addEventListener(Event.CANCEL, browseCancelled);
  133.                 cameraRoll.addEventListener(ErrorEvent.ERROR, mediaError);
  134.                
  135.                 // Open up the camera roll
  136.                 cameraRoll.browseForImage();
  137.             }
  138.             else
  139.             {
  140.                 mainScreen.feedbackText.text = "This device does not support CameraRoll functions.";
  141.             }
  142.         }
  143.        
  144.        
  145.        
  146.         // =================================================================================
  147.         // imageSelected
  148.         // =================================================================================
  149.         private function imageSelected(evt:MediaEvent):void
  150.         {
  151.             mainScreen.feedbackText.text = "Image Selected";
  152.            
  153.            
  154.             // Create a new imagePromise
  155.             var imagePromise:MediaPromise = evt.data;
  156.            
  157.            
  158.             // Open our data source
  159.             dataSource = imagePromise.open();
  160.            
  161.            
  162.             if(imagePromise.isAsync )
  163.             {
  164.                 mainScreen.feedbackText.text += "Asynchronous Mode Media Promise.";
  165.        
  166.                 var eventSource:IEventDispatcher = dataSource as IEventDispatcher;
  167.        
  168.                 eventSource.addEventListener( Event.COMPLETE, onMediaLoaded );
  169.             }
  170.             else
  171.             {
  172.                 mainScreen.feedbackText.text += "Synchronous Mode Media Promise.";
  173.                 readMediaData();
  174.             }          
  175.         }
  176.        
  177.        
  178.        
  179.         // =================================================================================
  180.         // browseCancelled
  181.         // =================================================================================
  182.         private function browseCancelled(event:Event):void
  183.         {
  184.             mainScreen.feedbackText.text = "Browse CameraRoll Cancelled";
  185.         }
  186.        
  187.        
  188.        
  189.         // =================================================================================
  190.         // mediaError
  191.         // =================================================================================
  192.         private function mediaError(event:Event):void
  193.         {
  194.             mainScreen.feedbackText.text = "There was an error";
  195.         }
  196.        
  197.        
  198.        
  199.         // =================================================================================
  200.         // onMediaLoaded
  201.         // =================================================================================
  202.         function onMediaLoaded( event:Event ):void
  203.         {
  204.             mainScreen.feedbackText.text += "Image Loaded.";
  205.             readMediaData();
  206.         }
  207.        
  208.        
  209.        
  210.         // =================================================================================
  211.         // readMediaData
  212.         // =================================================================================
  213.         function readMediaData():void
  214.         {
  215.             mainScreen.feedbackText.text += "Reading Image Data.";
  216.            
  217.             var imageBytes:ByteArray = new ByteArray();
  218.            
  219.             dataSource.readBytes( imageBytes );
  220.             tempDir = File.createTempDirectory();
  221.            
  222.            
  223.             // Set the userURL
  224.             var serverURL:String = "http://www.example.com/upload.php";
  225.            
  226.            
  227.             // Get the date and create an image name
  228.             var now:Date = new Date();         
  229.             var filename:String = "IMG" + now.fullYear + now.month + now.day + now.hours + now.minutes + now.seconds;
  230.    
  231.    
  232.             // Create the temp file
  233.             var temp:File = tempDir.resolvePath(filename);
  234.            
  235.            
  236.             // Create a new FileStream
  237.             var stream:FileStream = new FileStream();
  238.            
  239.             stream.open(temp, FileMode.WRITE);
  240.             stream.writeBytes(imageBytes);
  241.             stream.close();
  242.            
  243.            
  244.            
  245.             // Add event listeners for progress
  246.             temp.addEventListener(Event.COMPLETE, uploadComplete);
  247.             temp.addEventListener(IOErrorEvent.IO_ERROR, ioError);
  248.            
  249.            
  250.            
  251.             // Try to upload the file
  252.             try
  253.             {
  254.                 mainScreen.feedbackText.text += "Uploading File";
  255.                 //temp.upload(new URLRequest(serverURL));
  256.                
  257.                
  258.                 // We need to use URLVariables
  259.                 var params:URLVariables = new URLVariables();
  260.            
  261.            
  262.                 // Set the parameters that we will be posting alongside the image
  263.                 params.userid = "1234567";
  264.            
  265.                 // Create a new URLRequest
  266.                 var request:URLRequest = new URLRequest(serverURL);
  267.                
  268.                 // Set the request method to POST (as opposed to GET)
  269.                 request.method = URLRequestMethod.POST;
  270.                
  271.                 // Put our parameters into request.data
  272.                 request.data = params;
  273.                
  274.                 // Perform the upload
  275.                 temp.upload(request);
  276.             }
  277.             catch( e:Error )
  278.             {
  279.                 trace(e);
  280.                
  281.                 mainScreen.feedbackText.text += "Error Uploading File: " + e;
  282.                
  283.                 removeTempDir();               
  284.             }
  285.         }  
  286.        
  287.        
  288.        
  289.         // =================================================================================
  290.         // removeTempDir
  291.         // =================================================================================
  292.         function removeTempDir():void
  293.         {
  294.             tempDir.deleteDirectory(true);
  295.             tempDir = null;
  296.         }
  297.        
  298.        
  299.        
  300.         // ==================================================================================
  301.         // uploadComplete()    
  302.         // ==================================================================================
  303.         function uploadComplete(event:Event):void
  304.         {
  305.             mainScreen.feedbackText.text += "Upload Complete";
  306.         }
  307.        
  308.        
  309.        
  310.         // ==================================================================================
  311.         // ioError()       
  312.         // ==================================================================================
  313.         function ioError(event:Event):void
  314.         {
  315.             mainScreen.feedbackText.text += "Unable to process photo";
  316.         }
  317.     }
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement