您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

找到并打开 Azure Kinect 设备Find then open the Azure Kinect device

本文介绍如何找到然后打开 Azure Kinect DK。This article describes how you can find, then open your Azure Kinect DK. 本文将解释如何处理有多个设备连接到计算机的情况。The article explains how to handle the case where there are multiple devices connected to your machine.

你还可以参考 SDK 枚举示例,其中演示了如何使用本文所述的函数。You can also refer to the SDK Enumerate Example that demonstrates how to use the functions in this article.

本文将介绍以下函数:The following functions are covered:

发现已连接的设备数Discover the number of connected devices

首先使用 k4a_device_get_installed_count() 获取当前已连接的 Azure Kinect 设备数。First get the count of currently connected Azure Kinect devices using k4a_device_get_installed_count().

uint32_t device_count = k4a_device_get_installed_count();

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

打开设备Open a device

若要获取设备的相关信息或从中读取数据,首先需要使用 k4a_device_open() 打开该设备的句柄。To get information about a device, or to read data from it, you need to first open a handle to the device using 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 参数指示当连接了多个设备时要打开哪个设备。The index parameter of k4a_device_open() indicates which device to open if there are more than one connected. 如果你预期只会连接一个设备,可以传递 K4A_DEVICE_DEFAULT 的参数或 0 来指示第一台设备。If you only expect a single device to be connected, you can pass an argument of K4A_DEVICE_DEFAULT or 0 to indicate the first device.

用完句柄后,每当打开设备时,都需要调用 k4a_device_close()Anytime you open a device you need to call k4a_device_close() when you're done using the handle. 在关闭句柄之前,无法打开同一设备的其他句柄。No other handles can be opened to the same device until you've closed the handle.

识别特定的设备Identify a specific device

在附加或分离设备之前,设备按索引枚举的顺序不会更改。The order devices enumerate by index won't change until devices are attached or detached. 若要识别物理设备,应使用设备的序列号。To identify a physical device, you should use the device's serial number.

若要读取设备中的序列号,请在打开句柄后使用 k4a_device_get_serialnum() 函数。To read the serial number from the device, use the k4a_device_get_serialnum() function after you've opened a handle.

此示例演示如何分配适量内存来存储序列号。This example demonstrates how to allocate the right amount of memory to store the serial number.

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);

打开默认设备Open the default device

在大多数应用程序中,只会将单个 Azure Kinect DK 附加到同一台计算机。In most applications, there will only be a single Azure Kinect DK attached to the same computer. 如果只需连接到单个预期设备,可以结合 K4A_DEVICE_DEFAULTindex 调用 k4a_device_open() 打开第一台设备。If you only need to connect to the single expected device, you can call k4a_device_open() with index of K4A_DEVICE_DEFAULT to open the first device.

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;
}

后续步骤Next steps