- OpenNI Cookbook
- Soroush Falahati
- 856字
- 2021-08-13 16:30:18
OpenNI class and error handling
We will show how to create and initialize a context object in C++ and how to use the openni::Status
datatype to handle errors thrown by OpenNI core. We will use openni::Status
very often in the later recipes and it is a very important part of any successful application.
In this recipe, we try to show the version number of the current OpenNI environment and then initialize the OpenNI framework. This will ask OpenNI to search for any connected device and load their modules and drivers.
Getting ready
Create a project in Visual Studio 2010 and prepare it for working with OpenNI using the Creating a project in Visual Studio 2010 recipe in this chapter.
How to do it...
Have a look at the following steps:
- Open your project and then the project's main source code file. Locate this line:
int _tmain(int argc, _TCHAR* argv[]) {
- Write the following code snippet above the preceding line of code:
char ReadLastCharOfLine() { int newChar = 0; int lastChar; fflush(stdout); do { lastChar = newChar; newChar = getchar(); } while ((newChar != '\n') && (newChar != EOF)); return (char)lastChar; }
- Locate this line again:
int _tmain(int argc, _TCHAR* argv[]) {
- Write the following code snippet below the preceding line of code:
printf("OpenNI Version is %d.%d.%d.%d", OpenNI::getVersion().major, OpenNI::getVersion().minor, OpenNI::getVersion().maintenance, OpenNI::getVersion().build); printf("Scanning machine for devices and loading " "modules/drivers ...\r\n"); Status status = STATUS_OK; status = OpenNI::initialize(); if (status != STATUS_OK){ printf("ERROR: #%d, %s", status, OpenNI::getExtendedError()); return 1; } printf("Completed.\r\n"); // Requesting device and creating sensor stream, then // reading data goes here printf("Press ENTER to exit.\r\n"); ReadLastCharOfLine(); OpenNI::shutdown(); return 0;
How it works...
In the first step we defined a new method named ReadLastCharOfLine()
. This function will wait until the user presses the Enter key and will return the last character typed by the user before Enter or 0
if nothing. We will use this function to wait for the user input or ask the user to make a decision. In this example, we used it for preventing our application from closing before the user command. We will not describe it line by line because this function is not a part of our topic but it is simple and easy to understand.
In the second step we used the openni::OpenNI::getVersion()
method from the openni::OpenNI
class to get the version of the used OpenNI framework. The return value of this function is of the type openni::Version
(the other name for OniVersion). Using different fields of this structure we can access different version number categories.
printf("OpenNI Version is %d.%d.%d.%d", OpenNI::getVersion().major, OpenNI::getVersion().minor, OpenNI::getVersion().maintenance, OpenNI::getVersion().build);
Then we used openni::OpenNI
to initialize OpenNI. The initializing process includes initializing and preloading different modules and drivers by OpenNI. We don't need to create an object from the openni::OpenNI
class because all methods are static.
status = OpenNI::initialize();
Also, we checked if the initializing process ended without error, by creating a variable of type openni::Status
.
Status status = STATUS_OK; ... if (status != STATUS_OK)
openni::Status
will inform us if any error exists but it can't give us more information about this error. But on other hand OpenNI gives us a method that will return the latest error message. This method is openni::OpenNI::getExtendedError()
.
printf("ERROR: #%d, %s", status, OpenNI::getExtendedError());
If everything is ok, we can proceed to other steps including requesting and reading data from the device or file. These parts will be discussed in later recipes. And at last when we are done with OpenNI it is better to ask it to turn all devices down and release all resources. For doing so we need to execute openni::OpenNI::shutdown()
.
OpenNI::shutdown();
In our code we used the printf()
function for printing messages and variable's values to console.

Defining a method for displaying error message
From now on we will create and use a function in our project to handle openni::Status
, returned from different methods of OpenNI. This will help us reduce the number of conditions in our code and improve it by making it more readable and understandable. The following is the function that we will use in the later recipes:
bool HandleStatus(Status status) { if (status == STATUS_OK) return true; printf("ERROR: #%d, %s", status, OpenNI::getExtendedError()); ReadLastCharOfLine(); return false; }
This function will return false
in case of an error and will print an error message to the console. There is nothing new to describe here.
Possible values of openni::Status
Our status variable in the previous code is of the type openni::Status
. This enum
has different possible values that we will try to explain as follows:
openni::Status::STATUS_OK
: This specifies that there is no problem or erroropenni::Status::STATUS_ERROR
: This specifies that an error has happened in the process of execution of the requested method or functionopenni::Status::STATUS_NOT_IMPLEMENTED
: This specifies that the method or the function you called or used is not implemented yetopenni::Status::STATUS_NOT_SUPPORTED
: This specifies that the requested task is not supported or not possibleopenni::Status::STATUS_BAD_PARAMETER
: This specifies that the parameter of the method or function is incorrect, null, or irrelevantopenni::Status::STATUS_OUT_OF_FLOW
: In other words, overflow usually means a problem with the memory/device or stackopenni::Status::STATUS_NO_DEVICE
: This specifies that no device is connected and available to use
- ClickHouse性能之巔:從架構設計解讀性能之謎
- iOS 9 Game Development Essentials
- AngularJS Web Application Development Blueprints
- PostgreSQL技術內幕:事務處理深度探索
- 營銷數據科學:用R和Python進行預測分析的建模技術
- Python 3網絡爬蟲實戰
- 軟件測試技術指南
- Mathematica Data Analysis
- C++新經典
- 響應式Web設計:HTML5和CSS3實戰(第2版)
- Qlik Sense? Cookbook
- Learning Bootstrap 4(Second Edition)
- 算法超簡單:趣味游戲帶你輕松入門與實踐
- MySQL數據庫教程(視頻指導版)
- 可視化H5頁面設計與制作:Mugeda標準教程