Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package controller
- {
- import flash.display.Bitmap;
- import flash.display.BitmapData;
- import flash.display.Loader;
- import flash.display.LoaderInfo;
- import flash.events.ErrorEvent;
- import flash.events.Event;
- import flash.events.EventDispatcher;
- import flash.events.IEventDispatcher;
- import flash.events.MediaEvent;
- import flash.geom.Matrix;
- import flash.media.CameraRoll;
- import flash.media.MediaPromise;
- import flash.net.FileReference;
- import flash.utils.ByteArray;
- import flash.utils.IDataInput;
- import avmplus.getQualifiedClassName;
- import event.CameraRollUIControllerEvent;
- import jp.shichiseki.exif.ExifInfo;
- import jp.shichiseki.exif.IFD;
- import model.DynamicValues;
- import view.module.InfoBox;
- public class CameraRollUIController extends EventDispatcher
- {
- protected var _fRef:FileReference;
- protected var _camRollUI:CameraRoll;
- protected var _dataSource:IDataInput;
- protected var _exif:ExifInfo;
- protected var _orientation:int;
- public function CameraRollUIController()
- {
- }
- public function launchCameraRollUI():void
- {
- if( CameraRoll.supportsBrowseForImage )
- {
- _camRollUI = new CameraRoll();
- _camRollUI.addEventListener( MediaEvent.SELECT, onImageSelectComplete );
- _camRollUI.addEventListener( Event.CANCEL, onImageSelectCanceled );
- _camRollUI.addEventListener( ErrorEvent.ERROR, onImageSelectError );
- _camRollUI.browseForImage();
- }
- else
- {
- _fRef = new FileReference();
- _fRef.addEventListener( Event.SELECT, fileSelectHandler );
- _fRef.addEventListener( Event.COMPLETE, fileLoadHandler );
- _fRef.browse();
- trace( "CameraRoll is not supported on this device, opening a classic file browser window.");
- }
- }
- ////////////////////////////////////////////////////////////////////////////// ADL HANDLING
- private function fileSelectHandler( e:Event ):void
- {
- _fRef.load();
- }
- private function fileLoadHandler( e:Event ):void
- {
- _dataSource = _fRef.data;
- readMediaData();
- }
- ////////////////////////////////////////////////////////////////////////////// MOBILE DEVICES HANDLING
- /**
- * Triggered when the capture phase is complete
- */
- private function onImageSelectComplete( e:MediaEvent ):void
- {
- dispatchEvent( new CameraRollUIControllerEvent( CameraRollUIControllerEvent.IMG_CAPTURE_PENDING, false, false ) );
- var imagePromise:MediaPromise = e.data as MediaPromise;
- _dataSource = imagePromise.open();
- if( imagePromise.isAsync )
- {
- var eventSource:IEventDispatcher = _dataSource as IEventDispatcher;
- eventSource.addEventListener( Event.COMPLETE, onMediaLoaded );
- }
- else
- {
- readMediaData();
- }
- }
- private function onMediaLoaded( e:Event ):void
- {
- readMediaData();
- }
- private function onImageSelectCanceled(e:Event):void
- {
- dispatchEvent( new CameraRollUIControllerEvent ( CameraRollUIControllerEvent.IMG_CAPTURE_CANCELED, false, false ) );
- }
- private function onImageSelectError( e:ErrorEvent ):void
- {
- dispatchEvent( new CameraRollUIControllerEvent( CameraRollUIControllerEvent.IMG_CAPTURE_ERROR, false, false ) );
- }
- /**
- * Reads the media data and creates Bitmap object
- */
- private function readMediaData():void
- {
- var ba:ByteArray = new ByteArray();
- _dataSource.readBytes( ba );
- _exif = new ExifInfo( ba );
- if( _exif.ifds != null )
- {
- if( getOrientation( _exif.ifds.primary ) != -1 ) //---> iOS JFIF
- {
- _orientation = getOrientation( _exif.ifds.primary )
- }
- else if( getOrientation( _exif.ifds.exif ) != -1 )
- {
- _orientation = getOrientation( _exif.ifds.exif ) // ---> Android EXIF
- }
- else
- {
- _orientation = 0;
- }
- }
- else
- {
- _orientation = 0;
- }
- var l:Loader = new Loader();
- l.contentLoaderInfo.addEventListener( Event.COMPLETE, onImgLoaded );
- l.loadBytes( ba );
- ba.clear();
- ba = null;
- _dataSource = null;
- }
- /**
- * Triggered once the image is loaded into the Loader instance
- */
- protected function onImgLoaded( e:Event ):void
- {
- var li:LoaderInfo = e.target as LoaderInfo;
- var lo:Loader = li.loader;
- if( lo.content.width / lo.content.height > ( 3 / 1 ) || lo.content.height / lo.content.width < ( 1 / 3 ) )
- {
- lo.unload();
- dispatchEvent( new CameraRollUIControllerEvent( CameraRollUIControllerEvent.PANORAMIC_PICTURE_DETECTED, false, false, null ) );
- return;
- }
- var bmd1:BitmapData = ( li.content as Bitmap ).bitmapData;
- var bmd2:BitmapData;
- var imgRatio:Number = bmd1.width / bmd1.height;
- var mat:Matrix = new Matrix();
- switch( _orientation )
- {
- case 0:
- {
- trace( "ORIENTATION 0° Home button on the right" );
- bmd2 = new BitmapData( bmd1.width * 612 / bmd1.height, 612, false ); // on crée un bitmap de 612 de large
- mat.scale( bmd2.width / bmd1.width, bmd2.height / bmd1.height );
- break;
- }
- case 90:
- {
- trace( "ORIENTATION 90° Home button on the bottom" );
- bmd2 = new BitmapData( 612, bmd1.width * 612 / bmd1.height, false ); // on crée un bitmap de 612 de haut
- mat.rotate( _orientation / 180 * Math.PI );
- mat.translate( bmd1.height , 0 );
- mat.scale( bmd2.width / bmd1.width * imgRatio, bmd2.height / bmd1.height / imgRatio );
- break;
- }
- case 180:
- {
- trace( "ORIENTATION 180° Home button on the left" );
- bmd2 = new BitmapData( bmd1.width * 612 / bmd1.height, 612, false ); // on crée un bitmap de 612 de large
- mat.rotate( _orientation / 180 * Math.PI );
- mat.translate( bmd1.width , bmd1.height );
- mat.scale( bmd2.width / bmd1.width, bmd2.height / bmd1.height );
- break;
- }
- case 270:
- {
- trace( "ORIENTATION 270° Home button on top" );
- bmd2 = new BitmapData( 612, bmd1.width * 612 / bmd1.height, false ); // on crée un bitmap de 612 de haut
- mat.rotate( _orientation / 180 * Math.PI );
- mat.translate( 0, bmd1.width );
- mat.scale( bmd2.width / bmd1.width * imgRatio, bmd2.height / bmd1.height / imgRatio );
- break;
- }
- }
- bmd2.draw( bmd1, mat, null, null, null, true );
- DynamicValues.getInstance().tmpBitmap = bmd2;
- bmd1.dispose();
- bmd1 = null;
- bmd2.dispose();
- bmd2 = null;
- li.removeEventListener( Event.COMPLETE, onImgLoaded );
- lo.unload();
- lo = null;
- dispatchEvent( new CameraRollUIControllerEvent( CameraRollUIControllerEvent.IMG_CAPTURE_COMPLETE, false, false ) );
- InfoBox.getInstance().dismiss();
- }
- /**
- * Return IFD tags
- */
- protected function displayIFD( ifd:IFD ):String
- {
- //trace(" --- " + ifd.level + " --- ");
- var str:String = "";
- for (var entry:String in ifd) {
- trace(entry + ": " + ifd[entry]);
- str += (entry + ": " + ifd[entry] + "\n")
- }
- return str;
- }
- /**
- * Return string according to image orientation
- */
- protected function getOrientation( ifd:IFD ):int
- {
- var r:int;
- var str:int = 0;
- for ( var entry:String in ifd )
- {
- if(entry == "Orientation")
- {
- str = ifd[entry];
- }
- }
- switch( str )
- {
- case 1: //normal
- r = 0;
- break;
- case 3: //rotated 180 degrees (upside down)
- r = 180;
- break;
- case 6: //rotated 90 degrees CW
- r = 90;
- break;
- case 8: //rotated 90 degrees CCW
- r = 270;
- break;
- case 9: //unknown
- r = -1
- break;
- }
- return r;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment