package com.samles.camera.preview;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class CameraPreviewActivity extends Activity 
	implements SurfaceHolder.Callback {

	private SurfaceView surView;
	private SurfaceHolder surHolder;
	
	private Camera camera;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.main);

		surView = (SurfaceView)findViewById(R.id.surfaceview);
		
		surHolder = surView.getHolder();
		surHolder.addCallback(this);
		surHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}
	
	@Override
	public void surfaceCreated(SurfaceHolder holder) { 
		try {
			camera = Camera.open();
			camera.setPreviewDisplay(surHolder);
			camera.startPreview();
		}		
		catch (Exception e) {
		   Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
		}
	}
	
	@Override
	public void surfaceChanged(
			SurfaceHolder holder, int format, int width, int height) { }

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) { 
		camera.stopPreview();
		camera.release();
	}
}