· 6 years ago · Mar 07, 2020, 10:32 PM
1#include <opencv2/core.hpp>
2#include <opencv2/highgui.hpp>
3#include <iostream>
4#include <stdio.h>
5
6// для версии 2.4.13.6
7
8int main(int, char**)
9{
10 bool stat;
11
12 cv::Mat frame;
13 cv::Mat frame2;
14 std::vector<uchar> buff;
15
16 //--- INITIALIZE VIDEOCAPTURE
17 cv::VideoCapture cap;
18 // open the default camera using default API
19 // cap.open(0);
20 // OR advance usage: select any API backend
21 int deviceID = 0; // 0 = open default camera
22 int apiID = 0; // 0 = autodetect default API
23 // open selected camera using selected API
24 cap.open(deviceID + apiID);
25 // check if we succeeded
26 if (!cap.isOpened()) {
27 std::cerr << "ERROR! Unable to open camera\n";
28 return -1;
29 }
30 //--- GRAB AND WRITE LOOP
31 std::cout << "Start grabbing" << std::endl
32 << "Press any key to terminate" << std::endl;
33 for (;;)
34 {
35 // wait for a new frame from camera and store it into 'frame'
36 cap.read(frame);
37 // check if we succeeded
38 if (frame.empty()) {
39 std::cerr << "ERROR! blank frame grabbed\n";
40 break;
41 }
42
43 stat = cv::imencode(".jpeg", frame, buff);
44 // TODO: check stat
45
46 frame2 = cv::imdecode(buff, CV_LOAD_IMAGE_COLOR);
47
48 // show live and wait for a key with timeout long enough to show images
49 cv::imshow("Live", frame2);
50 if (cv::waitKey(5) >= 0)
51 break;
52 }
53 // the camera will be deinitialized automatically in VideoCapture destructor
54 return 0;
55}