Android5.1.1 自带Camera2/SnapdragonCamera的apk拍照方向旋转90°

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LEAD_SOLO/article/details/52056722

实际上这个原因是因为机器里没有重力感应器造成的。
根据Camera2/src/com/android/camera/PhotoModule.java 的代码查看原因。

160     // The degrees of the device rotated clockwise from its natural orientation.
<span style="white-space:pre">	</span>// 设备的角度旋转按顺时针旋转的自然方向
161     private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;   

………………

1298         // Set JPEG orientation. Even if screen UI is locked in portrait, camera orientation should
1299         // still match device orientation (e.g., users should always get landscape photos while
1300         // capturing by putting device in landscape.)
1301         int orientation = mActivity.isAutoRotateScreen() ? mDisplayRotation : mOrientation;//是否自动旋转的屏幕?                                                                                 
1302         Characteristics info = mActivity.getCameraProvider().getCharacteristics(mCameraId);
1303         mJpegRotation = info.getJpegOrientation(orientation);//利用orientation获得Jpeg的方向
1304         mCameraDevice.setJpegOrientation(mJpegRotation);//设置输出的Jpeg方向

可以得知这里是和设置角度有关的,可是查找全篇得知mOrientation只在如下代码片段修改

1347     @Override
1348     public void onOrientationChanged(int orientation) {
1349         if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {            
1350             return;
1351         }
1352 
1353         // TODO: Document orientation compute logic and unify them in OrientationManagerImpl.
1354         // b/17443789
1355         // Flip to counter-clockwise orientation.
1356         mOrientation = (360 - orientation) % 360;
1357     }
这个函数在由方向传感器发生变化后才触发,实际上是由PhotoModule继承的接口SensorEventListener有关。

 101 public class PhotoModule
 102         extends CameraModule
 103         implements PhotoController,
 104         ModuleController,
 105         MemoryListener,
 106         FocusOverlayManager.Listener,
 107         SensorEventListener,                                                          
 108         SettingsManager.OnSettingChangedListener,
 109         RemoteCameraModule,
 110         CountDownView.OnCountDownStatusListener 
因此在没有开启自动旋转或者没有方向传感器的时候可以将

--   private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
++      private int mOrientation = 270;

搞定!

猜你喜欢

转载自blog.csdn.net/LEAD_SOLO/article/details/52056722