HarmonyOS鸿蒙Next中uniapp怎么实现人脸识别功能

HarmonyOS鸿蒙Next中uniapp怎么实现人脸识别功能 uniapp怎么实现人脸识别功能 原生的不支持鸿蒙端

cke_258.png


更多关于HarmonyOS鸿蒙Next中uniapp怎么实现人脸识别功能的实战教程也可以访问 https://www.itying.com/category-93-b0.html

5 回复

可以使用uts插件调用鸿蒙端人体监测功能

需要申请ohos.permission.CAMERA权限

import { BusinessError } from '@kit.BasicServicesKit';
import { interactiveLiveness } from '@kit.VisionKit';
import { abilityAccessCtrl, PermissionRequestResult, Permissions } from '@kit.AbilityKit';
import { image } from '@kit.ImageKit';

import { DetectOptions } from '../interface.uts'

// Permission.ets
function grant(permission: Permissions): Promise<boolean> {
    return grants([permission]);
}

function grants(permissions: Array<Permissions>): Promise<boolean> {
    return new Promise(async (resolve, reject) => {
        try {
            let context = getContext();

            let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
            let permissionResult: PermissionRequestResult = await atManager.requestPermissionsFromUser(context, permissions);
            let grantStatus: Array<number> = permissionResult.authResults;
            let length: number = grantStatus.length;

            let flag = false;
            for (let i = 0; i < length; i++) {
                if (grantStatus[i] === 0) {
                    // 用户授权,可以继续访问目标操作
                    flag = true;
                    // 只有一个权限
                    break;
                } else {
                    // 用户拒绝授权,提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限
                }
            }
            resolve(flag);
        } catch (err) {
            reject(err);
        }
    });
}

// ImageUtil.ets
function pixelMapToArrayBuffer(mPixelMap: image.PixelMap, format: string = "image/jpeg", quality: number = 100): Promise<ArrayBuffer> {
    return new Promise(async (resolve, reject) => {
        try {
            const imagePackerApi = image.createImagePacker();
            let packOpts : image.PackingOption = { format, quality };
            const data = await imagePackerApi.packing(mPixelMap, packOpts)
            resolve(data)
        } catch (error) {
            reject(error);
        }
    })
}

// LivenessUtil.ets
const DEFAULT_LIVENESS_CONFIG: interactiveLiveness.InteractiveLivenessConfig = {
    isSilentMode: "INTERACTIVE_MODE" as interactiveLiveness.DetectionMode,
    routeMode: "back" as interactiveLiveness.RouteRedirectionMode,
    actionsNum: 3
}

const BUSINESS_ERROR_CODES = [1008301002, 1008302000, 1008302001, 1008302002, 1008302003, 1008302004];
const DETECTION_MODES: Array<String> = ['INTERACTIVE_MODE', 'SILENT_MODE']
const ACTIONS_NUMS: Array<Number> = [1, 2, 3, 4]

function _livenessDetection(livenessConfig: interactiveLiveness.InteractiveLivenessConfig): Promise<interactiveLiveness.InteractiveLivenessResult> {
    return new Promise(async (resolve, reject) => {
        try {
            const isGranted = await grant("ohos.permission.CAMERA");
            if (!isGranted) {
                reject({code: 201, message: "Permission denied"} as BusinessError)
                return;
            }
            interactiveLiveness.startLivenessDetection(livenessConfig, async (err, result: interactiveLiveness.InteractiveLivenessResult | undefined) => {
                if (err.code !== 0 || result === undefined) {
                    reject(err)
                    return
                }
                resolve(result);
            })
        } catch (error) {
            reject(error);
        }
    })
}

interface LivenessDetectionResult {
    isLive: boolean
    image: ArrayBuffer
}

function livenessDetection(livenessConfig: interactiveLiveness.InteractiveLivenessConfig = DEFAULT_LIVENESS_CONFIG): Promise<LivenessDetectionResult> {
    return new Promise(async (resolve, reject) => {
        try {

            const detectionResult = await _livenessDetection(livenessConfig);
            if (detectionResult?.mPixelMap) {
                const arrayBuffer = await pixelMapToArrayBuffer(detectionResult.mPixelMap)
                resolve({
                    isLive: true,
                    image: arrayBuffer
                } as LivenessDetectionResult)
            } else {
                resolve({
                    isLive: false
                } as LivenessDetectionResult)
            }
        } catch (error) {
            if (BUSINESS_ERROR_CODES.includes((error as BusinessError).code)) {
                resolve({
                    isLive: false
                } as LivenessDetectionResult)
            } else {
                reject(error);
            }
        }
    })
}

// 主要的部分
function validateOptions(options: DetectOptions): boolean {
    let actionsNum = options.actionsNum;
    let isSilentMode = options.isSilentMode;

    if (actionsNum && typeof actionsNum !== 'number') {
        return false;
    }

    if (isSilentMode && typeof isSilentMode !== 'string') {
        return false;
    }

    if (actionsNum && !ACTIONS_NUMS.includes(actionsNum)) {
        return false;
    }

    if (isSilentMode && !DETECTION_MODES.includes(isSilentMode)) {
        return false;
    }

    return true;
}

export function detect(options: DetectOptions): void {
	if (!validateOptions(options)) {
		const businessError = {code: 401, message: "Parameter error"} as BusinessError;
		options?.fail?.(businessError);
		options?.complete?.(businessError);
		return;
	}
    livenessDetection({
        actionsNum: options.actionsNum,
        isSilentMode: options.isSilentMode as interactiveLiveness.DetectionMode,
		routeMode: "back" as interactiveLiveness.RouteRedirectionMode
    } as interactiveLiveness.InteractiveLivenessConfig).then(result => {
        const detectResult:DetectResult = {
            alive: result.isLive,
            image: result.image
        }
        options?.success?.(detectResult);
        options?.complete?.(detectResult);
    }).catch((error: BusinessError) => {
        options?.fail?.(error);
        options?.complete?.(error);
    })
}
<template>
	<view>
		<view>ohos.permission.CAMERA 需要加这个权限</view>
		<button @tap="detectLiveness">活体检测</button>
		<image v-if="imageData" :src="imageData" mode="widthFix"></image>
	</view>
</template>

<script>
	import livenessUtil from '@/uni_modules/hw-liveness'
	export default {
		data() {
			return {
				imageData: ''
			}
		},
		methods: {
			detectLiveness() {
				livenessUtil.detect({
					actionsNum: 3,
					success: (res) => {
						console.log(JSON.stringify(res))
						uni.showModal({
							title: res.alive? '检测成功' : '检测失败'
						})
						this.imageData = "data:image/jpeg;base64," + uni.arrayBufferToBase64(res.image)
					},
					fail: (err) => {
						uni.showModal({
							title: err.message
						})
					}
				});
			}
		}
	}
</script>

<style>
</style>

更多关于HarmonyOS鸿蒙Next中uniapp怎么实现人脸识别功能的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


学习下

我知道鸿蒙提供了 Core Vision Kit(基础视觉服务)机器视觉相关的基础能力,例如通用文字识别(即OCR,Optical Character Recognition,也称为光学字符识别)、人脸检测、人脸比对以及主体分割等能力。

开发文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/core-vision-kit-guide

HarmonyOS Next中,UniApp实现人脸识别需使用系统提供的AI能力接口。通过@ohos.multimodalInput.facialAuth(人脸认证)Kit实现。主要步骤:1. 检查设备支持;2. 调用相关API进行人脸采集与认证。需在module.json5中声明ohos.permission.FACE_AUTH权限。

在HarmonyOS Next中,UniApp可以通过调用系统的人脸识别能力或集成第三方SDK来实现人脸识别功能。由于原生的UniApp API可能尚未完全适配HarmonyOS Next,建议采用以下方案:

  1. 使用HarmonyOS原生能力:通过UniApp的Native.js或原生插件机制,调用HarmonyOS的FaceDetector API(属于@ohos.multimodalInput.facialRecognition模块)。这需要编写HarmonyOS原生代码(ArkTS),并封装为UniApp可调用的模块。

  2. 集成第三方AI SDK:选择支持HarmonyOS的AI服务商(如华为HMS Core ML Kit的人脸识别服务),通过原生插件集成。HMS ML Kit提供了跨平台支持,可处理人脸检测、特征分析等功能。

  3. 注意事项

    • 权限配置:在HarmonyOS配置文件中声明ohos.permission.CAMERAohos.permission.FACE_RECOGNITION权限。
    • 兼容性:确保设备支持人脸识别硬件(如结构光或红外摄像头)。
    • 性能优化:离线识别可减少延迟,但需模型部署;在线识别依赖网络。

示例步骤(基于HarmonyOS原生调用):

  • 在DevEco Studio中创建HarmonyOS原生模块。
  • 实现人脸检测逻辑,使用FaceDetector类捕获并分析图像。
  • 将模块封装为UniApp插件,通过uni.requireNativePlugin调用。

若UniApp官方未来推出HarmonyOS Next适配版本,可直接使用相关API。目前需结合原生开发实现功能。

回到顶部