android – 相机预览颠倒了

我有一个使用
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英寸平板电脑,只有前置摄像头),预览会颠倒显示.有任何想法如何解决?

最佳答案 试试这个:

@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);
}
点赞