Android应用中拍照和录制视频都需要Surface,所以需要创建在Activiy中存在SurfaceView或者VideoView,可以在xml中配置,在代码中加载。同时主Activity中需要implements SurfaceHolder.Callback,实现其中的surfaceCreate, surfaceChanged, surfaceDestoryed三个方法。具体流程如下
First:
SurfaceView mSurfaceView = (SurfaceView)findViewById(R.id.surfaceview);
SurfaceHolder mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
当调用addCallback的时候,就会触发SurfaceHolder.Callback回调,执行surfaceCreate方法,可以在surfaceCreate中做一些初始化。
Second:
打开摄像头,进行初始化。如:
Camera mCamera = Camera.open();
mCamera.setPreviewDisplay(mHolder);
当surfaceview被覆盖或者应用结束退出时,surface会destory,执行surfaceDestoryed回调,可以在该回调中释放Camera,如:
mCamera.stopPrevew();
mCamera.release();
mCamera = null;
Thrid:
进行拍照时,调用takePicture接口,传入callback参数,并在最后一个callback中获取byte[] data,并将数据以jpg文件格式保存,如下所示:
mCamera.takePicture(new ShutterCallback(){}, new PictureCallback(){},new PictureCallback(){}, new PictureCallback(){public void onPictureTaken(byte[] data, Camera camera) {save(data)} });
在save方法中通过流输出到指定路径:
File mediaStoragePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "PicFiles");
String fileName = mediaStoragePath.getPath() + File.sperator + "PIC_" + System.currentTimeMills() + ".jpg";
File pictureFile = new File(fileName);
FileOutputStream fos = new FileOutputStream(picture);
fos.write(data);
fos.close();
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。