Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

surfaceCreated is not called

I'm trying to put a camera preview (SurfaceView) together with a button on the display, but all I get is a blank screen only showing the button. If I'm setting the SurfaceView as the only content (with setContentView(surfaceView) then the preview is displaying fine on the screen. What am I doing wrong?

Thanks in advance

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="0px"
        android:layout_weight="1">
        <SurfaceView
            android:id="@+id/camera_surface"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
        <Button 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:text="@string/trigger"
            android:id="@+id/trigger_picture_button" 
            android:layout_gravity="bottom" />
    </FrameLayout>
</LinearLayout>

My Activity:

public class CameraActivity extends Activity {

    private SurfaceView cameraPreview;
    private Button triggerPicture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);

        cameraPreview = (SurfaceView) findViewById(R.id.camera_surface);
        triggerPicture = (Button) findViewById(R.id.trigger_picture_button);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        cameraPreview = new CameraPreview(this);
//      setContentView(cameraPreview);
    }

My CameraPreview:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder holder;
private Camera camera;

public CameraPreview(Context context) {
    super(context);
    holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
    camera = openFrontFacingCamera();
    try {
        camera.setPreviewDisplay(holder);
    } catch (IOException e) {
        camera.release();
        camera = null;
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
    camera = null;
}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Camera.Parameters parameters = camera.getParameters();
    parameters.setPreviewSize(width, height);
    parameters.set("orientation", "portrait");
    camera.setParameters(parameters);
    camera.startPreview();
}

private Camera openFrontFacingCamera() {
    // returns Camera.open(
}
like image 758
Hans Muff Avatar asked May 06 '11 13:05

Hans Muff


People also ask

What is SurfaceView?

Provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen.

What is Surfaceholder callback?

An implementation of SurfaceView that uses the dedicated surface for displaying OpenGL rendering. NativeActivity. Convenience for implementing an activity that will be implemented purely in native code.


1 Answers

When you declare SurfaceView in the layout you tell Android to use built-in class. What you need to do is make it use your class by replacing SurfaceView in XML with your.package.name.CameraPreview.

One other thing: you should add a constructor to the CameraPreview taking parameters Context and AttributeSet, otherwise layout inflater won't be able to inflate your class. Also you don't need the line cameraPreview = new CameraPreview(this);.

like image 200
Malcolm Avatar answered Sep 18 '22 02:09

Malcolm