Android中拍照和录像功能
来源:互联网 发布:英语四级真题推荐知乎 编辑:程序博客网 时间:2024/05/21 07:56
目前Android中有很多项目需要实现拍照及录像功能,通常都使用camera和sufaceView实现,整个过程较为繁琐,并且代码量比较大。偶然在github上发现一个很好用的框架,在这里做一个简单介绍,后附github地址。
gif:
原项目中没有照明功能,由于项目中定义的camera我私有变量,如果需要照明功能的同学可以下载demo中的library,不需要的同学可以直接添加引用:
- compile 'com.flurgle:camerakit:0.9.17'
cameraview中拍照的图片一般在1M左右,需要上传的话最好先进行压缩,这里我引用了另一个github上的压缩工具,gradle引用:-
- compile 'id.zelory:compressor:2.0.0'
下面进入实际使用,首先是拍照和录像监听
- mCameraView.setCameraListener(new CameraListener() {
- @Override
- public void onPictureTaken(final byte[] jpeg) {
- super.onPictureTaken(jpeg);
- new Thread(new Runnable() {
- @Override
- public void run() {
-
- File file = CameraUtils.getFile(jpeg, getExternalCacheDir().getAbsolutePath(), System.currentTimeMillis() + "_original.jpg");
- try {
-
- final File compressedImageFile = new Compressor(MainActivity.this).setDestinationDirectoryPath(file.getParent()).compressToFile(file);
- Log.i(TAG,"图片保存的地址为:"+compressedImageFile.getAbsolutePath()+compressedImageFile.exists());
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(MainActivity.this,"拍照成功,地址为:"+compressedImageFile.getAbsolutePath()+compressedImageFile.exists(), Toast.LENGTH_SHORT).show();
- }
- });
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }).start();
-
- }
-
- @Override
- public void onVideoTaken(final File video) {
- super.onVideoTaken(video);
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(MainActivity.this,"录制成功,地址为:"+video.getAbsolutePath(), Toast.LENGTH_SHORT).show();
- }
- });
-
- Log.i(TAG,"视频保存的地为:"+video.getAbsolutePath());
- }
- });
将cameraview获得的byte数组存储到File文件中:- public static File getFile(byte[] bfile, String filePath, String fileName) {
- BufferedOutputStream bos = null;
- FileOutputStream fos = null;
- File file = null;
- try {
- File dir = new File(filePath);
- if(!dir.exists()||dir.isDirectory()){
- dir.mkdirs();
- }
- file = new File(filePath+"/"+fileName);
- fos = new FileOutputStream(file);
- bos = new BufferedOutputStream(fos);
- bos.write(bfile);
-
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (bos != null) {
- try {
- bos.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- if (fos != null) {
- try {
- fos.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- return file;
- }
- }
compressedImageFile就是需要的图片 video就是需要的MP4文件。
以下是常用的一些方法,更多的方法可以去github上面查阅:
-
-
-
- public void light(View view) {
- CameraUtils.flashControl(mCameraView);
- isLight =!isLight;
- }
-
- public void takephoto(View view) {
- if (isPhoto){
- mCameraView.captureImage();
- }else {
- if (!isVideoStarted) {
- Toast.makeText(this, "录像开始", Toast.LENGTH_SHORT).show();
- mCameraView.startRecordingVideo();
- }else {
- mCameraView.stopRecordingVideo();
- }
- isVideoStarted =!isVideoStarted;
- }
- }
-
- public void type(View view) {
- ((ImageView)view).setImageResource(
- isPhoto ?
- R.drawable.icon_photograp_photo :
- R.drawable.icon_photograp_video);
-
- isPhoto =!isPhoto;
- if (isPhoto){
- Toast.makeText(this, "已经切换到拍照模式", Toast.LENGTH_SHORT).show();
- }else Toast.makeText(this, "已经切换到录像模式", Toast.LENGTH_SHORT).show();
- }
-
- public void font_back(View view) {
- mCameraView.setFacing(isFont? CameraKit.Constants.FACING_BACK:CameraKit.Constants.FACING_FRONT);
- isFont=!isFont;
- }
-
- public void flash(View view) {
- mCameraView.setFlash(isFlash ?CameraKit.Constants.FLASH_OFF:CameraKit.Constants.FLASH_ON);
- isFlash =!isFlash;
- }
-
- public void zoom(View view) {
- mCameraView.setMethod(isStandard ?CameraKit.Constants.METHOD_STILL:CameraKit.Constants.METHOD_STANDARD);
- isStandard =!isStandard;
- }
调用照明方法:
- public static boolean flashControl(CameraView cameraView) {
- Camera.Parameters parameters =cameraView.getCamera().getParameters();
- if (Camera.Parameters.FLASH_MODE_OFF.equals(parameters.getFlashMode())) {
- parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
- cameraView.getCamera().setParameters(parameters);
- return true;
- } else if (Camera.Parameters.FLASH_MODE_TORCH.equals(parameters.getFlashMode())) {
- parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
- cameraView.getCamera().setParameters(parameters);
- }
- return false;
- }
github地址:CameraKit-android:https://github.com/gogopop/CameraKit-Android
Compressor:https://github.com/zetbaitsu/Compressor
demo下载地址在此 拍照录像demo