简体   繁体   中英

Using Camera in Portrait Orientation

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?

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

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;
}

}

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM