Wednesday, May 16, 2012

iOS camera's image rotation got in AS3

I'm developing a non-native iOS App using AS3 over Flash CS5.5. The app has the feature of taking photos with both cameras (obviously one at any time, not at the same time), and my problem consists in I don't know how to deal with the rotation of the image i'm seeing on my device.



I'm looking for a solution but don't have any which solves my problem.



Here is my code:



package
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.ActivityEvent;
import flash.events.MouseEvent;
import flash.media.Camera;
import flash.media.Video;

public class Main extends Sprite
{

private var cam:Camera;
private var vid:Video;


public function Main()
{
super();

// support autoOrients
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
cam = Camera.getCamera();

if (!cam)
{
trace("No camera is installed.");
}
else
{
connectCamera();
}
}

private function connectCamera():void
{
cam.setMode(320, 480, 25);
cam.setQuality(0,100);
vid = new Video();
vid.width = cam.width;
vid.height = cam.height;
vid.attachCamera(cam);
addChild(vid);
}
}
}





First of all thanks for your time. I've already updated the autoOrientation settings, and this is the code I'm managing:



    package 
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.ActivityEvent;
import flash.events.MouseEvent;
import flash.media.Camera;
import flash.media.Video;
import flash.events.StageOrientationEvent;
import flash.display.StageOrientation;


public class Main2 extends Sprite
{

private var cam:Camera;
private var vid:Video;
public var _currentOrientation:String;




public function Main2()
{
cam = Camera.getCamera();

if (! cam)
{
trace("No camera is installed.");
}
else
{
connectCamera();
}
stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING, orientationChangeListener);
}

private function connectCamera():void
{
cam.setMode(480, 320, 25);
cam.setQuality(0,100);
vid= new Video();
vid.width = cam.width;
vid.height = cam.height;
vid.attachCamera(cam);
addChild(vid);

}

private function orientationChangeListener(e:StageOrientationEvent):void
{
switch (e.afterOrientation)
{
case StageOrientation.DEFAULT :
_currentOrientation = "DEFAULT";
//set rotation value here
stage.rotation = 0;
break;

case StageOrientation.ROTATED_RIGHT :
_currentOrientation = "ROTATED_RIGHT";
//set rotation value here
stage.rotation = -90;
break;

case StageOrientation.ROTATED_LEFT :
_currentOrientation = "ROTATED_LEFT";
//set rotation value here
stage.rotation = 90;
break;

case StageOrientation.UPSIDE_DOWN :
_currentOrientation = "UPSIDE_DOWN";
//set rotation value here
stage.rotation = 180;
break;
}
}


}
}


Despite of the switch-case sentences, when you launch the "app" you see the image of the camera 90degrees rotated, and when you put the device (iPhone 4) to landscape (right or left) the image puts well. And when you rotate back, the image gets bad as the launching one.



Thank you in advance.



PS: Sorry for my English.
PS2: Edited.





No comments:

Post a Comment