Using Camera on android
<p>In my library project I have the following code to initialize the camera, and start previewing.</p>

<pre><code>public class CameraView  {

private SurfaceView preview;
private SurfaceHolder previewHolder;
private Camera camera;
private boolean activeCamera;
private boolean cameraConfigured;
private Activity activity;
private final static int NINTY_DEGREES = 90; 


public CameraView(Activity activity) {
    this.activity = activity;

    View rootView;
    LayoutInflater inflater = activity.getLayoutInflater();
    rootView = inflater.inflate(R.layout.layout_cameraview, null);
    preview = (SurfaceView) rootView.findViewById(R.id.surfaceView);
    previewHolder = preview.getHolder();
    previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    cameraConfigured = false;
}

private void initializeCamera(int w, int h) {
    if(camera != null &amp;&amp; previewHolder.getSurface() != null) {

        try 
        {
            camera.setPreviewDisplay(previewHolder);
        }
        catch(Exception e)
        {
            Log.e("Camera: ", e.getMessage());
        }


        if(!cameraConfigured) {
            Camera.Parameters params = camera.getParameters();
            Camera.Size size = getBestPreviewSize(w, h, params);

            if(size != null) {
                params.setPreviewSize(size.width, size.height);
                cameraConfigured = true;
            }
        }
    }
}

public void startCamera() {
    camera = Camera.open();
    previewHolder.addCallback(surfaceCallback);
}

public void stopCamera() {
    stopPreviewingOnDisplay();
}

public void takePicture() {
    View rootView;
    LayoutInflater inflater = activity.getLayoutInflater();
    rootView = inflater.inflate(R.layout.layout_cameraview, null);
    Button capture = (Button) rootView.findViewById(R.id.capture);
    capture.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.e("CameraView: ", "Capture button pressed!");
        }
    });
}

private void startPreviewingOnDisplay() {
    if(camera != null) {
        camera.setDisplayOrientation(NINTY_DEGREES);
        camera.startPreview();
        activeCamera = true;
    }
}

private void stopPreviewingOnDisplay() {
    if(camera != null &amp;&amp; activeCamera) {
        camera.stopPreview();
        activeCamera = false;
    }
}

private SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {    
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        initializeCamera(width, height);
        startPreviewingOnDisplay();
        Log.d("CameraView: ", "SurfaceChanged");
    }
};

private Camera.Size getBestPreviewSize(int w, int h, Camera.Parameters params) {

    Camera.Size result = null;

    for(Camera.Size size : params.getSupportedPreviewSizes()) {
        if(size.width &lt; w &amp;&amp; size.height &lt;= h) {
            if(result == null) {
                result = size;
            }
            else {
                int resultArea = result.width * result.height;
                int newArea = size.width * size.height;

                if(newArea &gt; resultArea)
                    result = size;
            }
        }
    }       
    return(result);
}
</code></pre>

<p>}</p>

<p>The usage of this class: </p>

<pre><code>public class History extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
                ......
       camera = new CameraView(this);
    }

    @Override
    public void onResume() { 
       super.onResume();
       Log.i("History.class", "onResume called!");
       camera.startCamera();
    }

    @Override
    public void onPause() {
       super.onPause();
       camera.stopCamera();
    }
}
</code></pre>

<p>I've debugged the code, and I notice that the camera object is being intitalized, but there is nothing appearing on the screen, it should have previewed the camera. Could anyone see whats wrong with this approach? </p>

<p>Thanks. </p>
