2011-08-17 27 views
4

我需要实现一个OrientationEventListener才能使相机正常工作。谷歌发布的onOrientationChanged样本实现看起来像这样:实现OrientationEventListener来帮助没有CameraInfo的摄像机问题?

@Override 
    public void onOrientationChanged(int orientation) { 
     if (orientation == ORIENTATION_UNKNOWN) return; 
    android.hardware.Camera.CameraInfo info = 
      new android.hardware.Camera.CameraInfo(); 
    android.hardware.Camera.getCameraInfo(cameraId, info); 
    orientation = (orientation + 45)/90 * 90; 
    int rotation = 0; 
    if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { 
     rotation = (info.orientation - orientation + 360) % 360; 
    } else { // back-facing camera 
     rotation = (info.orientation + orientation) % 360; 
    } 
    mParameters.setRotation(rotation); 
    } 

但我对建设API级别8,所以我没有CameraInfo。如何在没有CameraInfo的情况下完成与上述类似的操作?

+0

所以它只适用于背面相机?然后,只需使用'else'部分,就可以跳过有关'CameraInfo'部分的所有内容。 – bluefalcon

回答

1
public void onOrientationChanged(int orientation) { 
    if (orientation == ORIENTATION_UNKNOWN) return; 

    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; 
    } 

    parameters.setRotation(degrees); 
    mcamera.setParameters(parameters); 
} 

希望这会有所帮助。

编辑: 请参阅相机应用程序的源代码,以了解他们如何处理它。此链接是最新的来源。可以返回修订版本并查看2.2版本。
Camera source

编辑:这里是链接到附近的2.2版本的修订中的一个。搜索onOrientationChanged在这个文件:Camera src

+0

请参阅'Camera'应用程序源代码。此链接是最新的来源。可以返回修订版本并查看2.2版本。 http://android.git.kernel.org/?p=platform/packages/apps/Camera.git;a=tree;f=src/com/android/camera; – Ronnie

+0

希望帮助! – Ronnie

2

虽然方向改变,我发现我也必须改变相机预览匹配,如果你想旋转全屏幕图像,所以我使用类似的方法,但在onLayout(像这样的照相机表面视图的)方法:

/* 
* This class provides the surface for the camera. 
*/ 
public class CameraSurfaceView extends ViewGroup implements SurfaceHolder.Callback 
{ 

@Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) 
    { 
     if (changed) 
     {    

      final int width = right - left; 
      final int height = bottom - top; 

      int previewWidth = width; 
      int previewHeight = height; 
      if (mPreviewSize != null) 
      { 
       Display display = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 

       switch (display.getRotation()) 
       { 
        case Surface.ROTATION_0: 
         previewWidth = mPreviewSize.height; 
         previewHeight = mPreviewSize.width; 
         mCamera.setDisplayOrientation(90); 
         break; 
        case Surface.ROTATION_90: 
         previewWidth = mPreviewSize.width; 
         previewHeight = mPreviewSize.height; 
         break; 
        case Surface.ROTATION_180: 
         previewWidth = mPreviewSize.height; 
         previewHeight = mPreviewSize.width; 
         break; 
        case Surface.ROTATION_270: 
         previewWidth = mPreviewSize.width; 
         previewHeight = mPreviewSize.height; 
         mCamera.setDisplayOrientation(180); 
         break; 
       }          
      } 

      final int scaledChildHeight = previewHeight * width/previewWidth; 

      cameraView.layout(0, height - scaledChildHeight, width, height); 

     } 
    } 

}

这为我制作使用Android API 8的HTC欲望(Android 2.2的)。