Opening an already recorded file (ONI file) instead of a device

Prior to the Selecting a specific device for accessing depth stream recipe of this chapter we used ANY_DEVICE as the parameter of the openni::Device::open() method to open the first device and access its sensors. In the Selecting a specific device for accessing depth stream recipe of this chapter we learned that we can select what device to open by sending its hardware Uri to the openni::OpenNI::open() method. And in this recipe, we will learn another use of the openni::OpenNI::open() method.

You will find out how to record data from different sensors of a device to a file in the later sections of the chapter, but now we want to show how to re-open them as an individual device (just like when we open a physical device) and access saved data streams).

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.

You can find a sample file in the downloadable content of this book. Refer to the recipe's directory for the source code and ONI file.

How to do it...

Have a look at the following steps:

  1. Open your project and then the project's main source code file. Locate this line:
    int _tmain(int argc, _TCHAR* argv[])
    {
  2. 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 != '
    ') && (newChar != EOF));
      return (char)lastChar;
     }
    
    bool HandleStatus(Status status)
    {
      if (status == STATUS_OK)
        return true;
      printf("ERROR: #%d, %s", status,
        OpenNI::getExtendedError());
      ReadLastCharOfLine();
      return false;
    }
  3. Locate this line again:
    int _tmain(int argc, _TCHAR* argv[])
    {
  4. Write the following code snippet below the preceding line of code:
      Status status = STATUS_OK;
      printf("Scanning machine for devices and loading "
          "modules/drivers ...
    ");
      status = OpenNI::initialize();
      if (!HandleStatus(status)) return 1;
      printf("Completed.
    ");
    
      printf("Press ENTER to continue.
    ");
      ReadLastCharOfLine();
    
      char* addressOfFile =
          "C:\\MultipleHands_From_OpenNIorg.oni";
      printf("Opening ONI file from %s as device ...
    ",
            addressOfFile);
      Device device;
      status = device.open(addressOfFile);
      if (!HandleStatus(status)) return 1;
      printf("%s Opened, Completed.
    ",
        device.getDeviceInfo().getName());
    
      printf("Press ENTER to continue.
    ");
      ReadLastCharOfLine();
      
      printf("Checking if depth stream is supported ...
    ");
      if (false && !device.hasSensor(SENSOR_DEPTH))
      {
        printf("Depth stream not supported by this device. "
            "Press ENTER to exit.
    ");
        ReadLastCharOfLine();
        return 1;
      }
    
      printf("Asking device to create a depth stream ...
    ");
      VideoStream sensor;
      status = sensor.create(device, SENSOR_DEPTH);
      if (!HandleStatus(status)) return 1;
      printf("Completed.
    ");
    
      printf("Starting stream ...
    ");
      status = sensor.start();
      if (!HandleStatus(status)) return 1;
      printf("Completed.
    ");
    
      printf("Press ENTER to exit.
    ");
      ReadLastCharOfLine();
      sensor.destroy();
      device.close();
      OpenNI::shutdown();
      return 0;

How it works...

We defined our ReadLastCharOfLine() and HandleStatus() methods just like we did previously; read the previous recipes about that.

As you can see, most of the lines in the code are the same as the ones in the older recipes such as the Accessing video stream (depth/IR/RGB) and configuring them recipe or the Selecting a specific device for accessing depth stream recipe. There is only one minor change in the opening device:

  char* addressOfFile =
      "C:\\MultipleHands_From_OpenNIorg.oni";
  printf("Opening ONI file from %s as device ...
",
        addressOfFile);
  Device device;
  status = device.open(addressOfFile);

As it is brightly visible, we used the address of our file instead of the hardware Uri as the parameter of the openni::OpenNI::open() method.

How it works...

There's more...

Actually ONI files are like multichannel video files that can be used later to read color, IR, or depth data streams. Thinking from this perspective, we can guess that it is possible to change the speed of playing, even skip some frames, and so on; and actually it is possible and the way to do these customizations is via using the openni::PlaybackControl class. You can read more about this class and a sample code in Chapter 3, Using Low-level Data.

See also

  • The Recording streams to file (ONI file) recipe in Chapter 3, Using Low-level Data
  • The Controlling the player when opening a device from file recipe in Chapter 3, Using Low-level Data
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.150.142