rs2_error* e =0;voidcheck_error(rs2_error* e){if(e){printf("rs_error was raised when calling %s(%s):\n",rs2_get_failed_function(e),rs2_get_failed_args(e));printf(" %s\n",rs2_get_error_message(e));//如果发生了错误,这个语句会导致程序退出,并返回一个失败的状态码EXIT_FAILURE,通常用于指示程序执行失败。exit(EXIT_FAILURE);}}
// License: Apache 2.0. See LICENSE file in root directory.// Copyright(c) 2017 Intel Corporation. All Rights Reserved./* Include the librealsense C header files */#include<librealsense2/rs.h>#include<librealsense2/h/rs_pipeline.h>#include<librealsense2/h/rs_frame.h>#include<stdlib.h>#include<stdint.h>#include<stdio.h>#include"example.h"// These parameters are reconfigurable //#defineSTREAMRS2_STREAM_COLOR // rs2_stream is a types of data provided by RealSense device //#defineFORMATRS2_FORMAT_RGB8 // rs2_format identifies how binary data is encoded within a frame //#defineWIDTH640// Defines the number of columns for each frame //#defineHEIGHT480// Defines the number of lines for each frame //#defineFPS30// Defines the rate of frames per second //#defineSTREAM_INDEX0// Defines the stream index, used for multiple streams of the same type //intmain(){
rs2_error* e =0;// Create a context object. This object owns the handles to all connected realsense devices.// The returned object should be released with rs2_delete_context(...)
rs2_context* ctx =rs2_create_context(RS2_API_VERSION,&e);check_error(e);/* Get a list of all the connected devices. */// The returned object should be released with rs2_delete_device_list(...)
rs2_device_list* device_list =rs2_query_devices(ctx,&e);check_error(e);int dev_count =rs2_get_device_count(device_list,&e);check_error(e);printf("There are %d connected RealSense devices.\n", dev_count);if(0== dev_count)return EXIT_FAILURE;// Get the first connected device// The returned object should be released with rs2_delete_device(...)
rs2_device* dev =rs2_create_device(device_list,0,&e);check_error(e);print_device_info(dev);// Create a pipeline to configure, start and stop camera streaming// The returned object should be released with rs2_delete_pipeline(...)
rs2_pipeline* pipeline =rs2_create_pipeline(ctx,&e);check_error(e);// Create a config instance, used to specify hardware configuration// The retunred object should be released with rs2_delete_config(...)
rs2_config* config =rs2_create_config(&e);check_error(e);// Request a specific configurationrs2_config_enable_stream(config, STREAM, STREAM_INDEX, WIDTH, HEIGHT, FORMAT, FPS,&e);check_error(e);// Start the pipeline streaming// The retunred object should be released with rs2_delete_pipeline_profile(...)
rs2_pipeline_profile* pipeline_profile =rs2_pipeline_start_with_config(pipeline, config,&e);if(e){printf("The connected device doesn't support color streaming!\n");exit(EXIT_FAILURE);}while(1){// This call waits until a new composite_frame is available// composite_frame holds a set of frames. It is used to prevent frame drops// The returned object should be released with rs2_release_frame(...)
rs2_frame* frames =rs2_pipeline_wait_for_frames(pipeline, RS2_DEFAULT_TIMEOUT,&e);check_error(e);// Returns the number of frames embedded within the composite frameint num_of_frames =rs2_embedded_frames_count(frames,&e);check_error(e);int i;for(i =0; i < num_of_frames;++i){// The retunred object should be released with rs2_release_frame(...)
rs2_frame* frame =rs2_extract_frame(frames, i,&e);check_error(e);constuint8_t* rgb_frame_data =(constuint8_t*)(rs2_get_frame_data(frame,&e));check_error(e);unsignedlonglong frame_number =rs2_get_frame_number(frame,&e);check_error(e);rs2_time_t frame_timestamp =rs2_get_frame_timestamp(frame,&e);check_error(e);// Specifies the clock in relation to which the frame timestamp was measured
rs2_timestamp_domain frame_timestamp_domain =rs2_get_frame_timestamp_domain(frame,&e);check_error(e);constchar* frame_timestamp_domain_str =rs2_timestamp_domain_to_string(frame_timestamp_domain);
rs2_metadata_type frame_metadata_time_of_arrival =rs2_get_frame_metadata(frame, RS2_FRAME_METADATA_TIME_OF_ARRIVAL,&e);check_error(e);printf("RGB frame arrived.\n");printf("First 10 bytes: ");int i;for(i=0; i <10;++i)printf("%02x ", rgb_frame_data[i]);printf("\nFrame No: %llu\n", frame_number);printf("Timestamp: %f\n", frame_timestamp);printf("Timestamp domain: %s\n", frame_timestamp_domain_str);printf("Time of arrival: %lld\n\n", frame_metadata_time_of_arrival);rs2_release_frame(frame);}rs2_release_frame(frames);}// Stop the pipeline streamingrs2_pipeline_stop(pipeline,&e);check_error(e);// Release resourcesrs2_delete_pipeline_profile(pipeline_profile);rs2_delete_config(config);rs2_delete_pipeline(pipeline);rs2_delete_device(dev);rs2_delete_device_list(device_list);rs2_delete_context(ctx);return EXIT_SUCCESS;}
example.h
// License: Apache 2.0. See LICENSE file in root directory.// Copyright(c) 2017 Intel Corporation. All Rights Reserved.#include<librealsense2/rs.h>#include<stdio.h>#include<stdlib.h>/* Function calls to librealsense may raise errors of type rs_error*/voidcheck_error(rs2_error* e){if(e){printf("rs_error was raised when calling %s(%s):\n",rs2_get_failed_function(e),rs2_get_failed_args(e));printf(" %s\n",rs2_get_error_message(e));exit(EXIT_FAILURE);}}voidprint_device_info(rs2_device* dev){
rs2_error* e =0;printf("\nUsing device 0, an %s\n",rs2_get_device_info(dev, RS2_CAMERA_INFO_NAME,&e));check_error(e);printf(" Serial number: %s\n",rs2_get_device_info(dev, RS2_CAMERA_INFO_SERIAL_NUMBER,&e));check_error(e);printf(" Firmware version: %s\n\n",rs2_get_device_info(dev, RS2_CAMERA_INFO_FIRMWARE_VERSION,&e));check_error(e);}