我有一个使用Android设备相机的应用程序。拍摄照片的活动无法旋转,仅以纵向显示。 在大多数设备上,此代码可以正常工作:
int degrees = 0;
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;
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (cameraInfo.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (cameraInfo.orientation - degrees + 360) % 360;
} camera.setDisplayOrientation(result);
但是在设备上(DMTECH 725H,7英寸平板电脑,只有前置摄像头),预览会颠倒显示。有任何想法如何解决吗?
答案 0 :(得分:1)
试试这个:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
boolean isLandscape = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE;
int degrees = 0;
switch (mDisplay.getRotation()){
case Surface.ROTATION_0:
degrees = isLandscape? 0 : 90;
break;
case Surface.ROTATION_90:
degrees = isLandscape? 0 : 270;
break;
case Surface.ROTATION_180:
degrees = isLandscape? 180 : 270;
break;
case Surface.ROTATION_270:
degrees = isLandscape? 180 : 90;
break;
}
cameraManager.rotateDisplay(degrees, isLandscape);
}
答案 1 :(得分:1)
如this support issue中所述,相机旋转错误的原因可能是传感器的方向不正常。您还应该使用camera2
包而不是android.hardware.Camera
。
答案 2 :(得分:0)
这可能不是完美的解决方案。但请参考它,希望它适合你:)
android.hardware.Camera.setDisplayOrientation
http://developer.android.com/reference/android/hardware/Camera.Parameters.html
和
android.hardware.Camera.Parameters.setRotation
http://developer.android.com/reference/android/hardware/Camera.html