views:

3174

answers:

1

Hi,

I am trying to develop a custom widget for camera preview. I would like to fill the screen with camera preview and draw some buttons over it.

I have tried to create a custom widget the following way:

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

class CapturePreview extends SurfaceView implements SurfaceHolder.Callback {
    SurfaceHolder mHolder;
    Camera mCamera;

CapturePreview(Context context) {
    super(context);
    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, acquire the camera and tell it where
    // to draw.
    mCamera = Camera.open();
    try {
       mCamera.setPreviewDisplay(holder);
    } catch (IOException exception) {
        mCamera.release();
        mCamera = null;
        // TODO: add more exception handling logic here
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // Surface will be destroyed when we return, so stop the preview.
    // Because the CameraDevice object is not a shared resource, it's very
    // important to release it when the activity is paused.
    mCamera.stopPreview();
    mCamera = null;
}

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

}

and modified the main.xml file the following way:

<RelativeLayout android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/RelativeLayout">
<dev.video.client.CapturePreview android:id="@+id/capturePreview" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</RelativeLayout>

In the Activity class I have tried to bind the widget the following way:

private CapturePreview mPreview;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setFullscreen();

    setContentView(R.layout.main);

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    //mPreview = new CapturePreview(this);
    //setContentView(mPreview);

    mPreview = (CapturePreview)this.findViewById(R.id.capturePreview);
}

Every time I try to debug the application I get an error message and I haven't found out what could be wrong.

I would really appreciate any help regarding this problem.

Thanks!

+1  A: 

You need to create the following constructor in SurfaceView for this to work:

CapturePreview(Context context, AttributeSet attrs)

As you might know, in Java constructors aren't inherited from it's superclass, yet this is the constructor that Android tries to invoke when you use that component in an XML file (as opposed to defining your GUI programmatically). That won't work, of course, hence the error message, which should be a lot more clearer, in my opinion.

The AttributeSet contains every attribute mentioned in the XML declaration, so if you have custom attributes for your component, you can retrieve and take actions on them in the constructor.

Jaap-Jan