2011-03-15 181 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