2016-10-12 4 views
1

사용자 정의 카메라 활동이 생성되었지만 캡처 된 이미지의 방향이 잘못되었습니다. 세로 모드에서 이미지를 캡처하고 90도 회전 한 후 원래 위치에 있지만 가로 방향으로 잘못된 방향으로 이미지 캡처 중입니다.사용자 정의 카메라를 사용하여 캡처 한 이미지의 방향이 잘못되었습니다.

카메라 방향 풍경에서 이미지를 캡처하는 동안. 해결 카메라 미리보기 문제에 대한 세로 모드

enter image description here

결과 이미지 enter image description here

에서 이미지를 캡처하는 동안 enter image description here

는 이미지를 enter image description here

카메라 방향을 결과.

public int setPhotoOrientation(Activity activity, int cameraId) { 
     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; 
     // do something for phones running an SDK before lollipop 
     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; 
     } 

     return result; 
    } 

비트 맵 방향을 확인하십시오.

private Bitmap imageOreintationValidator(Bitmap bitmap, String path) { 

     ExifInterface ei; 
     try { 
      ei = new ExifInterface(path); 
      int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
        ExifInterface.ORIENTATION_NORMAL); 
      switch (orientation) { 
      case ExifInterface.ORIENTATION_ROTATE_90: 
       bitmap = rotateImage(bitmap, 90); 
       rot++; 
       break; 
      case ExifInterface.ORIENTATION_ROTATE_180: 
       bitmap = rotateImage(bitmap, 180); 
       rot++; 
       break; 
      case ExifInterface.ORIENTATION_ROTATE_270: 
       bitmap = rotateImage(bitmap, 270); 
       rot++; 
       break; 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return bitmap; 
    } 

답변

0

마지막으로 발견 된 해결책. 방향 센서를 사용하여 방향을 확인하고 각도에 따라 각도를 찾고 그 각도를 사용하여 결과 이미지를 회전하십시오.

public abstract class SimpleOrientationListener extends OrientationEventListener { 

    public static final int CONFIGURATION_ORIENTATION_UNDEFINED = Configuration.ORIENTATION_UNDEFINED; 
    private volatile int defaultScreenOrientation = CONFIGURATION_ORIENTATION_UNDEFINED; 
    public int prevOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; 
    private Context ctx; 
    private ReentrantLock lock = new ReentrantLock(true); 

    public SimpleOrientationListener(Context context) { 
     super(context); 
     ctx = context; 
    } 

    public SimpleOrientationListener(Context context, int rate) { 
     super(context, rate); 
     ctx = context; 
    } 

    @Override 
    public void onOrientationChanged(final int orientation) { 


     int currentOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; 
     int angle=0; 
     if (orientation >= 330 || orientation < 30) { 
      currentOrientation = Surface.ROTATION_0; 
      angle=90; 
     } else if (orientation >= 60 && orientation < 120) { 
      currentOrientation = Surface.ROTATION_90; 
      angle=180; 
     } else if (orientation >= 150 && orientation < 210) { 
      currentOrientation = Surface.ROTATION_180; 
      angle=270; 
     } else if (orientation >= 240 && orientation < 300) { 
      currentOrientation = Surface.ROTATION_270; 
      angle=360; 
     } 
     getset g = new getset(); 
     g.setK(angle); 
     if (prevOrientation != currentOrientation && orientation != OrientationEventListener.ORIENTATION_UNKNOWN) { 
      prevOrientation = currentOrientation; 
      if (currentOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) 
       reportOrientationChanged(currentOrientation); 
     } 

    } 

    private void reportOrientationChanged(final int currentOrientation) { 

     int defaultOrientation = getDeviceDefaultOrientation(); 
     int orthogonalOrientation = defaultOrientation == Configuration.ORIENTATION_LANDSCAPE ? Configuration.ORIENTATION_PORTRAIT 
       : Configuration.ORIENTATION_LANDSCAPE; 

     int toReportOrientation; 

     if (currentOrientation == Surface.ROTATION_0 || currentOrientation == Surface.ROTATION_180) 
      toReportOrientation = defaultOrientation; 
     else 
      toReportOrientation = orthogonalOrientation; 

     onSimpleOrientationChanged(toReportOrientation); 
    } 

    /** 
    * Must determine what is default device orientation (some tablets can have default landscape). Must be initialized when device orientation is defined. 
    * 
    * @return value of {@link Configuration#ORIENTATION_LANDSCAPE} or {@link Configuration#ORIENTATION_PORTRAIT} 
    */ 
    private int getDeviceDefaultOrientation() { 
     if (defaultScreenOrientation == CONFIGURATION_ORIENTATION_UNDEFINED) { 
      lock.lock(); 
      defaultScreenOrientation = initDeviceDefaultOrientation(ctx); 
      lock.unlock(); 
     } 
     return defaultScreenOrientation; 
    } 

    /** 
    * Provides device default orientation 
    * 
    * @return value of {@link Configuration#ORIENTATION_LANDSCAPE} or {@link Configuration#ORIENTATION_PORTRAIT} 
    */ 
    private int initDeviceDefaultOrientation(Context context) { 

     WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
     Configuration config = context.getResources().getConfiguration(); 
     int rotation = windowManager.getDefaultDisplay().getRotation(); 

     boolean isLand = config.orientation == Configuration.ORIENTATION_LANDSCAPE; 
     boolean isDefaultAxis = rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180; 

     int result = CONFIGURATION_ORIENTATION_UNDEFINED; 
     if ((isDefaultAxis && isLand) || (!isDefaultAxis && !isLand)) { 
      result = Configuration.ORIENTATION_LANDSCAPE; 
     } else { 
      result = Configuration.ORIENTATION_PORTRAIT; 
     } 
     return result; 
    } 

    /** 
    * Fires when orientation changes from landscape to portrait and vice versa. 
    * 
    * @param orientation value of {@link Configuration#ORIENTATION_LANDSCAPE} or {@link Configuration#ORIENTATION_PORTRAIT} 
    */ 
    public abstract void onSimpleOrientationChanged(int orientation); 

} 

미리보기 용으로 카메라 방향을 설정합니다.

public static int 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; 
     //int currentapiVersion = android.os.Build.VERSION.SDK_INT; 
     // do something for phones running an SDK before lollipop 
     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; 
     } 

     return result; 

    } 
관련 문제