Control Camera using CameraFragment

In most of application we need to add camera facility and the headache start when we need to some modify in Android camera API. Then solution is here and i am telling you how to control camera using CameraFragment Android library. It is a simple easy-to-integrate Camera Fragment for Android and their preview directly the camera view, and provides a easy API to capture or manage the device.

You can setup your own layout and control camera using CameraFragment.

In your module

compile 'com.github.florent37:camerafragment:1.0.4'

Implementation:

//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();

Actions

  • You can directly take a photo / video with
cameraFragment.takePhotoOrCaptureVideo(callback);
cameraFragment.takePhotoOrCaptureVideo(callback, directoryPath, fileName);
Directly take a photo / video
Directly take a photo / video
  • Flash can be enable / disabled ( AUTO / OFF / ON ) with
cameraFragment.toggleFlashMode();
Flash can be enable / disabled
Flash can be enable / disabled
  • Camera Type can be modified ( BACK / FRONT ) with
cameraFragment.switchCameraTypeFrontBack();
Camera Type can be modified
Camera Type can be modified
  • Camera action ( PHOTO / VIDEO ) can be modified with
cameraFragment.switchActionPhotoVideo();
Camera action ( PHOTO / VIDEO ) can be modified
Camera action ( PHOTO / VIDEO ) can be modified
  • And you can change the captured photo / video size with
cameraFragment.openSettingDialog();
Change the captured photo / video size
Change the captured photo / video size

Listeners

Result

Get back the result of the camera record / photo in the 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();
});

Text

CameraFragment can ping you with the current record duration with CameraFragmentTextListener

Widgets

CameraFragment comes with some default views

RecordButton, MediaActionSwitchView, FlashSwitchView, CameraSwitchView, CameraSettingsView

Some default views
Some default views

Hope you enjoyed so please comment below.

By Tell Me How

It is a technology blog and admin has excellent experience in programming from 5+ year. You can contact us at ceo.tellmehow@gmail.com

Share your thoughts

2 thoughts on “Control Camera using CameraFragment Android Library”

Leave a Reply

Loading Facebook Comments ...
Loading Disqus Comments ...