便捷相机:CameraFragment
CameraFragment
CameraFragment可以帮助你快速实现打开相机视图,并提供便捷的API来捕获图片。
效果如下:
使用说明:
初始化
- //you can configure the fragment by the configuration builder
- CameraFragment cameraFragment = CameraFragment.newInstance(new Configuration.Builder().build());
- getSupportFragmentManager().beginTransaction()
- .replace(R.id.content, cameraFragment, FRAGMENT_TAG)
- .commit();
你可以直接使用下面的代码拍照或者录制视频:
- cameraFragment.takePhotoOrCaptureVideo(callback);
切换Flash 模式enable / disabled ( AUTO / OFF / ON )
- cameraFragment.toggleFlashMode();
改变Camera类型(前置或者后置):
- cameraFragment.switchCameraTypeFrontBack();
设置Camera行为(拍照还是录制视频):
- cameraFragment.switchActionPhotoVideo();
还可以设置大小(分辨率):
- cameraFragment.openSettingDialog();
Result
在CameraFragmentResultListener中得到录制(或者拍照)的结果
- cameraFragment.setResultListener(new CameraFragmentResultListener() {
- @Override
- public void onVideoRecorded(byte[] bytes, String filePath) {
- //called when the video record is finished and saved
- startActivityForResult(PreviewActivity.newIntentVideo(MainActivity.this, filePath));
- }
- @Override
- public void onPhotoTaken(byte[] bytes, String filePath) {
- //called when the photo is taken and saved
- startActivity(PreviewActivity.newIntentPhoto(MainActivity.this, filePath));
- }
- });
Camera Listener
- cameraFragment.setStateListener(new CameraFragmentStateListener() {
- //when the current displayed camera is the back
- void onCurrentCameraBack();
- //when the current displayed camera is the front
- void onCurrentCameraFront();
- //when the flash is at mode auto
- void onFlashAuto();
- //when the flash is at on
- void onFlashOn();
- //when the flash is off
- void onFlashOff();
- //if the camera is ready to take a photo
- void onCameraSetupForPhoto();
- //if the camera is ready to take a video
- void onCameraSetupForVideo();
- //when the camera state is "ready to record a video"
- void onRecordStateVideoReadyForRecord();
- //when the camera state is "recording a video"
- void onRecordStateVideoInProgress();
- //when the camera state is "ready to take a photo"
- void onRecordStatePhoto();
- //after the rotation of the screen / camera
- void shouldRotateControls(int degrees);
- void onStartVideoRecord(File outputFile);
- void onStopVideoRecord();
- });