-
Notifications
You must be signed in to change notification settings - Fork 0
/
usbcamera.cpp
158 lines (131 loc) · 3.32 KB
/
usbcamera.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <windows.h>
#include "CameraApi.h"
#include "usbcamera.h"
#define VI_MAX_CAMERAS 20
DEFINE_GUID(CLSID_SystemDeviceEnum, 0x62be5d10, 0x60eb, 0x11d0, 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86);
DEFINE_GUID(CLSID_VideoInputDeviceCategory, 0x860bb310, 0x5d01, 0x11d0, 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86);
DEFINE_GUID(IID_ICreateDevEnum, 0x29840822, 0x5b84, 0x11d0, 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86);
//////列出USB摄像头设备列表
int ListDevices(vector<string>& list)
{
ICreateDevEnum *pDevEnum = NULL;
IEnumMoniker *pEnum = NULL;
int deviceCounter = 0;
CoInitialize(NULL);
HRESULT hr = CoCreateInstance(
CLSID_SystemDeviceEnum,
NULL,
CLSCTX_INPROC_SERVER,
IID_ICreateDevEnum,
reinterpret_cast<void**>(&pDevEnum)
);
if (SUCCEEDED(hr))
{
hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnum, 0);
if (hr == S_OK) {
IMoniker *pMoniker = NULL;
while (pEnum->Next(1, &pMoniker, NULL) == S_OK)
{
IPropertyBag *pPropBag;
hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag,
(void**)(&pPropBag));
if (FAILED(hr)) {
pMoniker->Release();
continue; // Skip this one, maybe the next one will work.
}
VARIANT varName;
VariantInit(&varName);
hr = pPropBag->Read(L"Description", &varName, 0);
if (FAILED(hr))
{
hr = pPropBag->Read(L"FriendlyName", &varName, 0);
}
if (SUCCEEDED(hr))
{
hr = pPropBag->Read(L"FriendlyName", &varName, 0);
int count = 0;
char tmp[255] = { 0 };
while (varName.bstrVal[count] != 0x00 && count < 255)
{
tmp[count] = (char)varName.bstrVal[count];
count++;
}
list.push_back(tmp);
}
pPropBag->Release();
pPropBag = NULL;
pMoniker->Release();
pMoniker = NULL;
deviceCounter++;
}
pDevEnum->Release();
pDevEnum = NULL;
pEnum->Release();
pEnum = NULL;
}
}
return deviceCounter;
}
CaptureThread::CaptureThread(QObject *parent, int device, std::string str) :
QThread(parent)
{
pause_status = true;
quitFlag = false;
devicenum = device;
width = 6000;
height = 4000;
/////最大的2400W像素
InputArray = (unsigned char *)malloc(width * height * 3);
CameraDeviceStr = str;
FrameCount = 0;
}
void CaptureThread::release()
{
free(InputArray);
free(OutputImg);
}
void CaptureThread::run()
{
forever
{
if (!pause_status)
{
if (quitFlag)
break;
if (CameraGetImageBuffer(devicenum, &g_tFrameHead, &g_pRawBuffer, 200) == CAMERA_STATUS_SUCCESS)
{
if (quitFlag)
break;
CameraImageProcess(devicenum, g_pRawBuffer, (unsigned char *)InputArray, &g_tFrameHead);
if (FrameCount == 0)
{
OutputImg = new cv::Mat(g_tFrameHead.iHeight, g_tFrameHead.iWidth, CV_8UC1);
}
CameraReleaseImageBuffer(devicenum,g_pRawBuffer);
OutputImg->data = (unsigned char *)InputArray;
FrameCount++;
emit captured(OutputImg);
}
else
{
printf("timeout \n");
usleep(1000);
}
}
else usleep(1000);
if (quitFlag) break;
}
}
void CaptureThread::stream()
{
pause_status = false;
}
void CaptureThread::pause()
{
pause_status = true;
}
void CaptureThread::stop()
{
pause_status = true;
quitFlag = true;
}