Skip to content

Customizing face recognition area borders#

Applies to LUNA ID for Android only.

In some cases, you may need the best shot search to start only after a user places their face in a certain area in the screen. You can specify face recognition area borders by implementing one of the following strategies:

  1. Border distances are not initialized
  2. Border distances are initialized with an Android custom view
  3. Border distances are initialized in dp
  4. Border distances are initialized automatically

Border distances are not initialized#

This strategy is useful if the border distances should be 0 pixels. This is the default strategy.

To implement the strategy, use the Default object of the InitBorderDistancesStrategy class.

Consider the code below for the strategy implementation:

LunaID.showCamera(
    activity,
    LunaID.ShowCameraParams(
    disableErrors = true,
    borderDistanceStrategy = InitBorderDistancesStrategy.Default
    )
)

Border distances are initialized with an Android custom view#

This strategy allows you to define how to calculate distances to the face recognition area inside an Android custom view. The custom view can stretch to fill the entire screen and contain different elements, one of which is a circle that corresponds to the face recognition area. The custom view must implement the MeasureBorderDistances interface. The interface result value is a child object with custom view border distances. Implementation of this interface is required due to impossibility to get the distances outside the custom view and allows you to comply with the encapsulation principle.

Consider the example code below for the MeasureBorderDistances interface implementation. It also shows how to implement a business logic according to which a chin and forehead must be inside the face recognition area.

override fun measureBorderDistances(): BorderDistancesInPx {
    val radius = minOf(right - left, bottom - top) / 2f
    val diameter = radius * 2

    val distanceFromLeftToCircle = (width - diameter) / 2f
    val distanceFromTopToCircle = (height - diameter) / 2f

    // business logic
    val foreheadZone = 64
    val chinZone = 36
    val horizontalMargin = 16

    val distanceFromTopWithForehead = distanceFromTopToCircle.toInt() + foreheadZone
    val distanceFromBottomWithChin = distanceFromTopToCircle.toInt() + chinZone
    val distanceHorizontalToCircle = distanceFromLeftToCircle.toInt() + horizontalMargin
    // business logic ends

    return BorderDistancesInPx(
        fromLeft = distanceHorizontalToCircle,
        fromTop = distanceFromTopWithForehead,
        fromRight = distanceHorizontalToCircle,
        fromBottom = distanceFromBottomWithChin,
    )
}

To implement the strategy, use the InitBorderDistancesStrategy.WithCustomView class. You also need to pass an argument with the ID of the custom view on the XML markup to the object of the WithCustomView class.

Consider the example code below for the strategy implementation:

LunaID.showCamera(
    context,
    LunaID.ShowCameraParams(
        disableErrors = true,
        borderDistanceStrategy = InitBorderDistancesStrategy.WithCustomView(
            R.id.overlay_viewport
        )
    )
) 

Border distances are initialized in dp#

This strategy allows you to specify distances to the face recognition area in density-independent pixels.

To implement the strategy, use the InitBorderDistancesStrategy.WithDp class.

Consider the example code below for the strategy implementation:

LunaID.showCamera(
    context,
    LunaID.ShowCameraParams(
        disableErrors = false,
        borderDistanceStrategy = InitBorderDistancesStrategy.WithDp(
            topPaddingInDp = 150,
            bottomPaddingInDp = 250,
            leftPaddingInDp = 8,
            rightPaddingInDp = 8
        )
    )
)

Border distances are initialized automatically#

This strategy allows you to automatically calculate distances to the face recognition area on the XML markup by using its ID:

<View
    android:id="@+id/faceZone"
    android:layout_width="200dp"
    android:layout_height="300dp"
    android:background="#1D000000"
    android:layout_gravity="top|center"
    android:layout_marginTop="150dp"/>

To implement the strategy, use the InitBorderDistancesStrategy.WithViewId class.

Consider the example code below for the strategy implementation:

LunaID.showCamera(
    context,
    LunaID.ShowCameraParams(
        disableErrors = false,
        borderDistanceStrategy = InitBorderDistancesStrategy.WithViewId(R.id.faceZone)
    )
)