Skip to content

Commit

Permalink
fixed issue
Browse files Browse the repository at this point in the history
  • Loading branch information
justin200914 committed Jul 18, 2023
1 parent 44e7085 commit 5b8ffdd
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class FaceRecognitionSdkViewManager(private val reactContext: ReactApplicationCo
fun setLivenessLevel(view: View, livenessLevel: Int) {
Log.e("TestEngine", "setLivenessLevel " + livenessLevel)
this.checkLivenessLevel = livenessLevel

startCamera()
}

@ReactProp(name = "cameraLens")
Expand Down
6 changes: 3 additions & 3 deletions android/src/main/java/com/facerecognitionsdk/FaceSDKModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,20 @@ class FaceSDKModule(reactContext: ReactApplicationContext) : ReactContextBaseJav
@ReactMethod
fun similarityCalculation(templates1: String, templates2: String, promise: Promise) {
val similarity = FaceSDK.similarityCalculation(Utils.base64ToByteArray(templates1), Utils.base64ToByteArray(templates2))
Log.e("TestEngine", "similarityCalculation" + similarity)
Log.e("KBY", "similarityCalculation" + similarity)
promise.resolve(similarity)
}

@ReactMethod
fun startCamera(promise: Promise) {
Log.e("TestEngine", "start camera" + faceRecognitionSdkViewManager);
Log.e("KBY", "start camera" + faceRecognitionSdkViewManager);
faceRecognitionSdkViewManager?.startCamera()
promise.resolve(0)
}

@ReactMethod
fun stopCamera(promise: Promise) {
Log.e("TestEngine", "stop camera");
Log.e("KBY", "stop camera");
faceRecognitionSdkViewManager?.stopCamera()
promise.resolve(0)
}
Expand Down
49 changes: 30 additions & 19 deletions android/src/main/java/com/facerecognitionsdk/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import android.graphics.Matrix
import android.net.Uri
import android.provider.MediaStore
import android.util.Base64

import android.util.Log
import com.kbyai.facesdk.FaceBox

import android.media.ExifInterface
import java.io.IOException
import java.io.InputStream

Expand Down Expand Up @@ -59,28 +59,39 @@ object Utils {

@Throws(IOException::class)
fun getCorrectlyOrientedImage(context: Context, photoUri: Uri): Bitmap {
var isStream: InputStream? = context.contentResolver.openInputStream(photoUri)
val dbo = BitmapFactory.Options()
dbo.inJustDecodeBounds = true
BitmapFactory.decodeStream(isStream, null, dbo)
isStream?.close()

val orientation = getOrientation(context, photoUri)
var rotate = 0
val inputStream: InputStream? = context.contentResolver.openInputStream(photoUri)
inputStream?.let {
val exif = ExifInterface(it)
val orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL
)
rotate = when (orientation) {
ExifInterface.ORIENTATION_ROTATE_270 -> 270
ExifInterface.ORIENTATION_ROTATE_180 -> 180
ExifInterface.ORIENTATION_ROTATE_90 -> 90
else -> 0
}
it.close()
}

var srcBitmap: Bitmap
isStream = context.contentResolver.openInputStream(photoUri)
val isStream = context.contentResolver.openInputStream(photoUri)
srcBitmap = BitmapFactory.decodeStream(isStream)
isStream?.close()

if (orientation > 0) {
val matrix = Matrix()
matrix.postRotate(orientation.toFloat())

srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.width,
srcBitmap.height, matrix, true)
}

return srcBitmap
val matrix = Matrix()
matrix.postRotate(rotate.toFloat())
return Bitmap.createBitmap(
srcBitmap,
0,
0,
srcBitmap.width,
srcBitmap.height,
matrix,
true
)
}

fun byteArrayToBase64(byteArray: ByteArray): String {
Expand Down
79 changes: 61 additions & 18 deletions example/src/FaceRecognitionPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// FaceRecognitionPage.js
import React, { useEffect, useState } from 'react';
import React, { useEffect, useState, useRef } from 'react';
import { View, Text, StyleSheet, TouchableOpacity, Image, } from 'react-native';
import { FaceRecognitionSdkView, FaceSDKModule } from 'face-recognition-sdk';
import { check, request, PERMISSIONS, RESULTS } from 'react-native-permissions'
Expand All @@ -13,10 +13,57 @@ const cameraPermission = Platform.select({
android: PERMISSIONS.ANDROID.CAMERA,
})

const FaceRecognitionView = () => {
const isFocused = useIsFocused();
const sdkViewRef = useRef(null);

useEffect(() => {
if (isFocused) {
startCamera();
} else {
stopCamera();
}

return () => {
stopCamera();
};
}, [isFocused]);

const startCamera = async () => {
await FaceSDKModule.startCamera();
};

const stopCamera = async () => {
await FaceSDKModule.stopCamera();
};

const handleViewLayout = () => {
if (isFocused && sdkViewRef.current) {
startCamera();
} else {
stopCamera();
}
};

return (
<View style={styles.container}>
<View style={styles.box} onLayout={handleViewLayout}>
<FaceRecognitionSdkView
ref={sdkViewRef}
style={{ flex: 1 }}
livenessLevel={1}
cameraLens={1}
/>
</View>
</View>
);
};

const FaceRecognitionPage = ({ navigation, route }) => {

const { persons } = route.params;
const [faces, setFaces] = useState([]);
const [cameraShow, setCameraShow] = useState(false);
const isFocused = useIsFocused();

var recognized = false;
Expand All @@ -26,7 +73,6 @@ const FaceRecognitionPage = ({ navigation, route }) => {
};

useEffect(() => {
console.log("isFocused", isFocused);
checkPermission();

const eventEmitter = new NativeEventEmitter(FaceSDKModule);
Expand All @@ -37,11 +83,12 @@ const FaceRecognitionPage = ({ navigation, route }) => {
}
});

return () => {
stopCamera();
// AppState.removeEventListener('change', handleAppStateChange);
// BackHandler.removeEventListener('hardwareBackPress', handleBackButton);
if(isFocused)
recognized = false;
else
recognized = true;

return () => {
eventListener.remove();
};

Expand Down Expand Up @@ -96,15 +143,6 @@ const FaceRecognitionPage = ({ navigation, route }) => {
}
};


const startCamera = async () => {
await FaceSDKModule.startCamera();
}

const stopCamera = async () => {
await FaceSDKModule.stopCamera();
}

const checkPermission = async () => {
const permissionStatus = await check(cameraPermission);
handlePermissionStatus(permissionStatus);
Expand All @@ -124,7 +162,7 @@ const FaceRecognitionPage = ({ navigation, route }) => {
requestPermission();
break;
case RESULTS.GRANTED:
startCamera();
setCameraShow(true);
break;
case RESULTS.BLOCKED:
break;
Expand All @@ -146,8 +184,13 @@ const FaceRecognitionPage = ({ navigation, route }) => {
</View>
</View>
<View style={styles.body}>
<FaceRecognitionSdkView style={styles.box} livenessLevel={1} cameraLens={1} />

{cameraShow ? (
<FaceRecognitionView />
) : (
<View>
<Text>Camera permission issue.</Text>
</View>
)}
<View
style={{
flex: 1,
Expand Down

0 comments on commit 5b8ffdd

Please sign in to comment.