package com.view;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.Buffer;
import java.nio.ByteBuffer;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.ImageView;

public class CameraView extends Activity implements SurfaceHolder.Callback{

	private SurfaceView camera;
	private SurfaceHolder cmr_holder;
	private Camera cmr;
	private boolean isRunning=false;
	private Bitmap pic;
	
	private static final String PIC_PREFIX="EventPicture";
	private static final String PIC_EXTENSION=".bmp";
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		if(isRunning)
			cmr.stopPreview();
		Camera.Parameters para=cmr.getParameters();
		para.setPreviewSize(width, height);
		cmr.setParameters(para);
		try{
			cmr.setPreviewDisplay(holder);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		cmr.startPreview();
		isRunning=true;
	}

	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		cmr=Camera.open();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		cmr.stopPreview();
		isRunning=false;
		cmr.release();
	}
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.TRANSLUCENT);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.setContentView(R.layout.cameraview);
		
		camera=(SurfaceView)findViewById(R.id.surface_camera);
		cmr_holder=camera.getHolder();
		cmr_holder.addCallback(this);
		cmr_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		
	}
	
	Camera.PictureCallback pc=new Camera.PictureCallback() {
		
		public void onPictureTaken(byte[] data, Camera camera) {
			// TODO Auto-generated method stub
			pic=BitmapFactory.decodeByteArray(data, 0, data.length);
			
			
		}
	};
	
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuItem shot=menu.add("Shot");
		MenuItem save=menu.add("Save");
		MenuItem rtrn=menu.add("Return");
		
		return super.onCreateOptionsMenu(menu);
		
	}
	
	private final OnMenuItemClickListener click_shot=new OnMenuItemClickListener()
	{
		public boolean onMenuItemClick(MenuItem item)
		{
			cmr.takePicture(null, null, pc);
			ImageView iv_pic=new ImageView(CameraView.this);
			iv_pic.setImageBitmap(pic);
			CameraView.this.setContentView(iv_pic);
			return true;
		}
	};
	
	private final OnMenuItemClickListener click_return=new OnMenuItemClickListener()
	{
		public boolean onMenuItemClick(MenuItem item)
		{
			setResult(RESULT_OK);
			finish();
			return true;
		}

	};
	private final OnMenuItemClickListener click_save=new OnMenuItemClickListener()
	{
		public boolean onMenuItemClick(MenuItem item)
		{
			File saveDir=Environment.getExternalStorageDirectory();
			File picDir;
			if(saveDir.exists())
			{
				try {
					picDir=saveDir.createTempFile(PIC_PREFIX, PIC_EXTENSION, saveDir);
					//BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(picDir)));
					FileOutputStream out=new FileOutputStream(picDir);
					ByteBuffer picBuffer = null;pic.copyPixelsToBuffer(picBuffer);
					byte[] picBytes = new byte[picBuffer.capacity()];
					picBuffer.get(picBytes);
					out.write(picBytes);
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			return true;
		}
	};

}
