facecore 0.1.0
facecore: ^0.1.0 copied to clipboard
Self-contained Flutter plugin for camera-based face liveness, depth, and anti-spoof checks before capturing a photo on Android and iOS.
example/lib/main.dart
import 'dart:io';
import 'package:facecore/facecore.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _facecore = Facecore();
final Set<LivenessAction> _selectedActions = {
LivenessAction.blink,
// LivenessAction.turnLeft,
// LivenessAction.turnRight,
};
FaceCaptureResult? _result;
String? _error;
bool _busy = false;
Future<void> _capture() async {
if (_selectedActions.isEmpty) {
setState(() => _error = 'Pick at least one liveness action.');
return;
}
setState(() {
_busy = true;
_error = null;
});
try {
final result = await _facecore.captureWithLiveness(
config: LivenessConfig(
actions: _actionsInOrder(),
title: 'Verify your identity',
subtitle: 'Follow the prompts to confirm you are present.',
),
);
setState(() => _result = result);
} on FaceCaptureException catch (e) {
setState(() => _error = e.toString());
} finally {
if (mounted) setState(() => _busy = false);
}
}
List<LivenessAction> _actionsInOrder() => [
for (final action in LivenessAction.values)
if (_selectedActions.contains(action)) action,
];
String _label(LivenessAction action) => switch (action) {
LivenessAction.blink => 'Blink',
LivenessAction.turnLeft => 'Turn left',
LivenessAction.turnRight => 'Turn right',
};
@override
Widget build(BuildContext context) {
final result = _result;
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Facecore Liveness Demo')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text(
'Liveness checks',
style: TextStyle(fontWeight: FontWeight.w600),
),
Wrap(
spacing: 8,
children: [
for (final action in LivenessAction.values)
FilterChip(
label: Text(_label(action)),
selected: _selectedActions.contains(action),
onSelected: _busy
? null
: (selected) => setState(() {
if (selected) {
_selectedActions.add(action);
} else {
_selectedActions.remove(action);
}
}),
),
],
),
const SizedBox(height: 16),
FilledButton(
onPressed: _busy ? null : _capture,
child: Text(_busy ? 'Working…' : 'Start liveness capture'),
),
const SizedBox(height: 16),
if (_error != null)
Text(_error!, style: const TextStyle(color: Colors.red)),
if (result != null) ...[
Text('Saved to: ${result.imagePath}'),
Text('Size: ${result.width}×${result.height}'),
Text('Motion stddev: ${result.motionStdDev.toStringAsFixed(2)}'),
if (result.depthRange != null)
Text('Depth range: ${result.depthRange!.toStringAsFixed(2)}'),
const SizedBox(height: 12),
Expanded(
child: Image.file(File(result.imagePath), fit: BoxFit.contain),
),
],
],
),
),
),
);
}
}