BioPass ID Face SDK React Native
Latest Version
February 27, 2023 - [v0.1.24]
Table of Contents
Quick Start Guide
First, you will need a license key to use the SDK. You can acquire a license key when subscribing to a BioPass ID plan.
Check out our official documentation for more in depth information on BioPass ID.
1. Prerequisites:
| Android | iOS |
---|
Support | SDK 21+ | iOS 13+ |
- A device with a camera
- License key
- Internet connection is required to verify the license
2. Installation
npm install @biopassid/face-sdk-react-native
Android
Change the minimum Android sdk version to 21 (or higher) in your android/app/build.gradle
file.
minSdkVersion 21
Change the compile Android sdk version to 31 (or higher) in your android/app/build.gradle
file.
compileSdkVersion 31
IOS
Requires iOS 13.0 or higher.
Add two rows to the ios/Info.plist
:
- one with the key
Privacy - Camera Usage Description
and a usage description. - and one with the key
Privacy - Photo Library Usage Description
and a usage description.
If editing Info.plist as text, add:
<key>NSCameraUsageDescription</key>
<string>Your camera usage description</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Your library usage description</string>
Then go into your project's ios folder and run pod install.
$ cd ios
$ pod install
3. How to use
Basic Example
To call Face Capture in your React Native project is as easy as follow:
import React from "react";
import { StyleSheet, View, Button } from "react-native";
import {
FaceConfigPreset,
FaceConfigType,
FaceEvent,
buildCameraView,
FaceCameraLensDirection,
} from "@biopassid/face-sdk-react-native";
export function App() {
const config = FaceConfigPreset.getConfig(
FaceConfigType.FACE_CAPTURE
)
.setLicenseKey("your-license-key")
.setDefaultCameraPosition(FaceCameraLensDirection.FRONT)
.setShowFlipCameraButton(true)
.setStringsScreenTitle("Capturando Face")
.setStylesOverlayColor("#CC000000");
function callback(event: FaceEvent) {
console.log(event.photo);
}
function handleButton() {
buildCameraView(config, callback);
};
return (
<View style={styles.container}>
<Button onPress={handleButton} title="Capture Face" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
backgroundColor: '#FFFFFF',
},
});
Example using Fetch API to call the BioPass ID API
For this example we used Enroll from the Multibiometrics package.
Here, you will need an API key to be able to make requests to the BioPass ID API.
import React from 'react';
import {StyleSheet, View, Button} from 'react-native';
import {
FaceConfigPreset,
FaceConfigType,
FaceEvent,
buildCameraView,
} from '@biopassid/face-sdk-react-native';
import {Base64} from './utils/Base64';
export default function App() {
const config = FaceConfigPreset.getConfig(
FaceConfigType.FACE_CAPTURE,
).setLicenseKey('your-license-key');
async function callback(event: FaceEvent) {
const imageBase64 = Base64.arrayBufferToBase64(event.photo);
const headers = {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': 'your-api-key',
};
const body = JSON.stringify({
Person: {
CustomID: 'your-customID',
Face: [{'Face-1': imageBase64}],
},
});
const response = await fetch(
'https://api.biopassid.com/multibiometrics/enroll',
{
method: 'POST',
headers,
body,
},
);
const data = await response.json();
console.log('Response status: ', response.status);
console.log('Response body: ', data);
}
function handleButton() {
buildCameraView(config, callback);
}
return (
<View style={styles.container}>
<Button onPress={handleButton} title="Capture Face" />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#FFFFFF',
},
});
Base64 util
const chars =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
export const Base64 = {
btoa: (input: string = '') => {
let str = input;
let output = '';
for (
let block = 0, charCode, i = 0, map = chars;
str.charAt(i | 0) || ((map = '='), i % 1);
output += map.charAt(63 & (block >> (8 - (i % 1) * 8)))
) {
charCode = str.charCodeAt((i += 3 / 4));
if (charCode > 0xff) {
throw new Error(
"'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.",
);
}
block = (block << 8) | charCode;
}
return output;
},
arrayBufferToBase64: (buffer: ArrayBuffer) => {
var binary = '';
var bytes = new Uint8Array(buffer);
bytes.forEach((item: number) => {
binary += String.fromCharCode(item);
});
return Base64.btoa(binary);
},
};
4. Callback
Setting the Callback
You can pass a function callback to receive the captured image. You can write you own callback following this example:
function callback(event: FaceEvent) {
console.log('photo: ', event.photo);
doSomething(event);
}
5. License
To use Face Capture you need a license key. To set the license key needed is simple as setting another attribute. Simply doing:
const config = FaceConfigPreset.getConfig(FaceConfigType.FACE_CAPTURE)
.setLicenseKey('your-license-key');
FaceConfig
You can also use pre-build configurations on your application, so you can automatically start using multiples services and features that better suit your application. You can instantiate each one and use it's default properties, or if you prefer you can change every config available. Here are the types that are supported right now:
FaceCaptureConfig
Variable name | Type | Default value |
---|
licenseKey | string | "" |
cameraPreset | FaceCameraPreset | FaceCameraPreset.FACE_CAPTURE |
defaultCameraPosition | FaceCameraLensDirection | FaceCameraLensDirection.FRONT |
outputFormat | FaceOutputFormat | FaceOutputFormat.JPEG |
captureButtonType | FaceCaptureButtonType | FaceCaptureButtonType.DEFAULT |
showFlashButton | boolean | false |
showFlipCameraButton | boolean | true |
flashEnabledByDefault | boolean | false |
showHelpText | boolean | true |
showScreenTitle | boolean | true |
autoCapture | boolean | true |
autoCaptureTimeout | number | 3000 |
strings | FaceStrings | |
styles | FaceStyles | |
ContinuousCaptureConfig
Variable name | Type | Default value |
---|
licenseKey | string | "" |
cameraPreset | FaceCameraPreset | FaceCameraPreset.FACE_CAPTURE |
defaultCameraPosition | FaceCameraLensDirection | FaceCameraLensDirection.FRONT |
outputFormat | FaceOutputFormat | FaceOutputFormat.JPEG |
captureButtonType | FaceCaptureButtonType | FaceCaptureButtonType.DEFAULT |
showFlashButton | boolean | false |
showFlipCameraButton | boolean | false |
flashEnabledByDefault | boolean | false |
showHelpText | boolean | true |
showScreenTitle | boolean | true |
autoCapture | boolean | true |
autoCaptureTimeout | number | 1000 |
strings | FaceStrings | |
styles | FaceStyles | |
FaceStrings
Variable name | Type | Default value |
---|
screenTitle | string | "Capturando Face" |
helpText | string | "Encaixe seu rosto no formato acima e aguarde o sinal verde" |
loading | string | "Processando..." |
customCaptureButtonText | string | "Capture Face" |
noFaceDetectedMessage | string | "Nenhuma face detectada" |
multipleFacesDetectedMessage | string | "Múltiplas faces detectadas" |
detectedFaceIsCenteredMessage | string | "Mantenha o celular parado" |
detectedFaceIsCloseMessage | string | "Afaste o rosto da câmera" |
detectedFaceIsDistantMessage | string | "Aproxime o rosto da câmera" |
detectedFaceIsOnTheLeftMessage | string | "Mova o celular para a direita" |
detectedFaceIsOnTheRightMessage | string | "Mova o celular para a esquerda" |
detectedFaceIsTooUpMessage | string | "Mova o celular para baixo" |
detectedFaceIsTooDownMessage | string | "Mova o celular para cima" |
FaceStyles
Variable name | Type | Default value |
---|
faceShape | FaceScreenShape | FaceScreenShape.CUSTOM |
faceColor | string | "#FFFFFF" |
faceEnabledColor | string | "#16AC81" |
faceDisabledColor | string | "#E25353" |
overlayColor | string | "#CC000000" |
backButtonIcon | string | null |
backButtonColor | string | "#FFFFFF" |
backButtonSize | FaceSize | FaceSize(24, 24) |
flashButtonIcon | string | null |
flashButtonSize | FaceSize | FaceSize(24, 24) |
flashButtonEnabledColor | string | "#FFCC01" |
flashButtonDisabledColor | string | "#FFFFFF" |
flipCameraButtonIcon | string | null |
flipCameraButtonColor | string | "#FFFFFF" |
flipCameraButtonSize | FaceSize | FaceSize(65, 43) |
textColor | string | "#FFFFFF" |
textSize | number | 20 |
customFont | string | null |
captureButtonIcon | string | null |
captureButtonColor | string | "#D9D9D9" |
captureButtonSize | FaceSize | FaceSize(76, 76) |
customCaptureButtonBackgroundColor | string | "#D6A262" |
customCaptureButtonSize | FaceSize | FaceSize(300, 45) |
customCaptureButtonTextColor | string | "#FFFFFF" |
customCaptureButtonTextSize | number | 20 |
Changing Font Family
Android
You can use the default font family or set a font you prefer. To set a font, create a folder font under res directory in your android/app/src/main/res
. Download the font which ever you want and paste it inside font folder. All font names must be only: lowercase a-z, 0-9, or underscore. The structure should be some thing like below.
Then, just set the font passing the name of the font file.
const config = FaceConfigPreset.getConfig(FaceConfigType.FACE_CAPTURE)
.setStylesFontFamily("roboto_mono_bold_italic");
IOS
To add the font files to your Xcode project:
- In Xcode, select the Project navigator.
- Drag your fonts from a Finder window into your project. This copies the fonts to your project.
- Select the font or folder with the fonts, and verify that the files show their target membership checked for your app’s targets.
Then, add the UIAppFonts key to your app’s Info.plist file. For the key’s value, provide an array of strings containing the relative paths to any added font files.
In the following example, the font file is inside the fonts directory, so you use fonts/RobotoMono-BoldItalic.ttf as the string value in the Info.plist file.
Finally, just set the font by passing the font name when instantiating FaceConfig.
const config = FaceConfigPreset.getConfig(FaceConfigType.FACE_CAPTURE)
.setStylesCustomFont("RobotoMono-BoldItalic")
Changing Icon
Android
You can use the default icon or set a icon you prefer. To set a icon, download the icon which ever you want and paste it inside drawable folder in your android/app/src/main/res
. The structure should be some thing like below.
Then, just set the font passing the name of the icon file.
const config = FaceConfigPreset.getConfig(FaceConfigType.FACE_CAPTURE)
.setStylesBackButtonIcon("ic_baseline_camera")
.setStylesFlashButtonIcon("ic_baseline_camera")
.setStylesFlipCameraButtonIcon("ic_baseline_camera")
.setStylesCaptureButtonIcon("ic_baseline_camera");
IOS
To add icon files to your Xcode project:
- In the Project navigator, select an asset catalog: a file with a .xcassets file extension.
- Drag an image from the Finder to the outline view. A new image set appears in the outline view, and the image asset appears in a well in the detail area.
Finally, just set the icon by passing the icon name when instantiating FaceConfig.
const config = FaceConfigPreset.getConfig(FaceConfigType.FACE_CAPTURE)
.setStylesBackButtonIcon("photo_camera")
.setStylesFlashButtonIcon("photo_camera")
.setStylesFlipCameraButtonIcon("photo_camera")
.setStylesCaptureButtonIcon("photo_camera")
FaceSize
Variable name | Type |
---|
width | number |
height | number |
FaceCameraPreset (enum)
Preset Type | Resolution |
---|
FaceCameraPreset.FACE_CAPTURE | 640x480 |
FaceCameraPreset.CONTINUOUS_CAPTURE | 640x480 |
FaceCameraLensDirection (enum)
Name |
---|
FaceCameraLensDirection.FRONT |
FaceCameraLensDirection.BACK |
FaceCaptureFormat (enum)
Name |
---|
FaceCaptureFormat.JPEG |
FaceCaptureFormat.PNG |
FaceCaptureButtonType (enum)
Name |
---|
FaceCaptureButtonType.CUSTOM |
FaceCaptureButtonType.ICON |
FaceScreenShape (enum)
Name |
---|
FaceScreenShape.SQUARE |
FaceScreenShape.ELLIPSE |
FaceScreenShape.CUSTOM |
Using Custom Capture Button
const config = FaceConfigPreset.getConfig(FaceConfigType.FACE_CAPTURE)
.setCaptureButtonType(FaceCaptureButtonType.CUSTOM)
.setStylesCustomButtonBackgroundColor("#FFFF00")
.setStylesCustomButtonTextColor("#FF00FF")
.setStringsCustomButtonText("Capture");
Using Icon Capture Button
const config = FaceConfigPreset.getConfig(FaceConfigType.FACE_CAPTURE)
.setCaptureButtonType(FaceCaptureButtonType.ICON)
.setStylesCaptureButtonIcon("ic_baseline_camera")
.setStylesCaptureButtonColor("#FF0000")
.setStylesCaptureButtonSize({width: 76, height: 76});
How to instantiate and functionalities
To instantiate it's easy as to do one function call (as we have seen previously on the example). You only need to specify which type of config you want using a ENUM FaceConfigType. Every type however can have different features implemented, here are the supported types:
Config Type | Enum | feature |
---|
Face Capture | FaceConfigType.FACE_CAPTURE | Capture still image and detects face |
Continuous Capture | FaceConfigType.CONTINUOUS_CAPTURE | Capture every frame per second |
FaceEvent
On your Face callback function, you receive a FaceEvent.
FaceEvent
Changelog
v0.1.24
v0.1.23
- Documentation update;
- New config option autoCaptureTimeout;
- UI customization improvements:
- Added FaceScreenShape with more face shape options;
- Added progress animation during face detection.
v0.1.22
- Bug fixes;
- Documentation update.
v0.1.21
v0.1.20
v0.1.19
v0.1.18
- Bug fixes;
- Flash mode fixes;
- Face detection improvement;
- New feature automatic capture;
- UI customization improvements;
- New feature face detection;
- Camera preset fix;
- Camera preview fix;
- New icon capture button;
- Fix in requesting permissions;
- Fix in performance of ContinuousCapture;
- Parameterizable screenTitle;
- New option to set text color;
- New custom capture button;
- Class name standardization;
- New option to set the font.
v0.1.13
- Face Capture SDK for Android and iOS.
License
This project is proprietary software. See the LICENSE for more information.