2011-03-15 183 views
8

我有一個摩托羅拉Defy操作系統Android 2.1,我用相機預覽製作應用程序。問題在於,相機在Android 2.1上運行良好,但在摩托羅拉,相機旋轉90度。我試圖這樣做:Android相機旋轉

Parameters parameters = camera.getParameters(); 
parameters.setRotation(90); 

但它不工作。我還沒有找到任何解決方案。

+0

回答在座-http://stackoverflow.com/questions/14066038 /爲什麼圖像捕獲使用相機意圖獲取旋轉在一些設備在Android – 2016-03-11 13:59:21

+0

這[[答案](http://stackoverflow.com/a/40678737/2835520)可以幫助你 – IgniteCoders 2016-11-18 13:56:28

回答

15
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { 
     camera.setDisplayOrientation(90); 
     lp.height = previewSurfaceHeight; 
     lp.width = (int) (previewSurfaceHeight/aspect); 
    } else { 
     camera.setDisplayOrientation(0); 
     lp.width = previewSurfaceWidth; 
     lp.height = (int) (previewSurfaceWidth/aspect); 
    } 
+0

謝謝你的回答,但setDisplayOrientation只適用於Android 2.2或2.3,我的應用程序是在2.1中。 2.1還有另一種工作方式嗎?如果有人解決了這個問題,請幫助我。 – Laura 2011-03-17 12:26:51

+1

emm。我在2.1上測試了它的工作完美。我認爲你的意思是mediarRecord.setDisplayOrientation-這個支持在2.2或更高的版本 – Peter 2011-03-17 14:33:06

+0

Camera.setDisplayOrientation只支持API 8(2.2)參見http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation (int) – Ixx 2012-07-30 10:49:02

0

我認爲你不能進行任何設置來支持API 2.2到2.1。該API在您當前的設備庫中沒有。您必須更改爲2.2,支持API 8級。順便說一句,我也嘗試使用API​​級別7:

Parameters parameters = camera.getParameters(); 
parameters.setRotation(90); 

此功能以及對三星Galaxy Tab,但Nexus手機。三星Galaxy Tab使用OS 2.2.0,而Nexus one使用OS 2.2.1。當我嘗試使用API​​級別8時:

camera.setDisplayOrientation(90); 

兩者都很好。所以我認爲當我們在Android OS 2.2.1上使用時,API級別7有問題。

2

camera.setDisplayOrientation(int)在2.1下不存在!

而這種代碼可正常工作,但在我的里程碑/ DROID :(

Camera.Parameters parameters = camera.getParameters(); 
parameters.set("orientation", "portrait"); 
camera.setParameters(parameters); 

你可以看到更多的http://code.google.com/p/android/issues/detail?id=1193#c42

+0

我試過摩托羅拉Defy android 2.1,它不起作用。 – Derzu 2013-01-09 20:09:53

+0

這對我在我的橙色舊金山非常適合我。謝謝:) – jampez77 2013-10-27 20:03:45

2

我發現這個代碼,在Android 1.6的工作和故障轉移(適用於我使用2.1和肖像模式目前無預覽旋轉)

public void surfaceCreated(SurfaceHolder holder){ 
     try{ 
      camera = Camera.open(); 
      setDisplayOrientation(camera, 90); 
      camera.setPreviewDisplay(holder); 
      camera.startPreview(); 
     }catch(IOException e){ 
      Log.d("CAMERA", e.getMessage()); 
     } 

} 

protected void setDisplayOrientation(Camera camera, int angle){ 
     Method downPolymorphic; 
     try 
     { 
      downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class }); 
      if (downPolymorphic != null) 
       downPolymorphic.invoke(camera, new Object[] { angle }); 
     } 
     catch (Exception e1) 
     { 
     } 
} 

活動有機器人:screenOrientation =「畫像」上的AndroidManifest.xml

http://code.google.com/p/android/issues/detail?id=1193#c42

+0

我試過摩托羅拉Defy android 2.1,它不起作用。 – Derzu 2013-01-09 20:23:36

6

有在Android文檔的這個官方示例代碼現在(下setDisplayOrientation()):

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

什麼是int cameraId? – 2013-03-01 13:25:32

+1

@Fahad相機ID是指後置或前置攝像頭。 0表示面向後方,1表示面向前方。可能最好使用靜態實例變量'Camera.CameraInfo.CAMERA_FACING_BACK'和'Camera.CameraInfo.CAMERA_FACING_FRONT'。 – 2013-08-12 05:14:54

+0

@Timmmm JFYI,'camera.setDisplayOrientation'在Samsung Galaxy SII上不做任何事情。 – 2013-08-12 05:15:50

1
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); 
} 
+0

這實際上工作得很好,如果camerapreview擴展了一些視圖,那麼我們也需要將width/onLayout方法中的高度計算。 – radhoo 2014-05-12 12:51:25