This page provides comprehensive examples of how to integrate and use the PerchEye SDK across Android, iOS, Flutter, and React Native platforms. Each code sample is designed to be clear, concise, and ready to use in your own projects.
Add the PerchEye SDK to your Android project and perform the initial setup.
// Add the dependency dependencies { implementation(files("libs/perch-eye-1.0.3-4.aar")) }
import com.percheye.android.PerchEye import android.os.Bundle import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { private lateinit var perchEye: PerchEye override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Initialize the SDK perchEye = PerchEye(context) perchEye.init() } override fun onDestroy() { super.onDestroy() // Always destroy the SDK when done perchEye.destroy() } }
fun detectFace(bitmap: Bitmap): ImageResult { perchEye.openTransaction() val result = perchEye.addImage(bitmap) when (result) { ImageResult.SUCCESS -> Log.d("PerchEye", "Face detected successfully") ImageResult.FACE_NOT_FOUND -> Log.d("PerchEye", "No face found") else -> Log.d("PerchEye", "Error: $result") } return result }
fun enrollFace(bitmap: Bitmap): String? { perchEye.openTransaction() val result = perchEye.addImage(bitmap) if (result == ImageResult.SUCCESS) { val hash = perchEye.enroll() saveUserFaceHash(userId, hash) return hash } return null }
fun verifyFace(bitmap: Bitmap, storedHash: String): Float { perchEye.openTransaction() val result = perchEye.addImage(bitmap) if (result == ImageResult.SUCCESS) { val similarity = perchEye.verify(storedHash) return similarity } return 0.0f }
Add the PerchEye framework to your iOS project and perform the initial setup.
# Download PerchEye Framework from the official source
# Drag PerchEyeFramework.xcframework into your Xcode project
# Ensure the framework is added to Frameworks, Libraries, and Embedded Content
# Set Embed & Sign for the framework
import PerchEyeFramework class ViewController: UIViewController { private var perchEye: PerchEyeSwift! override func viewDidLoad() { super.viewDidLoad() // Initialize the SDK perchEye = PerchEyeSwift() } deinit { // SDK automatically destroys on deinit perchEye.destroy() } }
func detectFace(image: UIImage) -> ImageResult { perchEye.openTransaction() let result = perchEye.load(image: image) switch result { case .success: print("Face detected successfully") case .faceNotFound: print("No face found") default: print("Error: \(result)") } return result }
func enrollFace(image: UIImage) -> String? { perchEye.openTransaction() let result = perchEye.load(image: image) if result == .success { let hash = perchEye.enroll() saveUserFaceHash(userId: userId, hash: hash) return hash } return nil }
func verifyFace(image: UIImage, storedHash: String) -> Float { perchEye.openTransaction() let result = perchEye.load(image: image) if result == .success { let similarity = perchEye.verify(hash: storedHash) return similarity } return 0.0 }
Add the PerchEye plugin to your Flutter project and perform the initial setup.
# Run this in your Flutter project: flutter pub add perch_eye
import 'package:perch_eye/perch_eye.dart'; class FaceRecognitionService { FutureinitializeSDK() async { // Initialize the SDK await PerchEye.init(); } Future cleanup() async { // Cleanup when done await PerchEye.destroy(); } }
FuturedetectFace(String base64Image) async { await PerchEye.openTransaction(); final result = await PerchEye.addImage(base64Image); switch (result) { case 'SUCCESS': print('Face detected successfully'); break; case 'FACE_NOT_FOUND': print('No face found'); break; default: print('Error: $result'); } return result; }
FutureenrollFace(String base64Image) async { await PerchEye.openTransaction(); final result = await PerchEye.addImage(base64Image); if (result == 'SUCCESS') { final hash = await PerchEye.enroll(); await saveUserFaceHash(userId, hash); return hash; } return null; }
FutureverifyFace(String base64Image, String storedHash) async { await PerchEye.openTransaction(); final result = await PerchEye.addImage(base64Image); if (result == 'SUCCESS') { final similarity = await PerchEye.verify(storedHash); return similarity; } return 0.0; }
Add the PerchEye module to your React Native project and perform the initial setup.
# Using npm npm install react-native-perch-eye # Using yarn yarn add react-native-perch-eye # For iOS, run pod install cd ios && pod install
import { init, destroy } from 'react-native-perch-eye'; class FaceRecognitionService { async initializeSDK() { // Initialize the SDK await init(); } async cleanup() { // Cleanup when done await destroy(); } }
import { openTransaction, addImage } from 'react-native-perch-eye'; async function detectFace(base64Image) { await openTransaction(); const result = await addImage(base64Image); switch (result) { case 'SUCCESS': console.log('Face detected successfully'); break; case 'FACE_NOT_FOUND': console.log('No face found'); break; default: console.log(`Error: ${result}`); } return result; }
import { openTransaction, addImage, enroll } from 'react-native-perch-eye'; async function enrollFace(base64Image) { await openTransaction(); const result = await addImage(base64Image); if (result === 'SUCCESS') { const hash = await enroll(); await saveUserFaceHash(userId, hash); return hash; } return null; }
import { openTransaction, addImage, verify } from 'react-native-perch-eye'; async function verifyFace(base64Image, storedHash) { await openTransaction(); const result = await addImage(base64Image); if (result === 'SUCCESS') { const similarity = await verify(storedHash); return similarity; } return 0.0; }