2013-04-15 72 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,因此如果任何人有比這更好的解決方案,請分享。

相關問題