SmartSelfie Capture

The Smile ID Library is great for capturing high-quality photos and supports a wide range of devices from low-end to high-end smartphones.

The Smile ID library enables capturing the perfect selfies through various checks on the device before capturing the final selfie.

There are five components essential to setting up the selfie capture process: CameraSourcePreview, SmartSelfieManager, SelfieCaptureConfig, OnFaceStateChangedListener, and SmartSelfieManager.OnCompleteListener.

CameraSourcePreview

The CameraSourcePreview is a custom view that presents the camera as an oval in the middle of the screen surrounded by a progress bar that loads as the perfect selfie is captured. This custom view provides a simple way to capture user selfies.

It is advised to give the view as much real estate as possible, maintaining an aspect ratio of about 2.5:3, but this can be adjusted as necessary.

Place the view in your layout XML file as indicated below:

<com.smileidentity.libsmileid.core.CameraSourcePreview
    android:id="@+id/previewCamera"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    app:captured_progress_state_color="<YOUR COLOR CHOICE>"
    app:capturing_progress_state_color="<YOUR COLOR CHOICE>"
    app:overlay_height="300dp"
    app:overlay_width="250dp"
    app:progress_width="15dp" />
AttributeMeaning

capturing_progress_state_color (color)

Defines the color of the progress circle when the library is capturing the selfie.

captured_progress_state_color (color)

Defines the color of the progress circle when the library has captured a selfie and when the capture is complete.

progress_width (dimension)

Width of the capture progress circle.

overlay_width (dimension)

Width of the oval guide for the face capture.

overlay_height (dimension)

Height of the oval guide for the face capture.

SelfieCaptureConfig

This object carries instructions on how the SelfieManager should capture selfies and has six main methods.

Step 1: Initialize the SelfieCaptureConfig

Create a SelfieCaptureConfig.Builder using the method below, which takes a context as its argument:

SelfieCaptureConfig.Builder selfieCaptureConfigBuilder = new SelfieCaptureConfig.Builder(Context context);

Step 2: Set the camera type

This method tells the SelfieManager which camera to use and takes two arguments from SelfieCaptureConfig, either SelfieCaptureConfig.BACK_CAMERA or SelfieCaptureConfig.FRONT_CAMERA.

selfieCaptureConfigBuilder.setCameraType(SelfieCaptureConfig.FRONT_CAMERA);

Step 3: Set the preview

This method takes the CameraSourcePreview as an argument and instructs the SelfieManager to use this view to show the camera activity as the selfie capture process progresses.

CameraSourcePreview preview = findViewById(R.id.previewCamera);
selfieCaptureConfigBuilder.setPreview(preview);

Step 4: Set if you would like to capture the selfie manually

By default, the SDK will capture the selfie once it has done checks on the face in the camera. However, there are cases when you might want to capture the selfie manually, which can be done through:

selfieCaptureConfigBuilder.setManualSelfieCapture(true);

Step 5: Setup if you would like the screen to flash

This enables the screen to flash when capturing the photo.

selfieCaptureConfigBuilder.setFlashScreenOnShutter(true);

Step 6: Finally, build

To summarize the whole process, the final code would be:

SelfieCaptureConfig selfieCaptureConfigBuilder = new SelfieCaptureConfig.Builder(this)
                .setCameraType(SelfieCaptureConfig.FRONT_CAMERA)
                .setPreview(mPreview)
                .setManualSelfieCapture(mMultipleEnroll)
                .setFlashScreenOnShutter(!mMultipleEnroll)
                .build();

SmartSelfieManager

At the heart of the selfie capture process is the SmartSelfieManager class, which manages the whole process by getting input from the CameraSourcePreview and providing your application with callbacks via the OnFaceStateChangedListener and SmartSelfieManager.OnCompleteListener.

Step 1: Create the SelfieCaptureConfig object

Instantiate the class using SmartSelfieManager(SelfieCaptureConfig config). This method expects an object of SelfieCaptureConfig to instruct how to capture selfies.

SmartSelfieManager smartSelfieManager = new SmartSelfieManager(selfieCaptureConfigBuilder);

Step 2: Setup Callbacks

Everything that happens in the SmartSelfieManager is relayed back to the implementing class through two callbacks: SmartSelfieManager.OnCompleteListener and OnFaceStateChangedListener.

SmartSelfieManager.OnCompleteListener

This interface has two methods. The first one is onComplete, which is called once the capture is complete and returns a bitmap to the implementing class.

void onComplete(Bitmap fullPreview);

The second one is onError, which relays errors that happen within the SmartSelfieManager. The main error is SIDError.COULD_NOT_INITIALIZE_CAMERA, which occurs when there is an error while trying to initialize the camera. It may also return exceptions with a summary of what went wrong.

SmartSelfieManager.takePicture

This method will take a manual selfie for cases when the selfie has been set to capture manually.

OnFaceStateChangedListener

This interface has one method, onFaceStateChange, which is responsible for relaying prompts to inform the user of actions to take to capture the best photo.

void onFaceStateChange(FaceState status);

This method returns an enum with the following values:

  • NO_FACE_FOUND - If SmartSelfieManager cannot find a face on the camera.

  • DO_MOVE_CLOSER - If SmartSelfieManager detects the user is too far from the camera.

  • DO_SMILE - If SmartSelfieManager has found a face and needs to confirm it is human, it asks the user to smile.

  • DO_SMILE_MORE - If SmartSelfieManager detects a smile but needs further confirmation, it might ask the user to smile more.

  • CAPTURING - SmartSelfieManager is busy capturing the photos.

  • FACE_TOO_CLOSE - If the face is too close to the camera and needs to move a bit further.

  • BLURRY - If the camera feed is blurry.

  • TOO_DARK - If the lighting condition is not optimal for the perfect selfie.

  • COLOR_CHECK_FAILED - If the color check fails.

  • IDLE - When the system is idle.

  • COMPATIBILITY_MODE - If there is an issue with the device's compatibility with the library.

In summary, the two callbacks would be:

smartSelfieManager.setOnCompleteListener(this);
smartSelfieManager.setOnFaceStateChangeListener(this);

@Override
public void onFaceStateChange(FaceState faceState) {
    switch (faceState) {
        case DO_SMILE:
            // Prompt the user to smile
            break;
        case CAPTURING:
            // Prompt the user that your app is capturing
            break;
        case DO_SMILE_MORE:
            // Prompt the user to smile more
            break;
        case NO_FACE_FOUND:
            // Prompt the user that no face is found
            break;
        case DO_MOVE_CLOSER:
            // Prompt the user to move closer to the camera
            break;
        case BLURRY:
            // Prompt the user that the image is blurry
            break;
        case TOO_DARK:
            // Prompt the user that more light is needed
            break;
        case COMPATIBILITY_MODE:
            // Prompt the user about compatibility mode
            break;
    }
}

@Override
public void onComplete(Bitmap fullPreviewFrame) {
    if (fullPreviewFrame != null) {
        // Process returned full preview frame
    }
}

@Override
public void onError(Throwable e) {
    // Handle error
}

Important SmartSelfieManager Callbacks

From version 6.1.0 of the library, there have been callbacks introduced to provide better stability regarding how the Android OS manages the camera. These should correlate with Android fragment/activity lifecycles.

@Override
protected void onResume() {
    super.onResume();
    smartSelfieManager.resume();
}

@Override
protected void onPause() {
    super.onPause();
    smartSelfieManager.pause();
}

@Override
protected void onStop() {
    super.onStop();
    smartSelfieManager.stop();
}

// smartSelfieManager.resume() should be called when your activity/fragment resumes (onResume)
// smartSelfieManager.pause() should be called when your activity/fragment pauses (onPause)
// smartSelfieManager.stop() should be called when your activity/fragment stops (onStop)

Overall Example Usage in an Activity or Fragment

public class SIDSelfieActivity extends AppCompatActivity implements
        OnFaceStateChangeListener,
        SmartSelfieManager.OnCompleteListener {

    CameraSourcePreview mPreview;
    private TextView mPromptTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sid_activity_selfie);
        mPreview = findViewById(R.id.previewCamera);
        mPromptTv = findViewById(R.id.prompt_tv);
        initSmartSelfie();
    }

    private void initSmartSelfie() {
        smart

SelfieManager = new SmartSelfieManager(getCaptureConfig());
        smartSelfieManager.setOnCompleteListener(this);
        smartSelfieManager.setOnFaceStateChangeListener(this);
        smartSelfieManager.captureSelfie($UNIQUE_TAG); // should be unique per job unless you want to overwrite a job
    }

    private SelfieCaptureConfig getCaptureConfig() {
        return new SelfieCaptureConfig.Builder(this) // or getActivity() if in fragment
                .setCameraType(SelfieCaptureConfig.FRONT_CAMERA)
                .setPreview(mPreview)
                .setManualSelfieCapture(false)
                .setProminentSmileText("Smile!")
                .build();
    }

    @Override
    protected void onResume() {
        super.onResume();
        smartSelfieManager.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        smartSelfieManager.pause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        smartSelfieManager.stop();
    }

    @Override
    public void onComplete(Bitmap fullPreviewFrame) {
        // Capture is complete, all selfies ready, can start upload screen
    }

    @Override
    public void onError(Throwable e) {
        // Handle errors and maybe retry using initSmartSelfie();
        // smartSelfieManager.resume();
    }

    @Override
    public void onFaceStateChange(FaceState faceState) {
        switch (faceState) {
            case DO_SMILE:
                mPromptTv.setText("Smile for the camera");
                break;
            case CAPTURING:
                mPromptTv.setText("Please wait while we capture");
                break;
            case DO_SMILE_MORE:
                mPromptTv.setText("Smile more");
                break;
            case NO_FACE_FOUND:
                mPromptTv.setText("Cannot find a face");
                break;
            case DO_MOVE_CLOSER:
                mPromptTv.setText("Please move closer");
                break;
            case BLURRY:
                mPromptTv.setText("Image is blurry");
                break;
            case TOO_DARK:
                mPromptTv.setText("Need more light");
                break;
            case COMPATIBILITY_MODE:
                mPromptTv.setText("Compatibility mode");
                break;
        }
    }
}

Last updated