Camera의 setDisplayOrientation 메소드

Posted by epicdev Archive : 2011. 10. 3. 15:52
출처: http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation(int)

public final void setDisplayOrientation (int degrees)

Since: API Level 8

Set the clockwise rotation of preview display in degrees. This affects the preview frames and the picture displayed after snapshot. This method is useful for portrait mode applications. Note that preview display of front-facing cameras is flipped horizontally before the rotation, that is, the image is reflected along the central vertical axis of the camera sensor. So the users can see themselves as looking into a mirror.

This does not affect the order of byte array passed in onPreviewFrame(byte[], Camera), JPEG pictures, or recorded videos. This method is not allowed to be called during preview.

If you want to make the camera image show in the same orientation as the display, you can use the following code.

 public static void 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;
     
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;
     
}
     camera
.setDisplayOrientation(result);
 
}
 

Parameters
degreesthe angle that the picture will be rotated clockwise. Valid values are 0, 90, 180, and 270. The starting position is 0 (landscape).