2013-04-15 67 views
2

让相机的参考,我这样做:的Nexus 7摄像机(?或所有的前摄像头)的镜像(倒)

@Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     Log.d(TAG, "Surface created!"); 
     mCamera = Camera.open(); 
     if (mCamera == null) { 
      // try to open front facing camera 
      try { 
       mCamera = Camera.open(0); 
      } catch (RuntimeException e) { 
       Toast.makeText(getApplicationContext(), 
         "No camera available!", Toast.LENGTH_LONG).show(); 
      } 
     } 
     try { 
      mCamera.setPreviewDisplay(mCameraHolder); 
      setCameraDisplayOrientation(CameraActivity.this, 0, mCamera); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      if (mCamera != null) 
       mCamera.release(); 
      mCamera = null; 
     } 
    } 

哪里setCameraDisplayOrientation是这样的(我的计算器在什么地方找到):

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

当我用Nexus 4拍照时,它有正确的方向。当我用我的Nexus 7试用它时,它只有一个前置摄像头,它被镜像(颠倒)(实际上它甚至没有这段代码,所以它没有什么我猜的)。

是否有任何可靠的方法来获取相机的方向,并使其工作?我已经搜索谷歌和Stackoverflow,但没有找到任何工作到目前为止。

+0

我很确定这是正常的(和预期的行为),并且所有用于视频聊天的“网络摄像头”都这样做。如果它们没有镜像,那么移动到屏幕左侧会使您的图像向右移动,并且会使用户有点迷失方向。图像是否存储在没有镜像的情况下? – 323go

+0

好吧,现在我不确定垂直是否是正确的。所以,当我拍摄我的脸时,它是颠倒的。 – damian

+0

嗯,好吧,那是*不*正确。我有nexi 4,7,10和三星标签。如果我今天下午晚些时候有空,我会检查一下。 – 323go

回答

1

这是我发现与Nexus 7以及我测试的多数其他设备(如HTC One M8,联想瑜伽,LG G4 Nexus 4,Note 4和Oneplus One)一起使用的解决方案。

camera.setDisplayOrientation(90);

但是,此解决方案不适用于翻转预览的Nexus 6,因此如果任何人有比这更好的解决方案,请分享。

相关问题