mirror of
https://gitee.com/openharmony/applications_app_samples
synced 2024-11-23 08:30:06 +00:00
add sample for FaceRecognition
Signed-off-by: wangzongmao <wangzongmao@huawei.com>
This commit is contained in:
parent
f6f5c2ea91
commit
ff0e435cf7
5
security/FaceRecognition/README.md
Normal file
5
security/FaceRecognition/README.md
Normal file
@ -0,0 +1,5 @@
|
||||
# Facial Recognition<a name="EN-US_TOPIC_0000001080120330"></a>
|
||||
|
||||
- Currently, 2D and 3D facial recognition are supported and can be used in identity authentication scenarios such as device unlocking, application login, and payment.
|
||||
|
||||
|
6
security/FaceRecognition/README_zh.md
Normal file
6
security/FaceRecognition/README_zh.md
Normal file
@ -0,0 +1,6 @@
|
||||
# 人脸识别<a name="ZH-CN_TOPIC_0000001080120330"></a>
|
||||
|
||||
- 当前生物特征识别支持2D人脸识别、3D人脸识别,可应用于设备解锁、应用登录、支付等身份认证场景。
|
||||
|
||||
|
||||
|
34
security/FaceRecognition/build.gradle
Normal file
34
security/FaceRecognition/build.gradle
Normal file
@ -0,0 +1,34 @@
|
||||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
apply plugin: 'com.huawei.ohos.app'
|
||||
|
||||
ohos {
|
||||
compileSdkVersion 5
|
||||
defaultConfig {
|
||||
compatibleSdkVersion 4
|
||||
}
|
||||
}
|
||||
buildscript {
|
||||
repositories {
|
||||
maven {
|
||||
url 'https://repo.huaweicloud.com/repository/maven/'
|
||||
}
|
||||
maven {
|
||||
url 'https://developer.huawei.com/repo/'
|
||||
}
|
||||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.huawei.ohos:hap:2.4.4.2'
|
||||
}
|
||||
}
|
||||
allprojects {
|
||||
repositories {
|
||||
maven {
|
||||
url 'https://repo.huaweicloud.com/repository/maven/'
|
||||
}
|
||||
maven {
|
||||
url 'https://developer.huawei.com/repo/'
|
||||
}
|
||||
jcenter()
|
||||
}
|
||||
}
|
18
security/FaceRecognition/entry/build.gradle
Normal file
18
security/FaceRecognition/entry/build.gradle
Normal file
@ -0,0 +1,18 @@
|
||||
apply plugin: 'com.huawei.ohos.hap'
|
||||
ohos {
|
||||
compileSdkVersion 5
|
||||
defaultConfig {
|
||||
compatibleSdkVersion 4
|
||||
}
|
||||
buildTypes {
|
||||
release {
|
||||
proguardOpt {
|
||||
proguardEnabled false
|
||||
rulesFiles 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
dependencies {
|
||||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||
}
|
53
security/FaceRecognition/entry/src/main/config.json
Normal file
53
security/FaceRecognition/entry/src/main/config.json
Normal file
@ -0,0 +1,53 @@
|
||||
{
|
||||
"app": {
|
||||
"bundleName": "ohos.samples.facerecognition",
|
||||
"version": {
|
||||
"code": 1000000,
|
||||
"name": "1.0"
|
||||
}
|
||||
},
|
||||
"deviceConfig": {},
|
||||
"module": {
|
||||
"package": "ohos.samples.facerecognition",
|
||||
"name": ".MainAbility",
|
||||
"reqCapabilities": [
|
||||
"video_support"
|
||||
],
|
||||
"deviceType": [
|
||||
"default"
|
||||
],
|
||||
"distro": {
|
||||
"deliveryWithInstall": true,
|
||||
"moduleName": "entry",
|
||||
"moduleType": "entry",
|
||||
"installationFree":false
|
||||
},
|
||||
"abilities": [
|
||||
{
|
||||
"skills": [
|
||||
{
|
||||
"entities": [
|
||||
"entity.system.home"
|
||||
],
|
||||
"actions": [
|
||||
"action.system.home"
|
||||
]
|
||||
}
|
||||
],
|
||||
"orientation": "portrait",
|
||||
"formsEnabled": false,
|
||||
"name": ".MainAbility",
|
||||
"icon": "$media:icon",
|
||||
"description": "$string:mainability_description",
|
||||
"label": "$string:app_name",
|
||||
"type": "page",
|
||||
"launchType": "standard"
|
||||
}
|
||||
],
|
||||
"reqPermissions": [
|
||||
{
|
||||
"name": "ohos.permission.ACCESS_BIOMETRIC"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ohos.samples.facerecognition;
|
||||
|
||||
import ohos.samples.facerecognition.slice.MainAbilitySlice;
|
||||
|
||||
import ohos.aafwk.ability.Ability;
|
||||
import ohos.aafwk.content.Intent;
|
||||
|
||||
/**
|
||||
* Facerecognition MainAbility
|
||||
*/
|
||||
public class MainAbility extends Ability {
|
||||
@Override
|
||||
public void onStart(Intent intent) {
|
||||
super.onStart(intent);
|
||||
super.setMainRoute(MainAbilitySlice.class.getName());
|
||||
}
|
||||
}
|
@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ohos.samples.facerecognition.slice;
|
||||
|
||||
import ohos.samples.facerecognition.ResourceTable;
|
||||
import ohos.samples.facerecognition.utils.ThreadPoolUtil;
|
||||
|
||||
import ohos.aafwk.ability.AbilitySlice;
|
||||
import ohos.aafwk.content.Intent;
|
||||
import ohos.agp.components.Component;
|
||||
import ohos.agp.components.LayoutScatter;
|
||||
import ohos.agp.components.Text;
|
||||
import ohos.agp.window.dialog.CommonDialog;
|
||||
import ohos.biometrics.authentication.BiometricAuthentication;
|
||||
import ohos.eventhandler.EventHandler;
|
||||
import ohos.eventhandler.EventRunner;
|
||||
import ohos.eventhandler.InnerEvent;
|
||||
|
||||
/**
|
||||
* MainAbilitySlice
|
||||
*/
|
||||
public class MainAbilitySlice extends AbilitySlice {
|
||||
private static final int EVENT_MESSAGE_SUCCESS = 0x1000001;
|
||||
|
||||
private static final int EVENT_MESSAGE_FAIL = 0x1000002;
|
||||
|
||||
private static final int BA_CHECK_NOT_ENROLLED = 4;
|
||||
|
||||
private Text resultText;
|
||||
|
||||
private String result;
|
||||
|
||||
private EventHandler handler = new EventHandler(EventRunner.current()) {
|
||||
@Override
|
||||
protected void processEvent(InnerEvent event) {
|
||||
switch (event.eventId) {
|
||||
case EVENT_MESSAGE_SUCCESS:
|
||||
showDialog(result);
|
||||
resultText.setText(result);
|
||||
break;
|
||||
case EVENT_MESSAGE_FAIL:
|
||||
resultText.setText(result);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private BiometricAuthentication biometricAuthentication;
|
||||
|
||||
@Override
|
||||
public void onStart(Intent intent) {
|
||||
super.onStart(intent);
|
||||
super.setUIContent(ResourceTable.Layout_main_ability_slice);
|
||||
initComponents();
|
||||
}
|
||||
|
||||
private void initComponents() {
|
||||
resultText = (Text) findComponentById(ResourceTable.Id_result_text);
|
||||
Component startButton = findComponentById(ResourceTable.Id_start);
|
||||
Component cancelButton = findComponentById(ResourceTable.Id_cancel);
|
||||
startButton.setClickedListener(this::startFaceUnlock);
|
||||
cancelButton.setClickedListener(this::cancelRecognition);
|
||||
}
|
||||
|
||||
private synchronized void startFaceUnlock(Component component) {
|
||||
try {
|
||||
biometricAuthentication = BiometricAuthentication.getInstance(getAbility());
|
||||
int availability = biometricAuthentication.checkAuthenticationAvailability(
|
||||
BiometricAuthentication.AuthType.AUTH_TYPE_BIOMETRIC_FACE_ONLY,
|
||||
BiometricAuthentication.SecureLevel.SECURE_LEVEL_S2, true);
|
||||
if (availability == 0) {
|
||||
execAuthentication();
|
||||
} else if (availability == BA_CHECK_NOT_ENROLLED) {
|
||||
result = "Face Unlock not enrolled, set up face Recognition first";
|
||||
} else {
|
||||
result = "Biometric authentication not support";
|
||||
}
|
||||
|
||||
} catch (IllegalAccessException e) {
|
||||
result = "Face Recognition error ";
|
||||
}
|
||||
handler.sendEvent(EVENT_MESSAGE_FAIL);
|
||||
}
|
||||
|
||||
private void execAuthentication() {
|
||||
ThreadPoolUtil.submit(() -> {
|
||||
int authenticationAction = biometricAuthentication.execAuthenticationAction(
|
||||
BiometricAuthentication.AuthType.AUTH_TYPE_BIOMETRIC_FACE_ONLY,
|
||||
BiometricAuthentication.SecureLevel.SECURE_LEVEL_S4, true, false, null);
|
||||
if (authenticationAction == 0) {
|
||||
result = "Unlocked successfully";
|
||||
handler.sendEvent(EVENT_MESSAGE_SUCCESS);
|
||||
} else {
|
||||
result = "Authentication failed, face biometric doesn't match";
|
||||
handler.sendEvent(EVENT_MESSAGE_FAIL);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void cancelRecognition(Component component) {
|
||||
if (biometricAuthentication != null) {
|
||||
biometricAuthentication.cancelAuthenticationAction();
|
||||
}
|
||||
}
|
||||
|
||||
private void showDialog(String message) {
|
||||
Component container = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_dialog_layout, null, false);
|
||||
Text content = (Text) container.findComponentById(ResourceTable.Id_message);
|
||||
content.setText("Recognition result:" + System.lineSeparator() + message);
|
||||
CommonDialog commonDialog = new CommonDialog(this);
|
||||
commonDialog.setSize(900, 400);
|
||||
commonDialog.setCornerRadius(30);
|
||||
commonDialog.setContentCustomComponent(container);
|
||||
commonDialog.show();
|
||||
Component btnOk = container.findComponentById(ResourceTable.Id_btn_ok);
|
||||
btnOk.setClickedListener(component -> commonDialog.hide());
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ohos.samples.facerecognition.utils;
|
||||
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* Global thread pool
|
||||
*/
|
||||
public class ThreadPoolUtil {
|
||||
private static final int CORE_COUNT = 10;
|
||||
|
||||
private static final int THREAD_COUNT = 20;
|
||||
|
||||
private static final int WORK_QUEUE_SIZE = 50;
|
||||
|
||||
private static final long KEEP_ALIVE = 10L;
|
||||
|
||||
private static final AtomicInteger THREAD_ID = new AtomicInteger(1);
|
||||
|
||||
private static ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_COUNT, THREAD_COUNT, KEEP_ALIVE,
|
||||
TimeUnit.SECONDS, new ArrayBlockingQueue<>(WORK_QUEUE_SIZE), new CommonThreadFactory());
|
||||
|
||||
private ThreadPoolUtil() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit task to execute
|
||||
*
|
||||
* @param task runnable task
|
||||
*/
|
||||
public static void submit(Runnable task) {
|
||||
executor.submit(task);
|
||||
}
|
||||
|
||||
/**
|
||||
* ThreadFactory
|
||||
*
|
||||
*/
|
||||
static class CommonThreadFactory implements ThreadFactory {
|
||||
@Override
|
||||
public Thread newThread(Runnable runnable) {
|
||||
String threadName = null;
|
||||
if (THREAD_ID.get() == Integer.MAX_VALUE) {
|
||||
threadName = "threadpool-common-" + THREAD_ID.getAndSet(1);
|
||||
} else {
|
||||
threadName = "threadpool-common-" + THREAD_ID.incrementAndGet();
|
||||
}
|
||||
return new Thread(runnable, threadName);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
{
|
||||
"string": [
|
||||
{
|
||||
"name": "app_name",
|
||||
"value": "FaceRecognition"
|
||||
},
|
||||
{
|
||||
"name": "mainability_description",
|
||||
"value": "hap sample empty page"
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
|
||||
ohos:shape="rectangle">
|
||||
<corners
|
||||
ohos:radius="75"/>
|
||||
<solid
|
||||
ohos:color="#0d000000"/>
|
||||
</shape>
|
@ -0,0 +1,50 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<DirectionalLayout
|
||||
xmlns:ohos="http://schemas.huawei.com/res/ohos"
|
||||
ohos:height="match_parent"
|
||||
ohos:width="300vp"
|
||||
ohos:alignment="center"
|
||||
ohos:orientation="vertical"
|
||||
ohos:padding="5vp">
|
||||
|
||||
<Text
|
||||
ohos:id="$+id:message"
|
||||
ohos:height="match_content"
|
||||
ohos:width="match_parent"
|
||||
ohos:multiple_lines="true"
|
||||
ohos:text_alignment="center"
|
||||
ohos:text_size="20fp"
|
||||
ohos:top_margin="10vp"/>
|
||||
|
||||
<Text
|
||||
ohos:height="1vp"
|
||||
ohos:width="match_parent"
|
||||
ohos:background_element="#220000ff"
|
||||
ohos:bottom_margin="5vp"
|
||||
ohos:text_alignment="center"
|
||||
ohos:text_size="18fp"
|
||||
ohos:top_margin="5vp"/>
|
||||
|
||||
<Button
|
||||
ohos:id="$+id:btn_ok"
|
||||
ohos:height="match_content"
|
||||
ohos:width="match_parent"
|
||||
ohos:margin="10vp"
|
||||
ohos:text="O K"
|
||||
ohos:text_alignment="center"
|
||||
ohos:text_size="20fp"/>
|
||||
</DirectionalLayout>
|
@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<DirectionalLayout
|
||||
xmlns:ohos="http://schemas.huawei.com/res/ohos"
|
||||
ohos:height="match_parent"
|
||||
ohos:width="match_parent"
|
||||
ohos:alignment="center"
|
||||
ohos:orientation="vertical"
|
||||
ohos:padding="5vp">
|
||||
|
||||
<Text
|
||||
ohos:height="match_content"
|
||||
ohos:width="match_content"
|
||||
ohos:margin="20vp"
|
||||
ohos:multiple_lines="true"
|
||||
ohos:text="Align the face with the front camera and press start button to unlock the face."
|
||||
ohos:text_size="16fp"/>
|
||||
|
||||
<Button
|
||||
ohos:id="$+id:start"
|
||||
ohos:height="40vp"
|
||||
ohos:width="240vp"
|
||||
ohos:background_element="$graphic:button_bg"
|
||||
ohos:margin="10vp"
|
||||
ohos:text="Start Face Unlock"
|
||||
ohos:text_size="16fp"/>
|
||||
|
||||
<Button
|
||||
ohos:id="$+id:cancel"
|
||||
ohos:height="40vp"
|
||||
ohos:width="240vp"
|
||||
ohos:background_element="$graphic:button_bg"
|
||||
ohos:margin="10vp"
|
||||
ohos:text="Cancel Authentication"
|
||||
ohos:text_size="16fp"/>
|
||||
|
||||
<Text
|
||||
ohos:id="$+id:result_text"
|
||||
ohos:height="match_content"
|
||||
ohos:width="match_content"
|
||||
ohos:margin="20vp"
|
||||
ohos:multiple_lines="true"
|
||||
ohos:text_size="16fp"/>
|
||||
</DirectionalLayout>
|
Binary file not shown.
After Width: | Height: | Size: 6.6 KiB |
BIN
security/FaceRecognition/screenshots/phone/fail.png
Normal file
BIN
security/FaceRecognition/screenshots/phone/fail.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 83 KiB |
BIN
security/FaceRecognition/screenshots/phone/main.png
Normal file
BIN
security/FaceRecognition/screenshots/phone/main.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 68 KiB |
BIN
security/FaceRecognition/screenshots/phone/success.png
Normal file
BIN
security/FaceRecognition/screenshots/phone/success.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 78 KiB |
1
security/FaceRecognition/settings.gradle
Normal file
1
security/FaceRecognition/settings.gradle
Normal file
@ -0,0 +1 @@
|
||||
include ':entry'
|
Loading…
Reference in New Issue
Block a user