2016-02-15 24 views
10

我是Android開發中的新手,所以如果我的問題微不足道,我很抱歉。在我的應用程序的一部分,我需要我的後置攝像頭的實時預覽,所以我創建了一個自定義類,它擴展了SurfaceView並實現了SurfaceHolder.Callback(我基本上遵循了android文檔中的說明)。Nexus 5x反向風景傳感器修復安卓相機預覽應用程序

不幸的是,我在Nexus 5x中測試我的應用程序,我剛剛意識到它已經以相反的方式安裝了相機傳感器。出於這個原因,在我的Nexus 5x上運行時,我的應用程序的相機預覽看起來顛倒了,這是我不想要的。

看來,新的android.hardware.camera2 API能夠自動處理這個問題。最終,我需要使用這個新API更新我的所有代碼,但現在我需要的是使用舊相機API時的快速修復。

所以我在那裏讀書,我發現了一段代碼,我需要在SurfaceChanged方法中介紹以解決此問題。這是它:

Display display = ((WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 

     if(display.getRotation() == Surface.ROTATION_0) 
     { 
      parameters.setPreviewSize(capHeight, capWidth);       
      camera.setDisplayOrientation(90); 
     } 

     if(display.getRotation() == Surface.ROTATION_90) 
     { 
      parameters.setPreviewSize(capWidth, capHeight);       
     } 

     if(display.getRotation() == Surface.ROTATION_180) 
     { 
      parameters.setPreviewSize(capHeight, capWidth);    
     } 

     if(display.getRotation() == Surface.ROTATION_270) 
     { 
      parameters.setPreviewSize(capWidth, capHeight); 
      camera.setDisplayOrientation(180); 
     } 

     camera.setParameters(parameters);*/ 

     camera.startPreview(); 

問題是,我沒有看到有什麼改變。

有什麼想法?

回答

0

同樣在這裏。 因爲camera2不支持API 21的設備<,並且不想實現這兩個API,所以很快就會得到一個快速修復。

我試圖像財產以後:

if (Build.MODEL.equals("Nexus 5x")){ 
    // rotate camera 180° 
    mCamera.setDisplayOrientation(180); 
} 

但是不能旋轉攝像頭的顯示。要檢查是否有什麼與PreviewCallback

使用2 apis是不好的事情? 正如在這個主題中,它不推薦: How to use Android's camera or camera2 API to support old and new API versions without deprecation notes?

+1

Nexus 6P怎麼樣? – innich

9

5X相機不是「反向」;它在其參數報告正確相機方向,所以不需要特殊的解決辦法,只是要確保你正確設置顯示方向:

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

http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation%28int%29

+4

這對於預覽是正確的,但是當您想要處理拍攝的圖像時,您仍然需要考慮相機旋轉度爲270,而在大多數設備上相機的旋轉度爲90(表示圖像顛倒)。 –

+5

@EmanuelMoecklin是的上述代碼修復了相機顯示,但顯示的圖像仍然顛倒,我該如何解決這個問題? – user2056563

+0

我使用OpenCV4Android旋轉了圖像 – Heribert

3
if (Build.MODEL.equals("Nexus 5X")){ 
    // rotate camera 180° 
    mCamera.setDisplayOrientation(180); 
} 

變化5倍到5倍會好的。