你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

找到并打开 Azure Kinect 设备

本文介绍如何找到然后打开 Azure Kinect DK。 本文将解释如何处理有多个设备连接到计算机的情况。

你还可以参考 SDK 枚举示例,其中演示了如何使用本文所述的函数。

本文将介绍以下函数:

发现已连接的设备数

首先使用 k4a_device_get_installed_count() 获取当前已连接的 Azure Kinect 设备数。

uint32_t device_count = k4a_device_get_installed_count();

printf("Found %d connected devices:\n", device_count);

打开设备

若要获取设备的相关信息或从中读取数据,首先需要使用 k4a_device_open() 打开该设备的句柄。

k4a_device_t device = NULL;

for (uint8_t deviceIndex = 0; deviceIndex < device_count; deviceIndex++)
{
    if (K4A_RESULT_SUCCEEDED != k4a_device_open(deviceIndex, &device))
    {
        printf("%d: Failed to open device\n", deviceIndex);
        continue;
    }

    ...

    k4a_device_close(device);
}

k4a_device_open()index 参数指示当连接了多个设备时要打开哪个设备。 如果你预期只会连接一个设备,可以传递 K4A_DEVICE_DEFAULT 的参数或 0 来指示第一台设备。

用完句柄后,每当打开设备时,都需要调用 k4a_device_close()。 在关闭句柄之前,无法打开同一设备的其他句柄。

识别特定的设备

在附加或分离设备之前,设备按索引枚举的顺序不会更改。 若要识别物理设备,应使用设备的序列号。

若要读取设备中的序列号,请在打开句柄后使用 k4a_device_get_serialnum() 函数。

此示例演示如何分配适量内存来存储序列号。

char *serial_number = NULL;
size_t serial_number_length = 0;

if (K4A_BUFFER_RESULT_TOO_SMALL != k4a_device_get_serialnum(device, NULL, &serial_number_length))
{
    printf("%d: Failed to get serial number length\n", deviceIndex);
    k4a_device_close(device);
    device = NULL;
    continue;
}

serial_number = malloc(serial_number_length);
if (serial_number == NULL)
{
    printf("%d: Failed to allocate memory for serial number (%zu bytes)\n", deviceIndex, serial_number_length);
    k4a_device_close(device);
    device = NULL;
    continue;
}

if (K4A_BUFFER_RESULT_SUCCEEDED != k4a_device_get_serialnum(device, serial_number, &serial_number_length))
{
    printf("%d: Failed to get serial number\n", deviceIndex);
    free(serial_number);
    serial_number = NULL;
    k4a_device_close(device);
    device = NULL;
    continue;
}

printf("%d: Device \"%s\"\n", deviceIndex, serial_number);

打开默认设备

在大多数应用程序中,只会将单个 Azure Kinect DK 附加到同一台计算机。 如果只需连接到单个预期设备,可以结合 K4A_DEVICE_DEFAULTindex 调用 k4a_device_open() 打开第一台设备。

k4a_device_t device = NULL;
uint32_t device_count = k4a_device_get_installed_count();

if (device_count != 1)
{
    printf("Unexpected number of devices found (%d)\n", device_count);
    goto Exit;
}

if (K4A_RESULT_SUCCEEDED != k4a_device_open(K4A_DEVICE_DEFAULT, &device))
{
    printf("Failed to open device\n");
    goto Exit;
}

后续步骤