android中Camera setDisplayOrientation使用

在写相机相关应用的时候遇到捕获的画面方向和手机的方向不一致的问题,比如手机是竖着拿的,但是画面是横的,这是由于摄像头默认捕获的画面byte[]是根据横向来的,而你的应用是竖向的,解决办法是调用setDisplayOrientation来设置PreviewDisplay的方向,效果就是将捕获的画面旋转多少度显示。
设置 preview 的顺时针旋转角度。这将影响 preview frames和拍照之后的相片显示。该方法主要用于垂直模式的应用。注意在旋转之前, front-facing cameras 的 preview显示是水平 flip 的,这就是说, image 是沿着 camera sensor 的垂直中心轴来反射的。所以用户可以像照镜子一样看到他们自己。这不会影响传入函数 onPreviewFrame(byte[], Camera) 的、JPEG 相片的、或记录的 video 的 byte array 的顺序,你可以自己做旋转处理。在preview 期间是不允许调用该方法的。如果你想要是你的照片和显示出来的角度一致,你可以参考下列代码:

 
 1 public static void setCameraDisplayOrientation (Activity activity, int cameraId, android.hardware.Camera camera) {  
 2     android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();  
 3     android.hardware.Camera.getCameraInfo (cameraId , info);  
 4     int rotation = activity.getWindowManager ().getDefaultDisplay ().getRotation ();  
 5     int degrees = 0;  
 6     switch (rotation) {  
 7         case Surface.ROTATION_0:  
 8             degrees = 0;  
 9             break;  
10         case Surface.ROTATION_90:  
11             degrees = 90;  
12             break;  
13         case Surface.ROTATION_180:  
14             degrees = 180;  
15             break;  
16         case Surface.ROTATION_270:  
17             degrees = 270;  
18             break;  
19     }  
20     int result;  
21     if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {  
22         result = (info.orientation + degrees) % 360;  
23         result = (360 - result) % 360;   // compensate the mirror  
24     } else {  
25         // back-facing  
26         result = ( info.orientation - degrees + 360) % 360;  
27     }  
28     camera.setDisplayOrientation (result);  
29 }  

 

转租:http://blog.csdn.net/fengye810130/article/details/11614427

posted @ 2016-03-25 14:36  鸭子船长  阅读(2259)  评论(0编辑  收藏  举报