2012-08-23 32 views
5

Estoy codificando para una tableta android y quiero que mi aplicación use la vista de retrato de la vista previa de la cámara surfaceView. Es el paisaje de forma predeterminada, y me trató el siguiente código para girar a la vista vertical:cómo cambiar la cámara Android al retrato en la vista de superficie?

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

Pero no funciona. ¿Alguien podría arreglarlo por favor?

+0

también se intentó con los argumentos de objeto [] = new Object [] {new Integer (90)}; pero aun así no pude arreglarlo. –

+0

Dado que tienes la SurfaceView para trabajar, ¿puedes darme un ejemplo? Necesito transmitir desde una ipcamera a mi android y quiero usar Surfaceview – Lily

Respuesta

7

es probable que desee utilizar la función setDisplayOrientation de la siguiente manera:

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

Utilizando el material de cámara params.set("orientation"... no es consistente a través de dispositivos y es realmente pre-SDK 8 idiomas.

Cuestiones relacionadas