Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <opencv2/highgui/highgui.hpp>
- #include <opencv2/opencv.hpp>
- #include <iostream>
- #include <conio.h>
- #include <Windows.h>
- #include <devguid.h>
- #include <setupapi.h>
- #include <ctime>
- #include <cstring>
- #pragma comment(lib, "setupapi.lib")
- using namespace std;
- using namespace cv;
- void printInfo();
- char* getInfo(int command);
- VideoWriter createVideoWriter();
- char* getCurrentTime();
- void printKeyInfo();
- bool isConsole = true;
- bool isWindowOpen = true;
- bool isPhoto = false;
- bool isRecording = false;
- bool isEnd = false;
- VideoWriter writer;
- HHOOK keyBoardHook;
- LRESULT CALLBACK LogKey(int nCode, WPARAM wParam, LPARAM lParam) {
- if (wParam == WM_KEYDOWN) {
- KBDLLHOOKSTRUCT* keyStruct = (KBDLLHOOKSTRUCT*)lParam;
- switch (keyStruct->vkCode)
- {
- case 73:
- if (isConsole == true) {
- system("CLS");
- printInfo();
- }
- break;
- case 83:
- if (isConsole == true) {
- system("CLS");
- printKeyInfo();
- }
- break;
- case 67:
- if (isConsole == true) {
- ShowWindow(GetConsoleWindow(), SW_HIDE);
- isConsole = false;
- }
- else {
- isConsole = true;
- ShowWindow(GetConsoleWindow(), SW_SHOW);
- }
- break;
- case 72: // H
- isWindowOpen = (isWindowOpen == true) ? false : true;
- if (isWindowOpen == true) {
- namedWindow("Web-camera", WINDOW_NORMAL);
- }
- if (isWindowOpen == false) {
- destroyWindow("Web-camera");
- }
- break;
- case 80: // P
- isPhoto = true;
- break;
- case 86: // V
- isRecording = (isRecording == true) ? false : true;
- if (writer.isOpened()) {
- writer.release();
- }
- else writer = createVideoWriter();
- break;
- case 27:
- isEnd = true;
- }
- return CallNextHookEx(keyBoardHook, nCode, wParam, lParam);
- }
- }
- int main() {
- keyBoardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LogKey, NULL, 0);
- MSG msg;
- Mat frame;
- VideoCapture cap(0);
- system("CLS");
- if (!cap.isOpened()) {
- cout << "Frame was not captured " << endl;
- return 0;
- }
- namedWindow("Web-camera", WINDOW_NORMAL);
- while (true) {
- while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- if (msg.message == WM_QUIT) {
- cout << "Received WM_QUIT message, exiting loop" << endl;
- break;
- }
- }
- if (isEnd == true) {
- break;
- }
- cap.read(frame);
- if (frame.empty())
- exit(-3);
- char t[80];
- strcpy_s(t, 80, getCurrentTime());
- putText(frame, t, Point(260, 465), FONT_HERSHEY_SIMPLEX, 1, Scalar(128, 128, 128), 1);
- if (isPhoto == true) {
- static int photo_counter = 0;
- string name;
- name = "Photo\\photo" + to_string(photo_counter++) + ".jpg";
- Mat new_frame = frame;
- imwrite(name, new_frame);
- isPhoto = false;
- }
- if (isRecording == true) {
- putText(frame, "Recording", Point(10, 30), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 255), 2);
- writer.write(frame);
- }
- if(isWindowOpen == true)
- imshow("Web-camera", frame);
- }
- UnhookWindowsHookEx(keyBoardHook);
- destroyAllWindows();
- return 0;
- }
- void printInfo() {
- setlocale(LC_ALL, "Russian");
- cout << "INFORMATION ABOUT WEBCAM" << endl;
- const int commandNumber = 7;
- int command[commandNumber] = { SPDRP_FRIENDLYNAME,
- SPDRP_MFG,
- SPDRP_DRIVER,
- SPDRP_LOCATION_INFORMATION,
- SPDRP_PHYSICAL_DEVICE_OBJECT_NAME,
- SPDRP_CAPABILITIES,
- SPDRP_HARDWAREID };
- for (int i = 0; i < commandNumber; i++) {
- char data[256];
- strcpy_s(data, 256, getInfo(command[i]));
- switch (i) {
- case 0:
- cout << " 1) Name: ";
- break;
- case 1:
- cout << " 2) Manufacter: ";
- break;
- case 2:
- cout << " 3) Driver: ";
- break;
- case 3:
- cout << " 4) Local information: ";
- break;
- case 4:
- cout << " 5) Object name:";
- break;
- case 5:
- cout << " 6) Capabilities: ";
- break;
- case 6:
- cout << " 7) Id: ";
- break;
- }
- cout << data << endl;
- }
- }
- char* getInfo(int command) {
- HDEVINFO hDeviceInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_CAMERA, NULL, NULL, DIGCF_PRESENT);
- if (hDeviceInfo == INVALID_HANDLE_VALUE)
- return NULL;
- SP_DEVINFO_DATA spDeviceInfoData = { 0 };
- ZeroMemory(&spDeviceInfoData, sizeof(SP_DEVINFO_DATA));
- spDeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
- SetupDiEnumDeviceInfo(hDeviceInfo, 0, &spDeviceInfoData);
- WCHAR data[256];
- SetupDiGetDeviceRegistryProperty(hDeviceInfo, &spDeviceInfoData, command, NULL, (PBYTE)data, sizeof(data), NULL);
- char ansiData[256];
- WideCharToMultiByte(CP_ACP, 0, data, -1, ansiData, sizeof(ansiData), NULL, NULL);
- return ansiData;
- }
- char* getCurrentTime() {
- time_t t = time(0);
- struct tm now;
- if (localtime_s(&now, &t) != 0) {
- exit(-2);
- }
- char datetime[80];
- strftime(datetime, 80, "%Y-%m-%d %H:%M:%S", &now);
- return datetime;
- }
- VideoWriter createVideoWriter() {
- static int counter = 0;
- int frameWidth = 640;
- int frameHeight = 480;
- int framePesSeconds = 13;
- string name = "Video\\video" + to_string(counter++) + ".mp4";
- VideoWriter writer(name, VideoWriter::fourcc('H', 'E', 'V', 'C'), framePesSeconds, Size(frameWidth, frameHeight));
- if (!writer.isOpened())
- exit(-1);
- return writer;
- }
- void printKeyInfo() {
- cout << "INFORMATION ABOUT KEYS" << endl;
- cout << " 1) Key \"I\" - information about the webcam" << endl <<
- " 2) Key \"C\" - console" << endl <<
- " 3) Key \"H\" - information from the webcam" << endl <<
- " 4) Key \"P\" - photo" << endl <<
- " 5) Key \"V\" - video" << endl <<
- " 6) Key \"S\" - keys" << endl <<
- " 7) Key \"ESC\" - terminate the program" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement