package com.samples.camera.takepicture;

import android.app.Activity;
import android.os.Bundle;

import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;
import android.widget.Toast;

public class CameraPreviewActivity extends Activity 
		implements SurfaceHolder.Callback, View.OnClickListener {

	private ImageButton bStart;
	private ImageButton bStop;	
	private ImageButton bTake;	
	
	private SurfaceView surView;
	private SurfaceHolder surHolder;
	
	private Camera camera;
	private boolean isCameraPreview = false;
	
    private ShutterCallback shutter = new ShutterCallback(){
		@Override
		public void onShutter() { }
	};

	private PictureCallback raw = new PictureCallback(){
		@Override
		public void onPictureTaken(byte[] arg0, Camera arg1) { }
	};
	
	PictureCallback jpg = new PictureCallback(){
		@Override
		public void onPictureTaken(byte[] arg0, Camera arg1) {
			Bitmap bitmapPicture
				= BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
			camera.stopPreview();
			camera.release(); 
			isCameraPreview = false;
				
			bStart.setEnabled(!isCameraPreview);
			bStop.setEnabled(isCameraPreview);
		 }
	};
		
	@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);
	  
	    LayoutInflater inflater = LayoutInflater.from(getBaseContext());
	    View overlay = inflater.inflate(R.layout.overlay, null);
	    LayoutParams params = new LayoutParams(
	    		LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
	    addContentView(overlay, params);
	    
		bStart = (ImageButton)overlay.findViewById(R.id.bStart);
		bStop = (ImageButton)overlay.findViewById(R.id.bStop);
		bTake = (ImageButton)overlay.findViewById(R.id.bTake);
		
		bStart.setOnClickListener(this);     
		bStop.setOnClickListener(this); 
		bTake.setOnClickListener(this);
		
		bTake.setEnabled(!isCameraPreview);
		bStop.setEnabled(!isCameraPreview);
	}
	
	@Override
	public void surfaceChanged(
			SurfaceHolder holder, int format, int width, int height) { }

	@Override
	public void surfaceCreated(SurfaceHolder holder) { }

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

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bStart:
			try {
				camera = Camera.open();
				camera.setPreviewDisplay(surHolder);
				camera.startPreview();
				isCameraPreview = true;
						
				bStart.setEnabled(!isCameraPreview);
				bStop.setEnabled(isCameraPreview);
			} 
			catch (IOException e) {
			   Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
			}
			break;
		case R.id.bTake:
			camera.takePicture(shutter, raw, jpg);
			break;	
		case R.id.bStop:
			camera.stopPreview();
			camera.release(); 
			isCameraPreview = false;
				
			bStart.setEnabled(!isCameraPreview);
			bStop.setEnabled(isCameraPreview);
			break;
		}		
	}
}