注册

便捷相机:CameraFragment

CameraFragment

CameraFragment可以帮助你快速实现打开相机视图,并提供便捷的API来捕获图片。

效果如下:

7ae21524d6ed3285471da54fa61e15db.gif

使用说明:

初始化

  1. //you can configure the fragment by the configuration builder
  2. CameraFragment cameraFragment = CameraFragment.newInstance(new Configuration.Builder().build());
  3. getSupportFragmentManager().beginTransaction()
  4.                 .replace(R.id.content, cameraFragment, FRAGMENT_TAG)
  5.                 .commit();

你可以直接使用下面的代码拍照或者录制视频:

  1. cameraFragment.takePhotoOrCaptureVideo(callback);

切换Flash 模式enable / disabled ( AUTO / OFF / ON )

  1. cameraFragment.toggleFlashMode();

改变Camera类型(前置或者后置):

  1. cameraFragment.switchCameraTypeFrontBack();

设置Camera行为(拍照还是录制视频):

  1. cameraFragment.switchActionPhotoVideo();

还可以设置大小(分辨率):

  1. cameraFragment.openSettingDialog();

Result

在CameraFragmentResultListener中得到录制(或者拍照)的结果

  1. cameraFragment.setResultListener(new CameraFragmentResultListener() {
  2.        @Override
  3.        public void onVideoRecorded(byte[] bytes, String filePath) {
  4.                 //called when the video record is finished and saved
  5.                 startActivityForResult(PreviewActivity.newIntentVideo(MainActivity.this, filePath));
  6.        }
  7.        @Override
  8.        public void onPhotoTaken(byte[] bytes, String filePath) {
  9.                 //called when the photo is taken and saved
  10.                 startActivity(PreviewActivity.newIntentPhoto(MainActivity.this, filePath));
  11.        }
  12. });

Camera Listener

  1. cameraFragment.setStateListener(new CameraFragmentStateListener() {
  2.     //when the current displayed camera is the back
  3.     void onCurrentCameraBack();
  4.     //when the current displayed camera is the front
  5.     void onCurrentCameraFront();
  6.     //when the flash is at mode auto
  7.     void onFlashAuto();
  8.     //when the flash is at on
  9.     void onFlashOn();
  10.     //when the flash is off
  11.     void onFlashOff();
  12.     //if the camera is ready to take a photo
  13.     void onCameraSetupForPhoto();
  14.     //if the camera is ready to take a video
  15.     void onCameraSetupForVideo();
  16.     //when the camera state is "ready to record a video"
  17.     void onRecordStateVideoReadyForRecord();
  18.     //when the camera state is "recording a video"
  19.     void onRecordStateVideoInProgress();
  20.     //when the camera state is "ready to take a photo"
  21.     void onRecordStatePhoto();
  22.     //after the rotation of the screen / camera
  23.     void shouldRotateControls(int degrees);
  24.     void onStartVideoRecord(File outputFile);
  25.     void onStopVideoRecord();
  26. });

Github地址:https://github.com/florent37/CameraFragment

下载地址:CameraFragment-master.zip

0 个评论

要回复文章请先登录注册