Guest User

CameraRollUIController

a guest
Jan 23rd, 2014
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package controller
  2. {
  3.    
  4.     import flash.display.Bitmap;
  5.     import flash.display.BitmapData;
  6.     import flash.display.Loader;
  7.     import flash.display.LoaderInfo;
  8.     import flash.events.ErrorEvent;
  9.     import flash.events.Event;
  10.     import flash.events.EventDispatcher;
  11.     import flash.events.IEventDispatcher;
  12.     import flash.events.MediaEvent;
  13.     import flash.geom.Matrix;
  14.     import flash.media.CameraRoll;
  15.     import flash.media.MediaPromise;
  16.     import flash.net.FileReference;
  17.     import flash.utils.ByteArray;
  18.     import flash.utils.IDataInput;
  19.    
  20.     import avmplus.getQualifiedClassName;
  21.    
  22.     import event.CameraRollUIControllerEvent;
  23.    
  24.     import jp.shichiseki.exif.ExifInfo;
  25.     import jp.shichiseki.exif.IFD;
  26.    
  27.     import model.DynamicValues;
  28.    
  29.     import view.module.InfoBox;
  30.    
  31.     public class CameraRollUIController extends EventDispatcher
  32.     {
  33.        
  34.         protected var _fRef:FileReference;
  35.         protected var _camRollUI:CameraRoll;
  36.         protected var _dataSource:IDataInput;
  37.         protected var _exif:ExifInfo;
  38.         protected var _orientation:int;
  39.        
  40.         public function CameraRollUIController()
  41.         {
  42.            
  43.         }
  44.        
  45.         public function launchCameraRollUI():void
  46.         {
  47.            
  48.             if( CameraRoll.supportsBrowseForImage )
  49.             {
  50.                 _camRollUI = new CameraRoll();
  51.                 _camRollUI.addEventListener( MediaEvent.SELECT, onImageSelectComplete );
  52.                 _camRollUI.addEventListener( Event.CANCEL, onImageSelectCanceled );
  53.                 _camRollUI.addEventListener( ErrorEvent.ERROR, onImageSelectError );
  54.                
  55.                 _camRollUI.browseForImage();
  56.                
  57.             }
  58.             else
  59.             {
  60.                 _fRef  = new FileReference();
  61.                 _fRef.addEventListener( Event.SELECT, fileSelectHandler );
  62.                 _fRef.addEventListener( Event.COMPLETE, fileLoadHandler );
  63.                 _fRef.browse();
  64.                 trace( "CameraRoll is not supported on this device, opening a classic file browser window.");
  65.             }
  66.            
  67.         }
  68.        
  69.         ////////////////////////////////////////////////////////////////////////////// ADL HANDLING
  70.         private function fileSelectHandler( e:Event ):void
  71.         {  
  72.             _fRef.load();  
  73.         }
  74.        
  75.         private function fileLoadHandler( e:Event ):void
  76.         {
  77.             _dataSource = _fRef.data;
  78.             readMediaData();
  79.         }
  80.        
  81.        
  82.         ////////////////////////////////////////////////////////////////////////////// MOBILE DEVICES HANDLING
  83.        
  84.         /**
  85.          * Triggered when the capture phase is complete
  86.          */
  87.         private function onImageSelectComplete( e:MediaEvent ):void
  88.         {
  89.            
  90.             dispatchEvent( new CameraRollUIControllerEvent( CameraRollUIControllerEvent.IMG_CAPTURE_PENDING, false, false ) );      
  91.            
  92.             var imagePromise:MediaPromise = e.data as MediaPromise;
  93.            
  94.             _dataSource = imagePromise.open();
  95.            
  96.             if( imagePromise.isAsync )
  97.             {
  98.                 var eventSource:IEventDispatcher = _dataSource as IEventDispatcher;    
  99.                 eventSource.addEventListener( Event.COMPLETE, onMediaLoaded );  
  100.             }
  101.             else
  102.             {
  103.                 readMediaData();
  104.             }
  105.            
  106.         }
  107.        
  108.         private function onMediaLoaded( e:Event ):void
  109.         {
  110.             readMediaData();
  111.         }
  112.        
  113.         private function onImageSelectCanceled(e:Event):void
  114.         {
  115.             dispatchEvent( new CameraRollUIControllerEvent ( CameraRollUIControllerEvent.IMG_CAPTURE_CANCELED, false, false ) );
  116.         }
  117.        
  118.         private function onImageSelectError( e:ErrorEvent ):void
  119.         {
  120.             dispatchEvent( new CameraRollUIControllerEvent( CameraRollUIControllerEvent.IMG_CAPTURE_ERROR, false, false ) );
  121.         }
  122.        
  123.         /**
  124.          * Reads the media data and creates Bitmap object
  125.          */
  126.         private function readMediaData():void
  127.         {
  128.             var ba:ByteArray = new ByteArray();
  129.            
  130.             _dataSource.readBytes( ba );
  131.             _exif = new ExifInfo( ba );
  132.            
  133.             if( _exif.ifds != null )
  134.             {
  135.                 if( getOrientation( _exif.ifds.primary ) != -1 ) //---> iOS JFIF
  136.                 {
  137.                     _orientation = getOrientation( _exif.ifds.primary )
  138.                 }
  139.                 else if( getOrientation( _exif.ifds.exif ) != -1 )
  140.                 {
  141.                     _orientation = getOrientation( _exif.ifds.exif ) // ---> Android EXIF
  142.                 }
  143.                 else
  144.                 {
  145.                     _orientation = 0;
  146.                 }
  147.             }
  148.             else
  149.             {
  150.                 _orientation = 0;
  151.             }
  152.            
  153.             var l:Loader = new Loader();
  154.             l.contentLoaderInfo.addEventListener( Event.COMPLETE, onImgLoaded );
  155.             l.loadBytes( ba );
  156.            
  157.             ba.clear();
  158.             ba = null;
  159.            
  160.             _dataSource = null;
  161.  
  162.         }
  163.        
  164.         /**
  165.          * Triggered once the image is loaded into the Loader instance
  166.          */
  167.         protected function onImgLoaded( e:Event ):void
  168.         {
  169.  
  170.             var li:LoaderInfo = e.target as LoaderInfo;
  171.             var lo:Loader = li.loader;
  172.            
  173.            
  174.             if( lo.content.width / lo.content.height > ( 3 / 1 ) || lo.content.height / lo.content.width < ( 1 / 3 ) )
  175.             {
  176.                 lo.unload();
  177.                 dispatchEvent( new CameraRollUIControllerEvent( CameraRollUIControllerEvent.PANORAMIC_PICTURE_DETECTED, false, false, null ) );
  178.                 return;
  179.             }
  180.            
  181.             var bmd1:BitmapData = ( li.content as Bitmap ).bitmapData;
  182.             var bmd2:BitmapData;
  183.            
  184.             var imgRatio:Number = bmd1.width / bmd1.height;
  185.             var mat:Matrix = new Matrix();
  186.            
  187.             switch( _orientation )
  188.             {
  189.                
  190.                 case 0:
  191.                 {
  192.                     trace( "ORIENTATION 0° Home button on the right" );
  193.                     bmd2 = new BitmapData( bmd1.width * 612 / bmd1.height, 612, false ); // on crée un bitmap de 612 de large
  194.                     mat.scale( bmd2.width / bmd1.width, bmd2.height / bmd1.height );
  195.                    
  196.                     break;
  197.                 }
  198.                
  199.                 case 90:
  200.                 {
  201.                     trace( "ORIENTATION 90° Home button on the bottom" );
  202.                     bmd2 = new BitmapData( 612, bmd1.width * 612 / bmd1.height, false ); // on crée un bitmap de 612 de haut
  203.                     mat.rotate( _orientation / 180 * Math.PI );
  204.                     mat.translate( bmd1.height , 0 );
  205.                     mat.scale( bmd2.width / bmd1.width * imgRatio, bmd2.height / bmd1.height / imgRatio );
  206.                    
  207.                     break;
  208.                 }
  209.                    
  210.                 case 180:
  211.                 {
  212.                     trace( "ORIENTATION 180° Home button on the left" );
  213.                     bmd2 = new BitmapData( bmd1.width * 612 / bmd1.height, 612, false ); // on crée un bitmap de 612 de large
  214.                     mat.rotate( _orientation / 180 * Math.PI );
  215.                     mat.translate( bmd1.width , bmd1.height );
  216.                     mat.scale( bmd2.width / bmd1.width, bmd2.height / bmd1.height );
  217.                    
  218.                     break;
  219.                 }
  220.                    
  221.                 case 270:
  222.                 {
  223.                     trace( "ORIENTATION 270° Home button on top" );
  224.                     bmd2 = new BitmapData( 612, bmd1.width * 612 / bmd1.height, false ); // on crée un bitmap de 612 de haut
  225.                     mat.rotate( _orientation / 180 * Math.PI );
  226.                     mat.translate( 0, bmd1.width );
  227.                     mat.scale( bmd2.width / bmd1.width * imgRatio, bmd2.height / bmd1.height / imgRatio );
  228.                    
  229.                     break;
  230.                 }
  231.  
  232.             }          
  233.            
  234.             bmd2.draw( bmd1, mat, null, null, null, true );
  235.            
  236.             DynamicValues.getInstance().tmpBitmap = bmd2;
  237.  
  238.             bmd1.dispose();
  239.             bmd1 = null;
  240.            
  241.             bmd2.dispose();
  242.             bmd2 = null;
  243.            
  244.             li.removeEventListener( Event.COMPLETE, onImgLoaded );
  245.            
  246.             lo.unload();
  247.             lo = null;
  248.            
  249.             dispatchEvent( new CameraRollUIControllerEvent( CameraRollUIControllerEvent.IMG_CAPTURE_COMPLETE, false, false ) );
  250.            
  251.             InfoBox.getInstance().dismiss();
  252.  
  253.         }
  254.        
  255.         /**
  256.          * Return IFD tags
  257.          */
  258.         protected function displayIFD( ifd:IFD ):String
  259.         {
  260.             //trace(" --- " + ifd.level + " --- ");
  261.             var str:String = "";
  262.             for (var entry:String in ifd) {
  263.                 trace(entry + ": " + ifd[entry]);
  264.                 str += (entry + ": " + ifd[entry] + "\n")
  265.             }
  266.            
  267.             return str;
  268.         }
  269.        
  270.        
  271.         /**
  272.          * Return string according to image orientation
  273.          */
  274.         protected function getOrientation( ifd:IFD ):int
  275.         {
  276.            
  277.             var r:int;
  278.            
  279.             var str:int = 0;
  280.             for ( var entry:String in ifd )
  281.             {
  282.                 if(entry == "Orientation")
  283.                 {
  284.                     str = ifd[entry];
  285.                 }
  286.             }
  287.            
  288.             switch( str )
  289.             {
  290.                 case 1: //normal
  291.                     r = 0;
  292.                     break;
  293.                 case 3: //rotated 180 degrees (upside down)
  294.                     r = 180;
  295.                     break;
  296.                 case 6: //rotated 90 degrees CW
  297.                     r = 90;
  298.                     break;
  299.                 case 8: //rotated 90 degrees CCW
  300.                     r = 270;
  301.                     break;
  302.                 case 9: //unknown
  303.                     r = -1
  304.                     break;
  305.             }
  306.            
  307.             return r;
  308.         }
  309.        
  310.     }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment