拍照Test

package com;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import com.cgt.camera.R;
import com.lilin.util.GetSysDate;
import com.lilin.util.SDCardHelp;
import com.lilin.util.ShowMsg;

/**
 * 
 * @author lilin
 * @date 2011-9-3 下午12:22:10
 * @ClassName: Main
 * @Description: TODO
 */
public class TakePhotoView extends Activity implements SurfaceHolder.Callback,
		OnClickListener, PictureCallback {
	private SurfaceView surfaceView;
	private SurfaceHolder surfaceHolder;
	private Camera camera;// 相机
	private Camera.Parameters cameraParam;// 相机参数
	private Camera.Size cameraSize;// 相机大小

	private boolean isPreViewing = true;
	private boolean isHasSurface;
	byte[] data;
	Button take_btn;// 拍照
	ImageView preView_iv;// 预览

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.TRANSLUCENT);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 无标题
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);// 全屏
		setContentView(R.layout.takephotoview);
		initView();

	}

	// 界面创建时被调用,一般是用来,打开相机,设置预览
	public void surfaceCreated(SurfaceHolder holder) {
		if (camera == null) {
			camera = Camera.open();// 不能放在构造函数里,否则产生不了预览效果
			try {
				camera.setPreviewDisplay(holder);
			} catch (IOException e) {
				e.printStackTrace();
			}
			isHasSurface = true;
		}
	}

	// 在界面尺寸改变时被调用,设置相机的参数(大小,格式)
	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
		if (isPreViewing) {
			isPreViewing = false;
			camera.stopPreview();
		}
		cameraParam = camera.getParameters();
		// 这项设置可以解决部分机型报setPreview()的错
		cameraParam.setPreviewSize(w, h);
		cameraParam.setPictureFormat(PixelFormat.JPEG);// 图片格式
		camera.setParameters(cameraParam);// 设置参数
		if (!isPreViewing) {
			camera.startPreview();
			isPreViewing = true;
		}
	}

	// 捕捉相机拍到的图片
	public void onPictureTaken(byte[] _data, Camera camera) {
		// 得到图片data
		Bitmap bitmap = BitmapFactory.decodeByteArray(_data, 0, _data.length);
		savepicture(bitmap, GetSysDate.getFormateDate(1));// 保存图片
		if (!isPreViewing) {
			camera.startPreview();
			isPreViewing = true;
		} else {
			isPreViewing = false;
			camera.stopPreview();
			camera.startPreview();// 开始预览
			isPreViewing = true;
		}
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		if (camera != null) {
			if (isPreViewing) {
				camera.stopPreview();
				isPreViewing = false;
			}
			camera.release();
			camera = null;
		}
		isHasSurface = false;
	}

	// 保存图片
	public void savepicture(Bitmap bitmap, String formateDate) {
		try {
			if (!SDCardHelp.getFileSavePath(getString(R.string.pic_savepath))
					.toString().equals(null)) {
				File file = new File(SDCardHelp
						.getFileSavePath(getString(R.string.pic_savepath)),
						formateDate + ".jpg");// 创建图片文件
				FileOutputStream outStream = new FileOutputStream(file);
				bitmap.compress(CompressFormat.JPEG, 100, outStream);
				outStream.close();
				take_btn.setEnabled(true); // 设置拍照按钮可用
				take_btn.setBackgroundResource(R.drawable.take_picture1);
			} else {
				ShowMsg.showToast(getApplicationContext(), "请插入内存卡!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 获取最佳预览视图
	public Camera.Size getBestPreviewSize(Camera.Parameters parameters, int w,
			int h) {
		Camera.Size result = null;
		for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
			if (size.width <= w && size.height <= h) {
				if (null == result)
					result = size;
				else {
					int resultDelta = w - result.width + h - result.height;
					int newDelta = w - size.width + h - size.height;
					if (newDelta < resultDelta)
						result = size;
				}
			}
		}
		return result;
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.takepic_btn:// 拍照
			// 自动对焦
			camera.takePicture(null, null, this);
			take_btn.setEnabled(false);
			take_btn.setBackgroundResource(R.drawable.take_picture2);
			break;
		default:
			break;
		}
	}

	public void initView() {
		surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
		take_btn = (Button) findViewById(R.id.takepic_btn);
		preView_iv = (ImageView) findViewById(R.id.myImageView);
		take_btn.setOnClickListener(this);
	}

	protected void onPause() {
		System.out.println("onpause");
		super.onPause();
	}

	protected void onResume() {
		surfaceHolder = surfaceView.getHolder();
		surfaceHolder.addCallback(this);
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		super.onResume();
	}

}

你可能感兴趣的:(test)