flutter开发实战-人脸识别相机使用

flutter开发实战-人脸识别相机使用

当需要拍摄的时候,需要检测到人脸再进行后续的操作,这里使用的是face_camera
在这里插入图片描述

一、引入face_camera

在工程的pubspec.yaml中引入插件

   # 检测人脸
  face_camera: ^0.0.8
    

iOS端需要设置相关权限
在info.plist文件中,设置相机等权限

<key>NSCameraUsageDescription</key>
	<string>Take a photo for display</string>
	<key>NSMicrophoneUsageDescription</key>
	<string>Take a video for display</string>
	<key>NSPhotoLibraryUsageDescription</key>
	<string>Read your photos for display</string>
	<key>UIApplicationSupportsIndirectInputEvents</key>
    

二、人脸识别相机使用

第一步是在main.dart中初始化face_camera

void main() async{
  WidgetsFlutterBinding.ensureInitialized(); //Add this

  await FaceCamera.initialize(); //Add this

  runApp(const MyApp());
}
    

然后在应用程序中渲染组件,设置onCapture回调。

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SmartFaceCamera(
          autoCapture: true,
          defaultCameraLens: CameraLens.front,
          message: 'Center your face in the square',
          onCapture: (File? image){
            
          },
        )
    );
  }
    

完整代码如下

import 'dart:io';

import 'package:flutter/material.dart';

import 'package:face_camera/face_camera.dart';

class FaceCameraPage extends StatefulWidget {
  const FaceCameraPage({Key? key}) : super(key: key);

  @override
  State<FaceCameraPage> createState() => _FaceCameraPageState();
}

class _FaceCameraPageState extends State<FaceCameraPage> {
  File? _capturedImage;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('FaceCamera example app'),
      ),
      body: Builder(builder: (context) {
        if (_capturedImage != null) {
          return Center(
            child: Stack(
              alignment: Alignment.bottomCenter,
              children: [
                Image.file(
                  _capturedImage!,
                  width: double.maxFinite,
                  fit: BoxFit.fitWidth,
                ),
                ElevatedButton(
                    onPressed: () => setState(() => _capturedImage = null),
                    child: const Text(
                      'Capture Again',
                      textAlign: TextAlign.center,
                      style:
                          TextStyle(fontSize: 14, fontWeight: FontWeight.w700),
                    ))
              ],
            ),
          );
        }
        return SmartFaceCamera(
            autoCapture: true,
            defaultCameraLens: CameraLens.front,
            onCapture: (File? image) {
              setState(() => _capturedImage = image);
            },
            onFaceDetected: (Face? face) {
              //Do something
            },
            messageBuilder: (context, face) {
              if (face == null) {
                return _message('Place your face in the camera');
              }
              if (!face.wellPositioned) {
                return _message('Center your face in the square');
              }
              return const SizedBox.shrink();
            });
      }),
    );
  }

  Widget _message(String msg) => Padding(
        padding: const EdgeInsets.symmetric(horizontal: 55, vertical: 15),
        child: Text(msg,
            textAlign: TextAlign.center,
            style: const TextStyle(
                fontSize: 14, height: 1.5, fontWeight: FontWeight.w400)),
      );
}

    

三、小结

flutter开发实战-人脸识别相机使用

学习记录,每天不停进步。

相关推荐

  1. 人脸识别开发项目汇总

    2024-05-12 08:24:04       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-12 08:24:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-12 08:24:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-12 08:24:04       82 阅读
  4. Python语言-面向对象

    2024-05-12 08:24:04       91 阅读

热门阅读

  1. 生活中的网络

    2024-05-12 08:24:04       28 阅读
  2. HarmonyOS应用开发者高级认证 试题+答案

    2024-05-12 08:24:04       29 阅读
  3. 1-k8s常见注意事项

    2024-05-12 08:24:04       26 阅读
  4. KubeKey 部署 K8s v1.28.8 实战

    2024-05-12 08:24:04       29 阅读
  5. 从零开始精通RTSP之传输AAC音频流

    2024-05-12 08:24:04       34 阅读
  6. 【GitHub】将本地VueCLI项目关联到GitHub远程仓库

    2024-05-12 08:24:04       30 阅读