生成第一个Femto Mega应用程序

本文内容

● 先决条件

● 头文件

● 查找 Femto Mega 设备

● 启动相机

● 错误处理

● 完整源代码

● 后续步骤


想要开始使用Femto Mega?本快速入门指南将帮助你启动并成功运行该设备!

本文将介绍以下函数:

● k4a_device_get_installed_count()

● k4a_device_open()

● k4a_device_get_serialnum()

● k4a_device_start_cameras()

● k4a_device_stop_cameras()

● k4a_device_close()



先决条件

1. 设置Femto Mega

2. 并安装 SDK了解Orbbec SDK 和 Orbbec SDK K4A Wrapper



头文件

只需要一个头文件,即 k4a.h。 请确保所选的编译器设置为使用 SDK 的库并包含文件夹。 此外,需要链接 k4a.lib 和 k4a.dll 文件。

#include <k4a/k4a.h>



查找 Femto Mega 设备

可将多个 Femto Mega 设备连接到计算机。 首先,我们将使用 k4a_device_get_installed_count() 函数确定有多少个设备,或者是否连接了任何设备。 此函数应可立即运行,而无需经过附加的设置。

uint32_t count = k4a_device_get_installed_count();


确定某个设备已连接到计算机后,可以使用 k4a_device_open() 将其打开。 可以提供想要打开的设备的索引,或者只对第一个设备使用 K4A_DEVICE_DEFAULT

// Open the first plugged in Kinect device
k4a_device_t device = NULL;
k4a_device_open(K4A_DEVICE_DEFAULT, &device);


与 Azure Kinect 库中的大多数内容一样,当你打开某种内容时,也应该在用完时将其关闭! 关闭时,请记得调用 k4a_device_close()

k4a_device_close(device);


打开设备后,可以进行测试以确保它正常工作。 让我们读取设备的序列号!

// Get the size of the serial number
size_t serial_size = 0;
k4a_device_get_serialnum(device, NULL, &serial_size);
// Allocate memory for the serial, then acquire it
char *serial = (char*)(malloc(serial_size));
k4a_device_get_serialnum(device, serial, &serial_size);
printf("Opened device: %s\n", serial);
free(serial);



启动相机

打开设备后,需要使用 k4a_device_configuration_t 对象配置相机。 相机配置包含大量不同的选项。 请选择最适合自己方案的设置。

// Configure a stream of 3840x2160 BRGA color data at 15 frames per second
k4a_device_configuration_t config = K4A_DEVICE_CONFIG_INIT_DISABLE_ALL;
config.camera_fps       = K4A_FRAMES_PER_SECOND_15;
config.color_format     = K4A_IMAGE_FORMAT_COLOR_BGRA32;
config.color_resolution = K4A_COLOR_RESOLUTION_2160P;
// Start the camera with the given configuration
k4a_device_start_cameras(device, &config);
// ...Camera capture and application specific code would go here...
// Shut down the camera when finished with application logic
k4a_device_stop_cameras(device);



错误处理

为简洁起见,我们不会在某些例子中显示错误处理。 但是,错误处理始终很重要! 许多函数返回常规的成功/失败类型 k4a_result_t,或者包含详细信息的更具体的变量,比如 k4a_wait_result_t。 请查看每个函数的文档或 IntelliSense,以了解该函数预期显示的错误消息!


可以使用 K4A_SUCCEEDED  K4A_FAILED 宏检查函数的结果。 因此,除了打开 Femto Mega 设备以外,我们还可以按如下所示保护函数调用:

// Open the first plugged in Kinect device
k4a_device_t device = NULL;
if ( K4A_FAILED( k4a_device_open(K4A_DEVICE_DEFAULT, &device) ) )
{
   printf("Failed to open k4a device!\n");
   return;
}



完整源代码

#pragma comment(lib, "k4a.lib")
#include <k4a/k4a.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
   uint32_t count = k4a_device_get_installed_count();
   if (count == 0)
   {
       printf("No k4a devices attached!\n");
       return 1;
   }
   // Open the first plugged in Kinect device
   k4a_device_t device = NULL;
   if (K4A_FAILED(k4a_device_open(K4A_DEVICE_DEFAULT, &device)))
   {
       printf("Failed to open k4a device!\n");
       return 1;
   }
   // Get the size of the serial number
   size_t serial_size = 0;
   k4a_device_get_serialnum(device, NULL, &serial_size);
   // Allocate memory for the serial, then acquire it
   char *serial = (char*)(malloc(serial_size));
   k4a_device_get_serialnum(device, serial, &serial_size);
   printf("Opened device: %s\n", serial);
   free(serial);
   // Configure a stream of 3840x2160 BRGA color data at 15 frames per second
   k4a_device_configuration_t config = K4A_DEVICE_CONFIG_INIT_DISABLE_ALL;
   config.camera_fps       = K4A_FRAMES_PER_SECOND_15;
   config.color_format     = K4A_IMAGE_FORMAT_COLOR_BGRA32;
   config.color_resolution = K4A_COLOR_RESOLUTION_2160P;
   // Start the camera with the given configuration
   if (K4A_FAILED(k4a_device_start_cameras(device, &config)))
   {
       printf("Failed to start cameras!\n");
       k4a_device_close(device);
       return 1;
   }
   // Camera capture and application specific code would go here
   // Shut down the camera when finished with application logic
   k4a_device_stop_cameras(device);
   k4a_device_close(device);
   return 0;
}



后续步骤

了解如何使用传感器 SDK 查找并打开 Femto Mega 设备查找并打开设备