libvisiontransfer  10.6.0
asynctransfer_example.cpp
1 /*******************************************************************************
2  * Copyright (c) 2023 Allied Vision Technologies 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 Nerian stereo devices
32  DeviceEnumeration deviceEnum;
33  DeviceEnumeration::DeviceList devices =
34  deviceEnum.discoverDevices();
35  if(devices.size() == 0) {
36  std::cout << "No devices discovered!" << std::endl;
37  return -1;
38  }
39 
40  // Print devices
41  std::cout << "Discovered devices:" << std::endl;
42  for(unsigned int i = 0; i< devices.size(); i++) {
43  std::cout << devices[i].toString() << std::endl;
44  }
45  std::cout << std::endl;
46 
47  // Create an image transfer object that receives data from
48  // the first detected device
49  AsyncTransfer asyncTransfer(devices[0]);
50 
51  // Receive 100 images
52  for(int imgNum=0; imgNum<100; imgNum++) {
53  std::cout << "Receiving image set " << imgNum << std::endl;
54 
55  // Receive image
56  ImageSet imageSet;
57  while(!asyncTransfer.collectReceivedImageSet(imageSet,
58  0.1 /*timeout*/)) {
59  // Keep on trying until reception is successful
60  }
61 
62  // Write all included images one after another
63  for(int i = 0; i < imageSet.getNumberOfImages(); i++) {
64  // Create PGM file
65  char fileName[100];
66  snprintf(fileName, sizeof(fileName), "image%03d_%d.pgm", imgNum, i);
67  imageSet.writePgmFile(i, fileName);
68  }
69  }
70  } catch(const std::exception& ex) {
71  std::cerr << "Exception occurred: " << ex.what() << std::endl;
72  }
73 
74  return 0;
75 }
visiontransfer::ImageSet::getNumberOfImages
int getNumberOfImages() const
Returns the number of images in this set.
Definition: imageset.h:431
visiontransfer::DeviceEnumeration::discoverDevices
DeviceList discoverDevices()
Discovers new devices and returns the list of all devices that have been found.
Definition: deviceenumeration.h:66
visiontransfer::DeviceEnumeration
Allows for the discovery of devices in the network.
Definition: deviceenumeration.h:42
visiontransfer::ImageSet
A set of one to three images, but usually two (the left camera image and the disparity map)....
Definition: imageset.h:50
visiontransfer::ImageSet::writePgmFile
void writePgmFile(int imageNumber, const char *fileName) const
Writes one image of the set to a PGM or PPM file.
Definition: imageset.cpp:106
visiontransfer::AsyncTransfer
Class for asynchronous transfer of image sets.
Definition: asynctransfer.h:45
Allied Vision