6

I'm trying to develop an app which uses the Camera. So far it's been working well, except that I'm unable to force the orientation to be "portrait". It seems to work well if I force all activities to "landscape", because the camera preview seems to fit in landscape.

Is there anyway to use the Camera in portrait mode?

paperclip
  • 650
  • 2
  • 8
  • 23

3 Answers3

6

Android devices v2.2 and above contain and API to rotate the display to portrait. Devices below 2.2 are landscape only. Your best bet is to detect if the device is 2.2 and rotate 90 degrees. Fall back on landscape for devices under 2.2. The good news is most Android devices are on 2.2 and above.

Check out my answer here for more info:

Camera is wrong unless keyboard is open

Community
  • 1
  • 1
Ryan Reeves
  • 10,209
  • 3
  • 42
  • 26
  • good knowledge, i could see lots of people having this problem without finding a solution. – owen gerig Nov 28 '11 at 18:12
  • You may want to add a setting in your app to allow users to force landscape. I've tested and it seems it's impossible to force portrait on some devices. So far I know these devices cannot be set to portrait, not even with Android 2.2+: "GT-S6102", "GT-S5360", "GT-S5660", "YP-G1", "YP-G70". I'm sure there are a lot more :/ – Jorge Cevallos Sep 07 '12 at 00:04
3
public void surfaceCreated(SurfaceHolder holder)
{
// The Surface has been created, acquire the camera and tell it where to draw.
mCamera = Camera.open();

Parameters params = mCamera.getParameters();

if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
{
params.set("orientation", "portrait");
mCamera.setDisplayOrientation(90);
}

try
{
mCamera.setPreviewDisplay(holder);
}
catch (IOException exception)
{
mCamera.release();
mCamera = null;
}

}
-1

edit: I was in the midst of Adobe AIR for Android development when I answered this question, and looking back at it, I realize this question didn't pertain to Adobe AIR.

Adobe says:

On devices that can change the screen orientation, such as mobile phones, a Video object attached to the camera will only show upright video in a landscape-aspect orientation. Thus, mobile apps should use a landscape orientation when displaying video and should not auto-rotate.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Camera.html

If you do really want to use the camera in portrait mode, my suggestion is to rotate the video object.

Here's some sample code that rotates the video object (_video) by an angle in degrees (source was pulled from elsewhere on stackoverflow):

        var matrix:Matrix = _video.transform.matrix; 
        var rect:Rectangle = _video.getBounds(this); 
        matrix.translate(- (rect.left + (rect.width/2)), - (rect.top + (rect.height/2))); 
        matrix.rotate((angle/180)*Math.PI); 
        matrix.translate(rect.left + (rect.width/2), rect.top + (rect.height/2));
        _video.transform.matrix = matrix;
Praveen
  • 331
  • 4
  • 15