libvisiontransfer  6.4.0
server_example.cpp
1 /*******************************************************************************
2  * Copyright (c) 2019 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/imagepair.h>
18 #include <iostream>
19 #include <exception>
20 #include <vector>
21 #include <cstdio>
22 #include <cstring>
23 
24 #ifdef _WIN32
25 # include <windows.h>
26 #else
27 # include <unistd.h>
28 #endif
29 
30 #ifdef _MSC_VER
31 // Visual studio does not come with snprintf
32 #define snprintf _snprintf_s
33 #endif
34 
35 using namespace std;
36 using namespace visiontransfer;
37 
38 int main() {
39  try {
40  // Create an image transfer object that will serve as server
41  AsyncTransfer asyncTransfer("0.0.0.0", "7681", ImageProtocol::PROTOCOL_UDP, true);
42 
43  // Initialize the image pair meta data
44  ImagePair imagePair;
45  imagePair.setWidth(640);
46  imagePair.setHeight(480);
47  imagePair.setPixelFormat(0, ImagePair::FORMAT_8_BIT_MONO);
48  imagePair.setPixelFormat(1, ImagePair::FORMAT_8_BIT_MONO);
49  imagePair.setRowStride(0, imagePair.getBytesPerPixel(0)*imagePair.getWidth());
50  imagePair.setRowStride(1, imagePair.getBytesPerPixel(0)*imagePair.getWidth());
51  std::vector<unsigned char> pixelData(imagePair.getRowStride(0) * imagePair.getHeight());
52  imagePair.setPixelData(0, &pixelData[0]);
53  imagePair.setPixelData(1, &pixelData[0]);
54 
55  int transfer = 0;
56  while(true) {
57 #ifdef _WIN32
58  Sleep(500);
59 #else
60  usleep(50000);
61 #endif
62  if(!asyncTransfer.isConnected()) {
63  // Continue waiting until a client connects
64  continue;
65  }
66 
67  if(transfer == 0) {
68  cout << "Client IP: " << asyncTransfer.getRemoteAddress() << endl;
69  }
70 
71  // Generate test image data
72  for(int y=0; y<imagePair.getHeight(); y++) {
73  for(int x=0; x<imagePair.getWidth(); x++) {
74  pixelData[y*imagePair.getRowStride(0) + x] = (y + x + transfer) & 0xff;
75  }
76  }
77  transfer++;
78 
79  // Send the image data
80  asyncTransfer.sendImagePairAsync(imagePair);
81  }
82  } catch(const std::exception& ex) {
83  std::cerr << "Exception occurred: " << ex.what() << std::endl;
84  }
85 
86  return 0;
87 }
int getHeight() const
Returns the height of each image.
Definition: imagepair.h:180
void setPixelFormat(int imageNumber, ImageFormat format)
Sets the pixel format for the given image.
Definition: imagepair.h:100
void setPixelData(int imageNumber, unsigned char *pixelData)
Sets the pixel data for the given image.
Definition: imagepair.h:118
void setRowStride(int imageNumber, int stride)
Sets a new row stride for the pixel data of one image.
Definition: imagepair.h:88
int getRowStride(int imageNumber) const
Returns the row stride for the pixel data of one image.
Definition: imagepair.h:188
void setWidth(int w)
Sets a new width for both images.
Definition: imagepair.h:74
int getWidth() const
Returns the width of each image.
Definition: imagepair.h:175
Class for asynchronous transfer of image pairs.
Definition: asynctransfer.h:33
void setHeight(int h)
Sets a new width for both images.
Definition: imagepair.h:79
A set of two images, which are usually the left camera image and the disparity map.
Definition: imagepair.h:33
int getBytesPerPixel(int imageNumber) const
Returns the number of bytes that are required to store one image pixel.
Definition: imagepair.h:279
Nerian Vision Technologies