1 Reply Latest reply on Apr 11, 2012 3:07 PM by nskoro

    Rotate Image - iOS CameraUI

    marthaic88

      Hello!

       

      I am trying to do the follow steps using CameraUI in iOS:

      1. Take a photo

      2. Show photo in the screen

      3. Rotate this photo

      4. Save the rotated photo in the CameraRoll

       

      to do that i wrote the follow code:

       

      Class Camera

       

      import ……..;

       

      private var camera:CameraUI;

      private var roll:CameraRoll;

      private var mpLoader:Loader;

      private var mediaPromise:MediaPromise;

       

      private var bitmapData:BitmapData;

      private var loaderInfoData:LoaderInfo;

               

      private var dataSource:IDataInput;

      private var tempDir:File;

      private var file:File;

      private var mediaBytes:ByteArray;

               

      private var ld:Loader;   

      private var stream:FileStream;

       

       

       

       

      public function showCamera():void{  

           if (CameraUI.isSupported){                

                camera = new CameraUI();

                camera.addEventListener(MediaEvent.COMPLETE, onComplete);

                camera.addEventListener(Event.CANCEL,onCancel);

                mediaType = medType;

                camera.launch(MediaType.IMAGE);

           }

      }

       

       

      private function onCancel(event:Event):void{

           dispatchEvent(event);

      }

               

      private function onComplete(event:MediaEvent):void{

           var cameraUI:CameraUI = event.target as CameraUI;

           cameraUI.removeEventListener(MediaEvent.COMPLETE, onComplete);

           cameraUI.removeEventListener(Event.CANCEL, onCameraUICanceled);

                     

           mediaPromise = event.data;

                               

           if(mediaPromise.isAsync){

                //Async systems(IOS)          

                dataSource = mediaPromise.open();

                var eventSource:IEventDispatcher = dataSource as IEventDispatcher;

                eventSource.addEventListener(Event.COMPLETE, onMediaLoaded);

           }else{

                //Sync systems(android)

                file = mediaPromise.file;

                dispatchEvent(event);

           }              

      }

       

      private function onMediaLoaded(event:Event):void{

                     

           mediaBytes = new ByteArray();

           dataSource.readBytes(mediaBytes);             

       

           tempDir = File.createTempDirectory();

           var now:Date = new Date();

           var filename:String;

           filename = now.fullYear + now.month + now.day+now.hours + now.minutes + now.seconds + ".JPG";

           file = tempDir.resolvePath(filename);

       

           //writing temporal file to display image

           stream = new FileStream();

           stream.open(file,FileMode.WRITE);

           stream.writeBytes(mediaBytes);

           stream.close();

                     

           if(file.exists){

                //go to process to get BitmapData for Add to CameraRoll

                mpLoader = new Loader();

            mpLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,onMediaLoadedBitmapData);

                mpLoader.loadBytes(mediaBytes);

           }

      }   

       

      private function onMediaLoadedBitmapData(event:Event):void{

           var loaderInfo:LoaderInfo = LoaderInfo(event.target);

           bitmapData = new BitmapData(loaderInfo.width,loaderInfo.height,false,0xFFFFFF);

           bitmapData.draw(loaderInfo.loader);

                     

           //Image rotate process

           var matrix:Matrix = new Matrix();

           matrix.translate(-bitmapData.width / 2, -bitmapData.height / 2);

           matrix.rotate(90 * (Math.PI / 180));

           matrix.translate(bitmapData.height / 2, bitmapData.width / 2);

           var matriximage:BitmapData = new BitmapData(bitmapData.height, bitmapData.width, false, 0x00000000);

           matriximage.draw(bitmapData, matrix);    

           mediaBytes = matriximage.getPixels(matriximage.rect);                             

                     

           //I used the next code but doesn't work

           //var jpgencode:JPEGEncoder = new JPEGEncoder(85);

           //mediaBytes = jpgencode.encode(matriximage);

                     

           bitmapData = matriximage;

                     

           //Re-Writing image with rotate ByteArray

           stream.open(file,FileMode.WRITE);

           stream.writeBytes(mediaBytes);

           stream.close();

       

           //Add Rotate image to CameraRoll

           addingToCameraRoll();

      }

       

      public function addingToCameraRoll():void{

           var result:String="";

           if(CameraRoll.supportsAddBitmapData){

                if(bitmapData!=null){

                      roll = new CameraRoll();

                      roll.addEventListener(Event.COMPLETE,onCompleteAddBitMapData);

                      roll.addBitmapData(bitmapData);

                      var me:MediaEvent = new MediaEvent(MediaEvent.COMPLETE);

                      dispatchEvent(me);

                }

           }

      }

       

      When I run my app, at the time when the image is copied to CameraRoll prompts an error:

      Error #2044: Unhandled ErrorEvent:. text=Error #3004: Insufficient file space.

             at ....

       

       

      Debugging and doign some test I found that the size of ByteArray "mediaBytes.bytesAvailable" (1162439)  is shorter than "matriximage.getPixels(matriximage.rect)" (12582912),  the images that was saved in CameraRoll is in the correct way, but the second file (temporal file) is not saved, and I can use the file to show it in the screen.

       

      I want to know if exists another way to rotate and show and image in iOS or what I am doing wrong.