【发布时间】:2015-02-15 16:21:47
【问题描述】:
我的整个安卓相机应用程序都处于纵向模式,所以当我打开相机时,我需要它自动以纵向模式打开。当我现在打开相机时,预览是横向的。如何将相机预览设置为以纵向模式打开,以便预览看起来正确?
【问题讨论】:
标签: java android android-camera orientation
我的整个安卓相机应用程序都处于纵向模式,所以当我打开相机时,我需要它自动以纵向模式打开。当我现在打开相机时,预览是横向的。如何将相机预览设置为以纵向模式打开,以便预览看起来正确?
【问题讨论】:
标签: java android android-camera orientation
您可以使用Android Developer 文档中的此方法来旋转相机预览。
public final void setDisplayOrientation(整数度)
设置预览显示的顺时针旋转度数。这 影响预览帧和快照后显示的图片。 此方法对于纵向模式应用程序很有用。注意 前置摄像头的预览显示在水平翻转之前 旋转,即图像沿中心反射 相机传感器的垂直轴。因此用户可以将自己视为 照镜子。
private void setCameraDisplayOrientation(Activity activity, int cameraId,
android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result = 0;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
我希望这会有所帮助。
【讨论】:
可能是因为你没有指定screenRotation试试:
android:screenOrientation="portrait"
或尝试以下方法:
在清单中添加方向属性
android:screenOrientation=["unspecified" | "behind" |
"landscape" | "portrait" |
"reverseLandscape" | "reversePortrait" |
"sensorLandscape" | "sensorPortrait" |
"userLandscape" | "userPortrait" |
"sensor" | "fullSensor" | "nosensor" |
"user" | "fullUser" | "locked"]
So in your case it will be
<activity android:name=".yourCameractivity"
....
android:screenOrientation="portrait"/>
【讨论】: