2012-08-23 30 views
5

我为Android平板电脑编码,我希望我的应用使用surfaceView相机预览的纵向视图。这是默认的风景,我试着将下面的代码旋转到纵向视图:如何将Android相机更改为Surfaceview中的人像?

public void surfaceCreated(SurfaceHolder holder){ 
    // The Surface has been created, acquire the camera and tell it where to draw. 
    mCamera = Camera.open(); 
    Parameters params = mCamera.getParameters(); 
    // If we aren't landscape (the default), tell the camera we want portrait mode 
    if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE){ 
    params.set("orientation", "portrait"); // "landscape" 
    // And Rotate the final picture if possible 
    // This works on 2.0 and higher only 
    //params.setRotation(90); 
    // Use reflection to see if it exists and to call it so you can support older versions 
     try { 
     Method rotateSet = Camera.Parameters.class.getMethod("setRotation", new Class[] { Integer.TYPE }); 
     Object arguments[] = new Object[] { new Integer(270) }; 
     rotateSet.invoke(params, arguments); 
     } catch (NoSuchMethodException nsme) { 
     // Older Device 
     Log.v("CameraView","No Set Rotation"); 
     } catch (IllegalArgumentException e) { 
     Log.v("CameraView","Exception IllegalArgument"); 
     } catch (IllegalAccessException e) { 
     Log.v("CameraView","Illegal Access Exception"); 
     } catch (InvocationTargetException e) { 
     Log.v("CameraView","Invocation Target Exception"); 
     } 
    } 
    mCamera.setParameters(params); 
    try{ 
    mCamera.setPreviewDisplay(holder); 
    } catch (IOException exception) { 
    mCamera.release(); 
    mCamera = null; 
    } 
} 

但它不起作用。请问有人能修好吗?

+0

也尝试过使用Object arguments [] = new Object [] {new Integer(90)};但我仍然无法修复它。 –

+0

既然你得到了表面视图的工作,你可以给我一个例子。我需要从ipcamera流到我的android,我想使用surfaceview – Lily

回答

7

你可能会想使用setDisplayOrientation功能如下:

public void surfaceCreated(SurfaceHolder holder) { 
    if (Build.VERSION.SDK_INT >= 8) mCamera.setDisplayOrientation(90); 
} 

使用相机params.set("orientation"...东西是不是跨设备一致的,是真正的预SDK 8种语言。

相关问题