Merge pull request #284 from mvines/a512cf9

Add support for VideoCapture grab/retrieve
This commit is contained in:
Peter Braden 2015-08-22 18:02:37 +02:00
commit 2691de4fa2
2 changed files with 64 additions and 3 deletions

View File

@ -37,6 +37,8 @@ VideoCaptureWrap::Init(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(ctor, "setPosition", SetPosition); NODE_SET_PROTOTYPE_METHOD(ctor, "setPosition", SetPosition);
NODE_SET_PROTOTYPE_METHOD(ctor, "close", Close); NODE_SET_PROTOTYPE_METHOD(ctor, "close", Close);
NODE_SET_PROTOTYPE_METHOD(ctor, "ReadSync", ReadSync); NODE_SET_PROTOTYPE_METHOD(ctor, "ReadSync", ReadSync);
NODE_SET_PROTOTYPE_METHOD(ctor, "grab", Grab);
NODE_SET_PROTOTYPE_METHOD(ctor, "retrieve", Retrieve);
target->Set(NanNew("VideoCapture"), ctor->GetFunction()); target->Set(NanNew("VideoCapture"), ctor->GetFunction());
}; };
@ -141,8 +143,10 @@ NAN_METHOD(VideoCaptureWrap::Close){
class AsyncVCWorker : public NanAsyncWorker { class AsyncVCWorker : public NanAsyncWorker {
public: public:
AsyncVCWorker(NanCallback *callback, VideoCaptureWrap* vc, Matrix* matrix) AsyncVCWorker(NanCallback *callback, VideoCaptureWrap* vc, Matrix* matrix,
: NanAsyncWorker(callback), vc(vc), matrix(matrix) {} bool retrieve = false, int channel = 0)
: NanAsyncWorker(callback), vc(vc), matrix(matrix),
retrieve(retrieve), channel(channel) {}
~AsyncVCWorker() {} ~AsyncVCWorker() {}
// Executed inside the worker-thread. // Executed inside the worker-thread.
@ -150,6 +154,12 @@ class AsyncVCWorker : public NanAsyncWorker {
// here, so everything we need for input and output // here, so everything we need for input and output
// should go on `this`. // should go on `this`.
void Execute () { void Execute () {
if (retrieve) {
if (!this->vc->cap.retrieve(matrix->mat, channel)) {
SetErrorMessage("retrieve failed");
}
return;
}
this->vc->cap.read(matrix->mat); this->vc->cap.read(matrix->mat);
} }
@ -180,6 +190,8 @@ class AsyncVCWorker : public NanAsyncWorker {
private: private:
VideoCaptureWrap *vc; VideoCaptureWrap *vc;
Matrix* matrix; Matrix* matrix;
bool retrieve;
int channel;
}; };
@ -210,3 +222,49 @@ NAN_METHOD(VideoCaptureWrap::ReadSync) {
NanReturnValue(im_to_return); NanReturnValue(im_to_return);
} }
class AsyncGrabWorker : public NanAsyncWorker {
public:
AsyncGrabWorker(NanCallback *callback, VideoCaptureWrap* vc)
: NanAsyncWorker(callback), vc(vc) {}
~AsyncGrabWorker() {}
void Execute () {
if (!this->vc->cap.grab()) {
SetErrorMessage("grab failed");
}
}
private:
VideoCaptureWrap *vc;
};
NAN_METHOD(VideoCaptureWrap::Grab) {
NanScope();
VideoCaptureWrap *v = ObjectWrap::Unwrap<VideoCaptureWrap>(args.This());
REQ_FUN_ARG(0, cb);
NanCallback *callback = new NanCallback(cb.As<Function>());
NanAsyncQueueWorker(new AsyncGrabWorker(callback, v));
NanReturnUndefined();
}
NAN_METHOD(VideoCaptureWrap::Retrieve) {
NanScope();
VideoCaptureWrap *v = ObjectWrap::Unwrap<VideoCaptureWrap>(args.This());
int channel = 0;
REQ_FUN_ARG(0, cb);
INT_FROM_ARGS(channel, 1);
NanCallback *callback = new NanCallback(cb.As<Function>());
NanAsyncQueueWorker(new AsyncVCWorker(callback, v, new Matrix(), true, channel));
NanReturnUndefined();
}

View File

@ -14,6 +14,9 @@ class VideoCaptureWrap: public node::ObjectWrap {
static NAN_METHOD(Read); static NAN_METHOD(Read);
static NAN_METHOD(ReadSync); static NAN_METHOD(ReadSync);
static NAN_METHOD(Grab);
static NAN_METHOD(Retrieve);
//(Optional) For setting width and height of the input video stream //(Optional) For setting width and height of the input video stream
static NAN_METHOD(SetWidth); static NAN_METHOD(SetWidth);
static NAN_METHOD(SetHeight); static NAN_METHOD(SetHeight);