@microblink/biometrics 1000.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +288 -0
- package/dist/biometrics.js +846 -0
- package/package.json +38 -0
- package/types/index.rollup.d.ts +376 -0
package/README.md
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
# @microblink/biometrics
|
|
2
|
+
|
|
3
|
+
Biometrics Web SDK for guided and headless face capture.
|
|
4
|
+
|
|
5
|
+
<!-- microblink:bundle-size:start -->
|
|
6
|
+
|
|
7
|
+
## Bundle size
|
|
8
|
+
|
|
9
|
+
Production consumer bundle sizes for `@microblink/biometrics`:
|
|
10
|
+
|
|
11
|
+
| Entrypoint | Minified | Gzip |
|
|
12
|
+
| ---------- | -------- | ------- |
|
|
13
|
+
| `root` | 26.28 kB | 7.26 kB |
|
|
14
|
+
|
|
15
|
+
External packages and runtime assets such as workers, WASM, and models are excluded. Shared code is included in each entrypoint that loads it.
|
|
16
|
+
|
|
17
|
+
_Generated automatically. Do not edit manually._
|
|
18
|
+
<!-- microblink:bundle-size:end -->
|
|
19
|
+
|
|
20
|
+
## Quickstart
|
|
21
|
+
|
|
22
|
+
### 1. Install
|
|
23
|
+
|
|
24
|
+
Install the SDK, the Core package that ships the runtime resources, and the Solid peer dependencies used by the package root:
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
npm install @microblink/biometrics @microblink/biometrics-core solid-js @ark-ui/solid solid-zustand @solid-primitives/keyed
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### 2. Host the resources
|
|
31
|
+
|
|
32
|
+
The SDK loads a Web Worker, WebAssembly modules, and model data at runtime. Copy them into a directory your application serves as static files:
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
mkdir -p public/resources
|
|
36
|
+
cp -R node_modules/@microblink/biometrics-core/dist/resources/. public/resources/
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Deploy the complete `resources/` tree and copy it again whenever you update the SDK. `resourcesLocation` is the parent URL of `resources/`. It defaults to the current page URL.
|
|
40
|
+
|
|
41
|
+
### 3. Serve the application
|
|
42
|
+
|
|
43
|
+
Serve the application over HTTPS or from `localhost`, because browsers only expose the camera in a secure context. Send these response headers to enable the faster multithreaded WebAssembly build:
|
|
44
|
+
|
|
45
|
+
```http
|
|
46
|
+
Cross-Origin-Opener-Policy: same-origin
|
|
47
|
+
Cross-Origin-Embedder-Policy: require-corp
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Without them, the SDK uses a single-threaded build.
|
|
51
|
+
|
|
52
|
+
### 4. Capture a face
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { createBiometricsUi } from "@microblink/biometrics";
|
|
56
|
+
|
|
57
|
+
const ui = await createBiometricsUi({
|
|
58
|
+
licenseKey: "<license-key>",
|
|
59
|
+
resourcesLocation: window.location.origin,
|
|
60
|
+
onResult(result) {
|
|
61
|
+
console.log("Captured face", result.bestImage);
|
|
62
|
+
void ui.destroy();
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
The SDK opens the front camera, guides the user, and calls `onResult` with a [`FaceCaptureResult`](#capture-result).
|
|
68
|
+
|
|
69
|
+
## Examples
|
|
70
|
+
|
|
71
|
+
- [Biometrics Simple](../../apps/examples/biometrics-simple/): guided capture with `createBiometricsUi`.
|
|
72
|
+
- [Biometrics Advanced Setup](../../apps/examples/biometrics-advanced-setup/): guided capture composed from the headless SDK, `@microblink/biometrics-ux-manager`, and Camera Manager.
|
|
73
|
+
- [Biometrics Custom UI](../../apps/examples/biometrics-custom-ui/): headless capture with `createBiometrics`, Camera Manager, and an application-owned interface.
|
|
74
|
+
|
|
75
|
+
## Browser Support
|
|
76
|
+
|
|
77
|
+
Biometrics supports camera capture in these browser versions and newer:
|
|
78
|
+
|
|
79
|
+
- Chrome / Chromium 96 (desktop and Android)
|
|
80
|
+
- Edge 96
|
|
81
|
+
- Opera 84
|
|
82
|
+
- Firefox 132 (desktop)
|
|
83
|
+
- Safari 16.4 (macOS)
|
|
84
|
+
- iOS Safari 16.4
|
|
85
|
+
|
|
86
|
+
The SDK must run on HTTPS or localhost because camera APIs require a secure context. Firefox for Android is not supported.
|
|
87
|
+
|
|
88
|
+
Call `checkBiometricsSupport({ resourcesLocation })` before loading the SDK to check the browser capabilities and the hosted resources. The report's `supported` flag summarizes the individual `checks`.
|
|
89
|
+
|
|
90
|
+
## Guided UI
|
|
91
|
+
|
|
92
|
+
Use `createBiometricsUi` when the SDK should manage the camera, onboarding, help, capture feedback, and retry flow.
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { createBiometricsUi, type BiometricsUiOptions } from "@microblink/biometrics";
|
|
96
|
+
|
|
97
|
+
const options: BiometricsUiOptions = {
|
|
98
|
+
licenseKey: "<license-key>",
|
|
99
|
+
resourcesLocation: window.location.origin,
|
|
100
|
+
targetNode: document.getElementById("biometrics-root") ?? undefined,
|
|
101
|
+
captureTimeoutMs: 60_000,
|
|
102
|
+
feedbackUiOptions: {
|
|
103
|
+
showOnboardingGuide: true,
|
|
104
|
+
showHelpButton: true,
|
|
105
|
+
},
|
|
106
|
+
onResult(result) {
|
|
107
|
+
console.log("Captured face", result);
|
|
108
|
+
},
|
|
109
|
+
onError(error) {
|
|
110
|
+
console.error(error.code, error);
|
|
111
|
+
},
|
|
112
|
+
onEvent(event) {
|
|
113
|
+
if (event.kind === "captureTimeout") {
|
|
114
|
+
console.log("The user did not complete capture in time");
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const ui = await createBiometricsUi(options);
|
|
120
|
+
|
|
121
|
+
// Later, when leaving the route.
|
|
122
|
+
await ui.destroy();
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
`createBiometricsUi` resolves after the SDK is initialized and the camera stream has started. It rejects with a `BiometricsError` when initialization fails.
|
|
126
|
+
|
|
127
|
+
During capture:
|
|
128
|
+
|
|
129
|
+
- `onResult` runs once after a successful capture. The capture UI is dismissed, but the SDK stays loaded until you call `destroy()`.
|
|
130
|
+
- `onError` reports initialization and capture errors. During capture, the UI shows its own error dialog with retry or cancel actions, so you do not need to present the error yourself.
|
|
131
|
+
- `onEvent` forwards face guidance, timeouts, the capture result, and onboarding and help interactions.
|
|
132
|
+
- The close button calls `destroy()` for you. Await `destroy()` when your application leaves the capture flow for another reason.
|
|
133
|
+
|
|
134
|
+
Set `preferredCameraDeviceId` to reopen a previously selected camera. `onSelectedCameraDeviceIdChange` reports the current selection so you can store it.
|
|
135
|
+
|
|
136
|
+
Set `helpTooltipShowDelay` to `null` to disable the help tooltip.
|
|
137
|
+
|
|
138
|
+
### UI customization
|
|
139
|
+
|
|
140
|
+
Use `feedbackUiOptions` for capture guidance and Biometrics localization. Use `cameraManagerUiOptions` for camera controls and Camera Manager localization.
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
await createBiometricsUi({
|
|
144
|
+
licenseKey: "<license-key>",
|
|
145
|
+
feedbackUiOptions: {
|
|
146
|
+
localizationStrings: {
|
|
147
|
+
onboarding_modal: {
|
|
148
|
+
title: "Get ready for your selfie",
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Set inherited CSS custom properties on `targetNode` or an ancestor to brand the UI. Supported Shadow Parts include:
|
|
156
|
+
|
|
157
|
+
- `capture-screen-part`
|
|
158
|
+
- `video-element-part`
|
|
159
|
+
- `camera-select-part`
|
|
160
|
+
- `mirror-camera-button-part`
|
|
161
|
+
- `torch-button-part`
|
|
162
|
+
- `close-button-part`
|
|
163
|
+
- `help-button-part`
|
|
164
|
+
- `help-button-tooltip-part`
|
|
165
|
+
|
|
166
|
+
Internal class names and descendant DOM structure are not public API.
|
|
167
|
+
|
|
168
|
+
## Headless API
|
|
169
|
+
|
|
170
|
+
Use `createBiometrics` when your application owns the camera and the interface. The SDK analyzes the frames you supply, emits face guidance, and resolves with a `FaceCaptureResult`.
|
|
171
|
+
|
|
172
|
+
This example uses the framework-independent Camera Manager `/core` entrypoint to supply frames:
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
import { BiometricsError, createBiometrics } from "@microblink/biometrics";
|
|
176
|
+
import { CameraManager } from "@microblink/camera-manager/core";
|
|
177
|
+
|
|
178
|
+
const videoElement = document.querySelector("video")!;
|
|
179
|
+
|
|
180
|
+
const sdk = await createBiometrics({
|
|
181
|
+
licenseKey: "<license-key>",
|
|
182
|
+
resourcesLocation: window.location.origin,
|
|
183
|
+
onDownloadProgress(progress) {
|
|
184
|
+
console.log(`Loading ${progress.progress}%`);
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
const cameraManager = new CameraManager({ preferredResolution: "1080p" });
|
|
189
|
+
cameraManager.initVideoElement(videoElement);
|
|
190
|
+
|
|
191
|
+
const session = sdk.startSession({ captureTimeoutMs: 60_000 });
|
|
192
|
+
|
|
193
|
+
session.onEvent((event) => {
|
|
194
|
+
if (event.kind === "faceGuidance") {
|
|
195
|
+
console.log(event.feedback);
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// Return the promise so Camera Manager waits until the SDK releases the frame buffer.
|
|
200
|
+
const removeFrameCallback = cameraManager.addFrameCaptureCallback((frame) => session.processFrame(frame));
|
|
201
|
+
|
|
202
|
+
try {
|
|
203
|
+
await cameraManager.startCameraStream({ preferredFacing: "front" });
|
|
204
|
+
session.setCameraSource(videoElement, cameraManager.getState().selectedCamera?.getVideoTrack());
|
|
205
|
+
await cameraManager.startFrameCapture();
|
|
206
|
+
|
|
207
|
+
const result = await session.run().catch((error: unknown) => {
|
|
208
|
+
if (error instanceof BiometricsError && error.isRetryable) {
|
|
209
|
+
return session.retry();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
throw error;
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
console.log(result.bestImage, result.livenessFrames, result.captureFrame);
|
|
216
|
+
} finally {
|
|
217
|
+
removeFrameCallback();
|
|
218
|
+
cameraManager.stopStream();
|
|
219
|
+
session.finish();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// When the application no longer needs face capture.
|
|
223
|
+
await sdk.close();
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
Keep one SDK instance for the lifetime of the page and start a new session for every capture. Sessions follow these rules:
|
|
227
|
+
|
|
228
|
+
- `run()` starts capture once per session. Frames passed to `processFrame()` before `run()` or after the capture completes are returned unprocessed.
|
|
229
|
+
- `processFrame()` owns its input buffer until the returned promise resolves. Await it before you submit another frame. The resolved buffer is attached and can be reused.
|
|
230
|
+
- `setCameraSource()` passes the camera video element and track to the capture engine. It applies to subsequent capture attempts, so call it before `run()` or `retry()` and again after switching cameras.
|
|
231
|
+
- `retry()` starts a new capture attempt after a retryable failure. Non-retryable failures end the session.
|
|
232
|
+
- `finish()` cancels any active capture and releases the session. Call it on every exit path.
|
|
233
|
+
|
|
234
|
+
Use `onEvent` for face guidance, `captureTechnicalData` (face bounds and landmarks), `captureFinished`, and `captureTimeout` events. Use `subscribe` to follow the session state through the `idle`, `capturing`, `succeeded`, `failed`, and `finished` phases.
|
|
235
|
+
|
|
236
|
+
### Session context
|
|
237
|
+
|
|
238
|
+
Call `session.getSessionContext()` to get the identifiers of the capture session:
|
|
239
|
+
|
|
240
|
+
```ts
|
|
241
|
+
const { traceId, sessionNumber } = await session.getSessionContext();
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
Send both values together. The context is available before `session.run()` resolves. The capture result also includes these values.
|
|
245
|
+
|
|
246
|
+
## Capture result
|
|
247
|
+
|
|
248
|
+
Every guided and headless capture returns `FaceCaptureResult`:
|
|
249
|
+
|
|
250
|
+
- `traceId` and `sessionNumber` correlate the capture with backend requests.
|
|
251
|
+
- `bestImage` is the selected user-visible selfie as `ImageData` with face landmarks.
|
|
252
|
+
- `supportingImages` contains other selected capture images.
|
|
253
|
+
- `captureFrame` is the optional engine-produced JPEG frame for face matching.
|
|
254
|
+
- `livenessFrames` contains ordered engine-produced QOI frames for liveness checks.
|
|
255
|
+
- `livenessBatchSignature` contains the optional batch signature.
|
|
256
|
+
|
|
257
|
+
Frame and batch signatures are present only when the native engine produces them. When the engine does not produce capture or liveness frames, `captureFrame` is undefined and `livenessFrames` is empty.
|
|
258
|
+
|
|
259
|
+
## WebAssembly runtime
|
|
260
|
+
|
|
261
|
+
The SDK selects a supported `simd`, `simd-threads`, `simd-relaxed`, or `simd-relaxed-threads` WebAssembly variant. Multithreaded variants require the cross-origin isolation headers listed in the [Quickstart](#3-serve-the-application).
|
|
262
|
+
|
|
263
|
+
Use `wasmVariant` to force a variant for testing:
|
|
264
|
+
|
|
265
|
+
```ts
|
|
266
|
+
await createBiometricsUi({
|
|
267
|
+
licenseKey: "<license-key>",
|
|
268
|
+
wasmVariant: "simd",
|
|
269
|
+
});
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
A forced variant that the browser does not support fails initialization.
|
|
273
|
+
|
|
274
|
+
## Errors
|
|
275
|
+
|
|
276
|
+
The SDK reports failures as `BiometricsError`. Use `code` to identify the failure, `stage` and `component` to locate it, and `isRetryable` to decide whether to offer a retry. `LicenseError`, `PermissionError`, `ConfigurationError`, and `SessionError` extend `BiometricsError`.
|
|
277
|
+
|
|
278
|
+
## Analytics
|
|
279
|
+
|
|
280
|
+
Analytics are queued and sent through the native WASM PingV3 transport. Use `analytics.userId` and `analytics.pingProxyUrl` to configure the transport. Set `analytics.enabled` to `false` to disable top-level capture metadata events.
|
|
281
|
+
|
|
282
|
+
## Development
|
|
283
|
+
|
|
284
|
+
```sh
|
|
285
|
+
pnpm install
|
|
286
|
+
pnpm --filter @microblink/biometrics build
|
|
287
|
+
pnpm --filter @microblink/biometrics test
|
|
288
|
+
```
|