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:
Attribute | Meaning |
---|---|
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:
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.
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.
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:
Step 5: Setup if you would like the screen to flash
This enables the screen to flash when capturing the photo.
Step 6: Finally, build
To summarize the whole process, the final code would be:
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.
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.
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.
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:
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.
Overall Example Usage in an Activity or Fragment
Last updated