OSVR-Core  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Imaging.cpp
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 // Internal Includes
26 #include <osvr/ClientKit/Context.h>
28 #include <osvr/ClientKit/Imaging.h>
30 
31 // Library/third-party includes
32 #include <opencv2/highgui/highgui.hpp>
33 
34 // Standard includes
35 #include <iostream>
36 #include <string>
37 
40 static const std::string
41  windowNameAndInstructions("OSVR imaging demo | q or esc to quit");
42 
43 bool gotSomething = false;
44 
45 void imagingCallback(void *userdata,
46  osvr::util::time::TimeValue const &timestamp,
48  // Convert the image pointer into an OpenCV matrix.
49  cv::Mat frame(report.metadata.height, report.metadata.width,
51  report.buffer.get());
52 
53  if (frame.empty()) {
54  std::cout << "Error, frame empty!" << std::endl;
55  return;
56  }
57 
59  if (!gotSomething) {
60  gotSomething = true;
61  std::cout << "Got first report: image is " << frame.cols << "x"
62  << frame.rows << std::endl;
63  }
64 
65  cv::imshow(windowNameAndInstructions, frame);
66  osvr::clientkit::ImagingReport &lastReport =
67  *static_cast<osvr::clientkit::ImagingReport *>(userdata);
68  lastReport = report;
69 }
70 int main() {
71  osvr::clientkit::ClientContext context("com.osvr.exampleclients.Imaging");
72 
73  osvr::clientkit::Interface camera = context.getInterface("/camera");
74 
78 
79  // Register the imaging callback.
80  osvr::clientkit::registerImagingCallback(camera, &imagingCallback,
81  &lastReport);
82 
83  // Output instructions to the console.
84  std::cout << std::endl << windowNameAndInstructions << std::endl;
85 
86  // Pretend that this is your application's mainloop.
87  // We're using a simple OpenCV "highgui" loop here.
88  cv::namedWindow(windowNameAndInstructions);
89  while (1) {
90  context.update();
91  char key = static_cast<char>(cv::waitKey(1)); // wait 1 ms for a key
92  if ('q' == key || 'Q' == key || 27 /*esc*/ == key) {
93  break;
94  }
95  }
96 
97  std::cout << "Library shut down, exiting." << std::endl;
98  return 0;
99 }
int main()
Definition: Imaging.cpp:70
Interface handle object. Typically acquired from a ClientContext.
Client context object: Create and keep one in your application. Handles lifetime management and provi...
Definition: Context_decl.h:57
The user-friendly imaging report. Note that passing this around by value is OK (doesn't copy the imag...
Definition: Imaging_decl.h:52
ImageBufferPtr buffer
A shared pointer with custom deleter that owns the underlying image data buffer for the frame...
Definition: Imaging_decl.h:61
Header.
int computeOpenCVMatType(OSVR_ImagingMetadata const &metadata)
Computes the OpenCV matrix type (as in CV_8UC3) from a metadata struct.
Interface getInterface(const std::string &path)
Get the interface associated with the given path.
Definition: Context.h:61
OSVR_ImagingMetadata metadata
Metadata containing the properties of this frame.
Definition: Imaging_decl.h:57
void update()
Updates the state of the context - call regularly in your mainloop.
Definition: Context.h:54
Header.
void imagingCallback(void *userdata, osvr::util::time::TimeValue const &timestamp, osvr::clientkit::ImagingReport report)
Definition: Imaging.cpp:45
Header containing the inline implementation of Interface.
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81