调用系统相机拍摄照片的时候,如何能连续拍摄一组照片,而不是点击确定之后就返回

Java code
    
    
    
    
public class MainActivity extends Activity { // 声明拍照界面组件 private SurfaceView surfaceView; // 声明界面控制组件 private SurfaceHolder surfaceHolder; // 声明照相机 private Camera camera; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); // 获取拍照界面组件 surfaceView = (SurfaceView)findViewById(R.id.preview); // 获取界面控制组件 surfaceHolder = surfaceView.getHolder(); // 界面控制组件回调,处理打开相机,关闭相机及照片尺寸改变 Callback surfaceCallback = new Callback() { @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub // 停止预览 camera.stopPreview(); // 释放相机资源 camera.release(); camera = null ; } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub // 打开相机 camera = Camera.open(); try { // 设置预览 camera.setPreviewDisplay(holder); } catch (Exception ex){ ex.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub // 获得相机参数 Camera.Parameters params = camera.getParameters(); // 设置照片大小 params.setPreviewSize(width, height); // 设置照片格式 params.setPictureFormat(PixelFormat.JPEG); // 设置相机参数 camera.setParameters(params); // 开始预览 camera.startPreview(); } }; // 为SurfaceHolder添加回调并设置类型 surfaceHolder.addCallback(surfaceCallback); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } class SavePictureTask extends AsyncTask < byte [],String,String > { @Override protected String doInBackground( byte []... params) { // TODO Auto-generated method stub // 创建文件 File picture = new File(Environment.getExternalStorageDirectory(), " picture.jpg " ); // 如果文件存在就删除掉 if (picture.exists()){ picture.delete(); } try { // 获得文件输出流 FileOutputStream fos = new FileOutputStream(picture.getPath()); // 写入文件 fos.write(params[ 0 ]); // 清空并关闭文件流 fos.flush(); fos.close(); } catch (Exception ex){ ex.printStackTrace(); } return null ; } } // 照片回调,执行保存照片任务并回到预览界面 Camera.PictureCallback pictureCallback = new PictureCallback() { @Override public void onPictureTaken( byte [] data, Camera camera) { // TODO Auto-generated method stub new SavePictureTask().execute(data); camera.startPreview(); } }; // 拍照方法 private void takePic(){ // 停止预览 [color = #FFFF00]camera.stopPreview();[ / color] // 拍照 camera.takePicture( null , null , pictureCallback); } // 响应拍照按键事件 @Override public boolean onKeyDown( int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_CAMERA || keyCode == KeyEvent.KEYCODE_SEARCH){ [color = #FF0000]takePic();[ / color] return true ; } return super .onKeyDown(keyCode, event); }


划红线的代码用线程管理,多执行几次就行了,黄色代码注释掉就不会停止预览了

你可能感兴趣的:(exception,String,null,callback,byte,照片)