参考官网教程: https://docs.microsoft.com/zh-cn/azure/kinect-dk/build-first-app
使用环境:Azure Kinect SDK v1.4.1 + VS2019
文章目录
一、项目配置
1. 新建空白C++控制台工程并添加源文件Main.cpp
Main.cpp添加后可以先空着。
2. 安装 Azure Kinect NuGet 包
右键 引用 - 管理NuGet程序包
选择nuget.org 作为包源,选择“浏览”选项卡,然后搜索Microsoft.Azure.Kinect.Sensor,从列表中选择该包并安装。
安装完成后会多出图示文件
3. 添加头文件和库文件
3.1 加入头文件k4a.h
直接右键 头文件 - 添加 - 现有项,找到k4a.h导入。默认路径为C:\Program Files\Azure Kinect SDK v1.4.1\sdk\include\k4a
3.2 配置头文件目录和库文件目录
右键自己的项目,选择属性,上方的平台选择 x64
- C/C++ - 常规 - 附加包含目录,加入SDK的include路径
- 链接器 - 常规 - 附加库目录,加入SDK的lib路径
- 链接器 - 输入 - 附加依赖项,加入k4a.lib
注意:每添加一项后,点击右下角应用。 - 配置环境变量
如果发生报错“未找到k4a.dll”,那么需要将C:\Program Files\Azure Kinect SDK v1.4.1\sdk\windows-desktop\amd64\release\bin添加到环境变量中,重启VS或者重启电脑后生效。
二、完整源代码
把以下代码拷贝到Main.cpp中
#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 4096x3072 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_3072P;
// 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;
}
首行 #pragma comment(…) 指令将一个注释放入一个对象文件或可执行文件中,常用lib关键字帮我们链入一个库文件。如:#pragma comment(lib,“k4a.lib”) 该指令用来将k4a.lib库文件加入到本工程中。
三、结果展示
控制台输出自己的设备ID,表明运行成功。