android camera的基本使用

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

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

public class MyCamera extends Activity{
	private SurfaceView surfaceView;
	Camera camera;
	boolean preview=false;
	private static final String TAG = "camera";
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		Window window=getWindow();
//		设置没有title
		requestWindowFeature(Window.FEATURE_NO_TITLE);
//		全屏显示
		window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager
				.LayoutParams.FLAG_FULLSCREEN);
		window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		setContentView(R.layout.main);
		surfaceView=(SurfaceView)this.findViewById(R.id.surfaceView);
		surfaceView.getHolder().addCallback(new SufaceListener());
		//下面设置surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前
		surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		surfaceView.getHolder().setFixedSize(176,144);//设置分辨率
	}
	private final class SufaceListener implements SurfaceHolder.Callback{
		
		
		public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){
			
		}
	
		public void surfaceCreated(SurfaceHolder holder){
			try{
				camera=Camera.open();
				Camera.Parameters parameters=camera.getParameters();
				WindowManager wm=(WindowManager)getSystemService(Context.WINDOW_SERVICE);
				Display display=wm.getDefaultDisplay();
				parameters.setPreviewSize(display.getWidth(),display.getHeight());
				//每秒3帧
				parameters.setPreviewFrameRate(3);
				parameters.setPreviewFormat(PixelFormat.JPEG);
				parameters.set("jpeg-quality", 85);
				parameters.setPictureSize(display.getWidth(), display.getHeight());
				camera.setParameters(parameters);
				camera.setPreviewDisplay(surfaceView.getHolder());//显示取景画面
				camera.startPreview();
				preview=true;
			}
			catch(Exception e){
				Log.e(TAG,e.toString());
				
			}
		}
		public void surfaceDestroyed(SurfaceHolder holder){
			if(camera!=null){
				if(preview)
					camera.stopPreview();
				camera.release();
				camera=null;
			}
		}
	
		
	}
	public boolean onKeyDown(int keyCode,KeyEvent event){
		if(camera!=null&&event.getRepeatCount()==0){
			switch(keyCode){
			case KeyEvent.KEYCODE_SEARCH:
				camera.autoFocus(null);//自动对焦
				break;
			case KeyEvent.KEYCODE_DPAD_CENTER: ;
			case  KeyEvent.KEYCODE_CAMERA:
				camera.takePicture(null,null, new PictureCallbackListener());
				break;
			}
		}
		return true;
	}
	public final class PictureCallbackListener implements Camera.PictureCallback{
		public void onPictureTaken(byte[] data,Camera canera){
			try{
				Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
				File file=new File(Environment.getExternalStorageDirectory(),"ljh.jpg");
				FileOutputStream outStream=new FileOutputStream(file);
				bitmap.compress(CompressFormat.JPEG, 100,outStream);
				outStream.close();
				//
				camera.stopPreview();
				camera.startPreview();
				preview=true;
			}
			catch(Exception e){
				Log.e(TAG,e.toString());
			}
		}
	}
	/*
	//调用系统自带的照相功能
	Intent intent=new Intent();
	intent.setAction("android.media.action.STILL_IMAGE_CAMERA");
		*/
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值