libvisiontransfer  8.1.0
asynctransfer_example.cpp
1 /*******************************************************************************
2  * Copyright (c) 2020 Nerian Vision GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #include <visiontransfer/deviceenumeration.h>
16 #include <visiontransfer/asynctransfer.h>
17 #include <visiontransfer/imageset.h>
18 #include <iostream>
19 #include <exception>
20 #include <stdio.h>
21 
22 #ifdef _MSC_VER
23 // Visual studio does not come with snprintf
24 #define snprintf _snprintf_s
25 #endif
26 
27 using namespace visiontransfer;
28 
29 int main() {
30  //try {
31  // Search for SceneScan devices
32  DeviceEnumeration deviceEnum;
33  DeviceEnumeration::DeviceList devices = deviceEnum.discoverDevices();
34  if(devices.size() == 0) {
35  std::cout << "No devices discovered!" << std::endl;
36  return -1;
37  }
38 
39  // Print devices
40  std::cout << "Discovered devices:" << std::endl;
41  for(unsigned int i = 0; i< devices.size(); i++) {
42  std::cout << devices[i].toString() << std::endl;
43  }
44  std::cout << std::endl;
45 
46  // Create an image transfer object that receives data from
47  // the first detected SceneScan
48  AsyncTransfer asyncTransfer(devices[0]);
49 
50  // Receive 100 images
51  for(int i=0; i<100; i++) {
52  std::cout << "Receiving image set " << i << std::endl;
53 
54  // Receive image
55  ImageSet imageSet;
56  while(!asyncTransfer.collectReceivedImageSet(imageSet,
57  0.1 /*timeout*/)) {
58  // Keep on trying until reception is successful
59  }
60 
61  // Write all included images one after another
62  for(int imageNumber = 0; imageNumber <= imageSet.getNumberOfImages(); imageNumber++) {
63  // Create PGM file
64  char fileName[100];
65  snprintf(fileName, sizeof(fileName), "image%03d_%d.pgm", i,
66  imageNumber);
67 
68  imageSet.writePgmFile(imageNumber, fileName);
69  }
70  }
71  //} catch(const std::exception& ex) {
72  // std::cerr << "Exception occurred: " << ex.what() << std::endl;
73  //}
74 
75  return 0;
76 }
DeviceList discoverDevices()
Discovers new devices and returns the list of all devices that have been found.
void writePgmFile(int imageNumber, const char *fileName) const
Writes one image of the set to a PGM or PPM file.
Definition: imageset.cpp:101
int getNumberOfImages() const
Returns the number of images in this set.
Definition: imageset.h:400
A set of one to three images, but usually two (the left camera image and the disparity map)...
Definition: imageset.h:38
Class for asynchronous transfer of image sets.
Definition: asynctransfer.h:33
Allows for the discovery of devices in the network.
Nerian Vision Technologies