34static cv::Size calc_optimal_camera_resolution(
const char* supported,
int width,
int height)
41 float min_diff = FLT_MAX;
49 while ((supported[idx] !=
'\0') && (supported[idx] !=
','))
52 sscanf(&supported[prev_idx],
"%dx%d", &tmp_width, &tmp_height);
54 int w_diff = width - tmp_width;
55 int h_diff = height - tmp_height;
56 if ((h_diff >= 0) && (w_diff >= 0))
58 if ((h_diff <= min_diff) && (tmp_height <= 720))
60 frame_width = tmp_width;
61 frame_height = tmp_height;
68 }
while (supported[idx - 1] !=
'\0');
70 return cv::Size(frame_width, frame_height);
77 mVI =
new videoInput();
79 mVI =
new cv::VideoCapture();
85 mBufferSize = cv::Size(640, 480);
93 ret = mVI->setupDevice(mIndex);
95 ret = mVI->setupDevice(mIndex, VI_COMPOSITE);
98 mBuffer.resize(mVI->getSize(mIndex));
102 mIndex = CV_CAP_ANDROID_BACK;
103 else if (mIndex == 1)
104 mIndex = CV_CAP_ANDROID_FRONT;
106 ret = mVI->open(mIndex);
110 mVI->set(CV_CAP_PROP_ANDROID_ANTIBANDING, CV_CAP_ANDROID_ANTIBANDING_OFF);
111 mVI->set(CV_CAP_PROP_ANDROID_FOCUS_MODE, CV_CAP_ANDROID_FOCUS_MODE_CONTINUOUS_VIDEO);
114 SetSize(mBufferSize.width, mBufferSize.height);
117 ret = mVI->open(mIndex);
126 mVI->stopDevice(mIndex);
135 mVI->stopDevice(mIndex);
147 return mVI->isDeviceSetup(mIndex);
149 return mVI->isOpened();
156 throw std::logic_error(
"Device not initialised");
160 if (!mVI->isFrameNew(mIndex) || !mVI->getPixels(mIndex, &mBuffer[0],
false,
true))
162 throw std::logic_error(
"Device has bad pixel buffer");
165 cv::Mat frame(mVI->getHeight(mIndex), mVI->getWidth(mIndex), CV_8UC3, &mBuffer[0]);
171 throw std::logic_error(
"Device has bad pixel buffer");
174 if (!mVI->retrieve(frame, CV_CAP_ANDROID_COLOR_FRAME_BGR) || frame.empty())
175 throw std::logic_error(
"frame is empty");
186 return mVI->getWidth(mIndex);
188 return mBufferSize.width;
200 return mVI->getHeight(mIndex);
202 return mBufferSize.height;
214 mVI->stopDevice(mIndex);
215 mVI->setupDevice(mIndex, width, height);
219 mVI->setupDevice(mIndex);
221 mBuffer.resize(mVI->getSize(mIndex));
223 union {
double prop;
const char* name; } u;
224 u.prop = mVI->get(CV_CAP_PROP_SUPPORTED_PREVIEW_SIZES_STRING);
226 mBufferSize = cv::Size(0, 0);
229 mBufferSize = calc_optimal_camera_resolution(u.name, width, height);
233 MMechostr(MSKRUNTIME,
">> Cannot get supported camera camera_resolutions\n");
236 MMechostr(MSKDEBUG,
">>>> Camera set size : %ix%i\n", mBufferSize.width, mBufferSize.height);
238 if ((mBufferSize.width != 0) && (mBufferSize.height != 0))
240 mVI->set(CV_CAP_PROP_FRAME_WIDTH, mBufferSize.width);
241 mVI->set(CV_CAP_PROP_FRAME_HEIGHT, mBufferSize.height);
245 mBufferSize = cv::Size(640, 480);
263 std::vector<std::string> result;
266 IBaseFilter *p_base_filter = NULL;
267 IMoniker *p_moniker = NULL;
272 ICreateDevEnum *p_dev_enum = NULL;
274 hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, IID_ICreateDevEnum, (
void **)&p_dev_enum);
282 IEnumMoniker *p_class_enum = NULL;
283 hr = p_dev_enum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &p_class_enum, 0);
285 p_dev_enum->Release();
293 if (p_class_enum == NULL)
306 while (p_class_enum->Next(1, &p_moniker, &i_fetched) == S_OK)
310 hr = p_moniker->BindToStorage(0, 0, IID_IPropertyBag, (
void **)&p_bag);
315 hr = p_bag->Read(L
"FriendlyName", &var, NULL);
320 int i_convert = WideCharToMultiByte(CP_ACP, 0, var.bstrVal, SysStringLen(var.bstrVal), NULL, 0, NULL, NULL);
321 char *p_buf = (
char *)malloc(i_convert + 1);
324 WideCharToMultiByte(CP_ACP, 0, var.bstrVal, SysStringLen(var.bstrVal), p_buf, i_convert, NULL, NULL);
325 SysFreeString(var.bstrVal);
326 p_buf[i_convert] =
'\0';
328 result.push_back(std::string(p_buf));
334 p_moniker->Release();
337 p_class_enum->Release();
341 unsigned int cameraNumber = 0;
342 cv::VideoCapture temp_camera;
344 for (
int i = 0; i < 128; i++)
346 cv::VideoCapture temp_camera(i);
347 if (temp_camera.isOpened())
350 sprintf(buffer,
"cam%d", cameraNumber);
351 result.push_back(buffer);
354 else if (temp_camera.open(i))
357 sprintf(buffer,
"cam%d", cameraNumber);
358 result.push_back(buffer);
360 temp_camera.release();
373 std::cout <<
"taking a snapshot of camera" << mIndex <<
"... ";
376 std::cout <<
"failed. camera not opened yet." << std::endl;
383 std::cout <<
"failed. Frame is empty." << std::endl;
387 if (!cv::imwrite(path, frame))
389 std::cout <<
"failed. Couldn't write to " << path <<
"." << std::endl;
393 std::cout <<
"success. Frame written to " << path <<
"." << std::endl;
405 MMechostr(MSKDEBUG,
">>>> updating frame");
408 catch (std::exception e)
410 MMechostr(MSKDEBUG,
">>>> %s", e.what());
416 MMechostr(MSKDEBUG,
">>>> frame empty!");
420 ANativeWindow* window = (ANativeWindow*)SCgetExtra(
"hscol");
423 MMechostr(MSKDEBUG,
"error: window empty !");
426 ANativeWindow_Buffer buffer;
429 if (ANativeWindow_lock(window, &buffer, 0) < 0)
434 cv::resize(frame, frame, cv::Size(buffer.width, buffer.height), 0, 0, cv::INTER_CUBIC);
436 for (
unsigned int y = 0; y < frame.rows; y++)
438 for (
unsigned int x = 0; x < frame.cols; x++)
440 unsigned long srcByte = (x * 3) + (frame.cols * 3 * y);
441 unsigned long destByte = (x * 4) + (buffer.width * 4 * y);
443 ((
unsigned char*)buffer.bits)[destByte + 2] = frame.data[srcByte];
444 ((
unsigned char*)buffer.bits)[destByte + 1] = frame.data[srcByte + 1];
445 ((
unsigned char*)buffer.bits)[destByte + 0] = frame.data[srcByte + 2];
446 ((
unsigned char*)buffer.bits)[destByte + 3] = 255;
451 ANativeWindow_unlockAndPost(window);
457 std::list<CameraInput*>::iterator it;