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");
*/
}