PerchEye Logo PerchEye SDK

PerchEye SDK Integration Examples

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.

1. Getting Started

Add the PerchEye SDK to your Android project and perform the initial setup.

Add Dependencies

// Add the dependency
dependencies {
    implementation(files("libs/perch-eye-1.0.3-4.aar"))
}

Initialize the SDK

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()
    }
}

2. Face Detection

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
}

3. Face Enrollment

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
}

4. Face Verification

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
}

1. Getting Started

Add the PerchEye framework to your iOS project and perform the initial setup.

Add Dependencies

# 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

Initialize the SDK

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()
    }
}

2. Face Detection

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
}

3. Face Enrollment

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
}

4. Face Verification

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
}

1. Getting Started

Add the PerchEye plugin to your Flutter project and perform the initial setup.

Add Dependencies

# Run this in your Flutter project:

flutter pub add perch_eye

Initialize the SDK

import 'package:perch_eye/perch_eye.dart';

class FaceRecognitionService {

  Future initializeSDK() async {
    // Initialize the SDK
    await PerchEye.init();
  }

  Future cleanup() async {
    // Cleanup when done
    await PerchEye.destroy();
  }
}

2. Face Detection

Future detectFace(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;
}

3. Face Enrollment

Future enrollFace(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;
}

4. Face Verification

Future verifyFace(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;
}

1. Getting Started

Add the PerchEye module to your React Native project and perform the initial setup.

Add Dependencies

# 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

Initialize the SDK

import { init, destroy } from 'react-native-perch-eye';

class FaceRecognitionService {

  async initializeSDK() {
    // Initialize the SDK
    await init();
  }

  async cleanup() {
    // Cleanup when done
    await destroy();
  }
}

2. Face Detection

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;
}

3. Face Enrollment

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;
}

4. Face Verification

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;
}
Note: All platform implementations provide consistent APIs and return values. The similarity threshold of 0.8 is recommended for most use cases, but you can adjust it based on your security requirements.
Tip: For better accuracy across all platforms, use multiple images of the same face from different angles and lighting conditions when enrolling.