2011-07-28 24 views
9

He visto problemas similares a los que estoy teniendo, pero todavía no puedo encontrar una solución. Soy muy nuevo en el desarrollo de Android, por lo que tengo problemas para rastrear mi problema. De todos modos, estoy tratando de crear una vista previa de la cámara usando una clase CameraSurfaceView que he creado que extiende SurfaceView e implementa SurfaceHolder.Callback. No importa lo que intento en mi clase de StartCamera, el método surfaceCreated() nunca se llama, por lo tanto, mi cámara nunca se inicia. ¡Cualquier ayuda sería genial, gracias!surfaceCreated() Nunca llamó

StartCamera.java

import net.peterd.zombierun.R; 
import android.hardware.Camera; 
import android.os.Bundle; 
import android.widget.FrameLayout; 

public class StartCamera extends BaseActivity { 

    private Camera mCamera; 
    private CameraSurfaceView mView; 

    public void onCreate(Bundle state) { 
     // TODO Auto-generated constructor stub 
     super.onCreate(state); 
     setContentView(R.layout.start_camera); 
    } 

    public void onResume() { 
     super.onResume(); 

     mView = new CameraSurfaceView(this); 
     FrameLayout preview = (FrameLayout) findViewById(R.id.cPreview); 
     preview.addView(mView); 
    } 

    public void onPause() { 
     mCamera.stopPreview(); 
     mCamera.release(); 
    } 

} 

CameraSurfaceView.java

import java.io.IOException; 
import android.content.Context; 
import android.hardware.Camera; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback 
{ 
     private SurfaceHolder holder; 
     private Camera camera; 

     public CameraSurfaceView(Context context) 
     { 
       super(context); 

       //Initiate the Surface Holder properly 
       this.holder = this.getHolder(); 
       this.holder.addCallback(this); 
       this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
     } 

     @Override 
     public void surfaceCreated(SurfaceHolder holder) 
     { 
       try 
       { 
         //Open the Camera in preview mode 
         this.camera = Camera.open(); 
         this.camera.setPreviewDisplay(this.holder); 
       } 
       catch(IOException ioe) 
       { 
         ioe.printStackTrace(System.out); 
       } 
     } 

     @Override 
     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
     { 
       // Now that the size is known, set up the camera parameters and begin 
       // the preview. 
       Camera.Parameters parameters = camera.getParameters(); 
       parameters.setPreviewSize(width, height); 
       camera.setParameters(parameters); 
       camera.startPreview(); 
     } 

     @Override 
     public void surfaceDestroyed(SurfaceHolder holder) 
     { 
       // Surface will be destroyed when replaced with a new screen 
       //Always make sure to release the Camera instance 
       camera.stopPreview(); 
       camera.release(); 
       camera = null; 
     } 

     public Camera getCamera() { 
      return camera; 
     } 
} 

start_camera.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <FrameLayout android:id="@+id/cPreview" 
     android:layout_weight="1" android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    </FrameLayout> 

</LinearLayout> 

Respuesta

6

Al parecer, esto se debe a FrameLayout estar vacío y por lo tanto no crear ningún tipo de SurfaceView . Puede intentar forzar el alto y el ancho de configuración lineal de LinearLayout manualmente o simplemente agregar un imageView dentro de FrameLayout. Como FrameLayout puede contener muchos widgets, pero solo muestra el último de ellos, puede colocar un ImageView dentro de FrameLayout mostrando una imagen pequeña solo para que se cree el SurfaceView.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<FrameLayout android:id="@+id/cPreview" 
    android:layout_weight="1" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

<!-- Adding small dummy image --> 
<ImageView android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    src="@drawable/small_picture"> 

</FrameLayout> 

</LinearLayout> 
+1

Estoy teniendo el mismo problema. ¿Alguien puede resolver esto? –

0

que tenían errores de esta línea en el método CameraSurfaceView, surfaceChanged:

camera.setParameters(parameters); 

que funcionaba bien una vez que comento que fuera.

1

@jvnane He intentado tu código, funciona, con un error menor.

surfaceChanged se invoca sin problema. ¿De qué manera crees que no funciona? Trate de añadir mensaje de depuración y verlo en LogCat:

@Override 
public void surfaceCreated(final SurfaceHolder holder) 
{ 
    Log.i("Camera Test", "It works!"); 
    try 
    { 
     //Open the Camera in preview mode 
     this.camera = Camera.open(); 
     this.camera.setPreviewDisplay(this.holder); 
    } 
    catch(final IOException ioe) 
    { 
     ioe.printStackTrace(System.out); 
    } 
} 

de errores menores: Camera objeto se libera en surfaceDestroyed por lo que no es necesario para liberarlo de nuevo en la actividad de . Además, debe llamar al super.onPause() al anular el método onPause().

5

He tenido el mismo problema. Para mí fue porque el tamaño de la Vista se estaba configurando a 0 y, por lo tanto, nunca se creó la superficie. Lo comprobé configurando un tamaño específico y, de repente, se llamaron las devoluciones de llamada. (Igual, aparentemente, se mantiene si la Vista está distribuida fuera de la pantalla).