Android编程中调用Camera时预览画面有旋转问题的解决方法

2019-12-10 19:52:46于海丽

易采站长站为您分析Android编程中调用Camera时预览画面有旋转问题的解决方法,涉及Android针对Camera调用摄像头源码部分的相关修改技巧,需要的朋友可以参考下

本文实例讲述了Android编程中调用Camera时预览画面有旋转问题的解决方法。,具体如下:

在调用Camera写应用的时候,前后摄像头的情况有时候是不一样的。有时候,明明后摄像头没有问题,而调用到前摄像头时,却倒转了180°,或者其他角度,百思不得其解。在查看了Android源码之后,发现它的解决办法很是好,接下来贴个源码,以备日后查看。

 

 
  1. public static int getDisplayRotation(Activity activity) {  int rotation = activity.getWindowManager().getDefaultDisplay() 
  2. .getRotation();  switch (rotation) { 
  3. case Surface.ROTATION_0: return 0;  case Surface.ROTATION_90: return 90; 
  4. case Surface.ROTATION_180: return 180;  case Surface.ROTATION_270: return 270; 
  5. }  return 0; 
  6. }  public static void setCameraDisplayOrientation(Activity activity, 
  7. int cameraId, Camera camera) {  // See android.hardware.Camera.setCameraDisplayOrientation for 
  8. // documentation.  Camera.CameraInfo info = new Camera.CameraInfo(); 
  9. Camera.getCameraInfo(cameraId, info);  int degrees = getDisplayRotation(activity); 
  10. int result;  if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
  11. result = (info.orientation + degrees) % 360;  result = (360 - result) % 360; // compensate the mirror 
  12. } else { // back-facing  result = (info.orientation - degrees + 360) % 360; 
  13. }  camera.setDisplayOrientation(result);