C++ 彩色流示例代码-Color Stream Viewer

# 彩色示例-ColorViewer

 

功能描述:本示例主要演示了使用SDK获取彩色数据并绘制显示、获取分辨率并选择进行设置、显示彩色图像,并通过ESC_KEY键退出程序

> 本示例基于C++ High Level API进行演示

 

首先需要创建一个Pipeline,通过Pipeline可以很容易的打开和关闭多种类型的流并获取一组帧数据

ob::Pipeline pipe;

 

获取彩色相机的所有流配置,包括流的分辨率,帧率,以及帧的格式

// Create a pipeline with default device
ob::Pipeline pipe;

// Configure which streams to enable or disable for the Pipeline by creating a Config
std::shared_ptr<ob::Config> config = std::make_shared<ob::Config>();

std::shared_ptr<ob::VideoStreamProfile> colorProfile = nullptr;
try {
    // Get all stream profiles of the color camera, including stream resolution, frame rate, and frame format
    auto profiles = pipe.getStreamProfileList(OB_SENSOR_COLOR);
    try {
        // Find the corresponding Profile according to the specified format, and choose the RGB888 format first
        colorProfile = profiles->getVideoStreamProfile(640, 480, OB_FORMAT_YUYV, 30);
    }
    catch(ob::Error &e) {
        // If the specified format is not found, select the first one (default stream profile)
        colorProfile = std::const_pointer_cast<ob::StreamProfile>(profiles->getProfile(OB_PROFILE_DEFAULT))->as<ob::VideoStreamProfile>();
    }
    config->enableStream(colorProfile);
}
catch(ob::Error &e) {
    std::cerr << "Current device is not support color sensor!" << std::endl;
    exit(EXIT_FAILURE);
}

```

启动在Config中配置的流

pipe.start(config);

 

以阻塞的方式等待一帧数据,该帧是一个复合帧,里面包含配置里启用的所有流的帧数据,并设置帧的等待超时时间

auto frameSet = pipe.waitForFrames(100);    //设置等待时间为100ms

 

30帧打印一次metadata

// print metadata every 30 frames
auto index = colorFrame->index();
if(index % 30 == 0) {
    std::cout << "*************************** Color Frame #" << index << " Metadata List ********************************" << std::endl;
    for(int metaDataType = 0; metaDataType < OB_FRAME_METADATA_TYPE_COUNT; metaDataType++) {
        // Check if it is supported metaDataType for current frame
        if(colorFrame->hasMetadata((OBFrameMetadataType)metaDataType)) {
            // Get the value of the metadata
            std::cout << metaDataTypes[metaDataType] << ": " << colorFrame->getMetadataValue((OBFrameMetadataType)metaDataType) << std::endl;
        }
        else {
            std::cout << metaDataTypes[metaDataType] << ": " << "unsupported" << std::endl;
        }
    }
    std::cout << "********************************************************************************" << std::endl << std::endl;
}

 

停止Pipeline,将不再产生帧数据

pipe.stop();

最终的彩色图显示如下

image.png

程序正常退出之后资源将会自动释放