Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ofZach/e438bc0274cc1e11e3c8cff41db25665 to your computer and use it in GitHub Desktop.
Save ofZach/e438bc0274cc1e11e3c8cff41db25665 to your computer and use it in GitHub Desktop.
#include <videocapture/Capture.h>
using namespace ca;
class videoCaptureThreaded : public ofThread{
public:
static void fcallback(PixelBuffer& buffer){
((videoCaptureThreaded *)buffer.user)->handleFrame(buffer);
}
void handleFrame(PixelBuffer& buffer){
//printf("%i %i \n", buffer.width[0], buffer.height[0]);
//printf("Got a pixel buffer, size: %lu.\n", (buffer.nbytes / 4) * 2 / 1280);
//cout << "hi! " <<endl;
unsigned char * data = (unsigned char*)buffer.plane[0];
for (int i = 0; i < 1280 * 720; i++){
unlockedData[i] = data[i*2+1];
}
lock();
bIsFrameNew = true;
memcpy(lockedData, unlockedData, 1280*720);
unlock();
}
bool bNewFrame(){
bool bFrameNew = false;
lock();
if (bIsFrameNew == true){
bFrameNew = true;
bIsFrameNew = false;
}
unlock();
return bFrameNew;
}
unsigned char * getPixels(){
lock();
memcpy(returnData, lockedData, 1280*720);
unlock();
return returnData;
}
Settings cfg;
Capture *cap;
unsigned char * lockedData;
unsigned char * unlockedData;
unsigned char * returnData;
bool bIsFrameNew;
ofMutex mutex;
~videoCaptureThreaded(){
stopThread();
ofSleepMillis(500);
cap->stop();
}
void setup(){
lockedData = new unsigned char[1280*720];
unlockedData = new unsigned char[1280*720];
returnData = new unsigned char[1280*720];
int r = 0;
int formats[] = { CA_JPEG_OPENDML } ;
int width = 1280;
int height = 720;
cfg.device = 0;
cfg.capability = 0;
cfg.format = CA_UYVY422;
cap = new Capture(fcallback, (void *) this);
/* List the devices and output formats. */
cap->listDevices();
cap->listOutputFormats();
/* Check if there the capability is supported. */
cfg.capability = cap->findCapability(cfg.device, width, height, formats, 1);
if (cfg.capability < 0) {
cap->listCapabilities(cfg.device);
printf("Error: failed to find the capability.\n");
return 1;
}
printf("We found a capability for %d x %d, capability: %d\n", width, height, cfg.capability);
/* Open the capture device. */
r = cap->open(cfg);
if (r < 0) {
cap->listCapabilities(cfg.device);
printf("Error: cannot open the device: %d\n", r);
return 1;
}
/* And start iterating. */
}
void threadedFunction(){
/* Start capturing. */
if (cap->start() < 0) {
cap->listCapabilities(cfg.device);
printf("Error: failed to start capturing.\n");
return 1;
}
while (true) {
cap->update();
ofSleepMillis(1000/30);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment