write t'll it is over

This commit is contained in:
Hiro Protagonist 2017-03-26 17:57:29 +13:00
parent 3a07c33d60
commit 60dc96f50b
6 changed files with 271 additions and 46 deletions

View file

@ -4,104 +4,113 @@
using namespace FlyCapture2;
CameraManager::CameraManager(QObject *parent) : QObject(parent), num_cameras {0}, camera_index {-1}, is_capturing {false} {
CameraManager::CameraManager( QObject * parent ) : QObject( parent ), num_cameras {0}, camera_index {-1}, is_capturing {false} {
image_buffer = new QVector<FlyCapture2::Image *>();
}
CameraManager::~CameraManager() {
delete image_buffer;
stopCapture();
camera.Disconnect();
stopCapture();
camera.Disconnect();
}
void CameraManager::connectCamera(int index) {
// Don't connect twice.
if(camera_index == index)
return;
void CameraManager::connectCamera( int index ) {
// Don't connect twice.
if ( camera_index == index )
return;
Error error; // general purpose error object
if(camera.IsConnected()){
if ( camera.IsConnected() ) {
error = camera.Disconnect();
if(error != PGRERROR_OK) {
if ( error != PGRERROR_OK ) {
throw error;
return;
}
}
}
qDebug() << "Connecting to camera: " << index << '\n';
// get PGRGuid of the Camera
BusManager bus_mgr;
error = bus_mgr.GetCameraFromIndex(index, &camera_guid);
if(error != PGRERROR_OK) {
throw error;
return;
}
qDebug() << "Connecting to camera: " << index << '\n';
// connectCamera
error = camera.Connect(&camera_guid);
if(error != PGRERROR_OK) {
throw error;
return;
}
// get PGRGuid of the Camera
BusManager bus_mgr;
error = bus_mgr.GetCameraFromIndex( index, &camera_guid );
if ( error != PGRERROR_OK ) {
throw error;
return;
}
// connectCamera
error = camera.Connect( &camera_guid );
if ( error != PGRERROR_OK ) {
throw error;
return;
}
cam_config = FlyCapture2::FC2Config();
cam_config.grabMode = GrabMode::BUFFER_FRAMES;
cam_config.numBuffers = 10; // We gonna move them into our own data structure.
cam_config.highPerformanceRetrieveBuffer = true;
camera.SetConfiguration(&cam_config);
camera.SetConfiguration( &cam_config );
camera_index = index;
}
// The capture callback is a wrapper to emit the frameCaptured signal.
void CameraManager::imageGrabbed(FlyCapture2::Image *image) {
void CameraManager::imageGrabbed( FlyCapture2::Image * image ) {
//thread Safe
static QMutex mutex;
mutex.lock();
image_buffer->append(image);
emit frameCaptured(image_buffer->front());
image_buffer->append( image );
emit frameCaptured( image_buffer->front() );
delete image_buffer->front();
image_buffer->removeFirst();
if(!is_capturing && image_buffer->empty())
emit stopped;
mutex.unlock();
return;
}
void CameraManager::stopCapture() {
if(!is_capturing)
return;
bool CameraManager::stopCapture() {
if ( !is_capturing )
return false;
grabber->stopCapturing();
camera.StopCapture();
Error error = camera.StopCapture();
if ( error != PGRERROR_OK ) {
throw error;
return false;
}
is_capturing = false;
is_capturing = false;
return image_buffer->empty();
}
void CameraManager::startCapture() {
// Don't capture twice!
if(is_capturing)
// Don't capture twice!
if ( is_capturing )
return;
Error error;
error = camera.StartCapture();
if(error != PGRERROR_OK) {
if ( error != PGRERROR_OK ) {
throw error;
return;
}
// Just my own async image grabbing!
// TODO: ERRORS!
grabber = new ImageGrabber(this);
grabber->setCamera(&camera);
connect(grabber, &ImageGrabber::imageCaptured, this, &CameraManager::imageGrabbed, Qt::QueuedConnection);
connect(grabber, &ImageGrabber::finished, grabber, &ImageGrabber::deleteLater);
grabber = new ImageGrabber( this );
grabber->setCamera( &camera );
connect( grabber, &ImageGrabber::imageCaptured, this, &CameraManager::imageGrabbed, Qt::QueuedConnection );
connect( grabber, &ImageGrabber::finished, grabber, &ImageGrabber::deleteLater );
grabber->start();
is_capturing = true;
is_capturing = true;
}

View file

@ -0,0 +1,116 @@
#include "cameramanager.h"
#include <QDebug>
#include <QMutex>
using namespace FlyCapture2;
CameraManager::CameraManager( QObject * parent ) : QObject( parent ), num_cameras {0}, camera_index {-1}, is_capturing {false} {
image_buffer = new QVector<FlyCapture2::Image *>();
}
CameraManager::~CameraManager() {
delete image_buffer;
stopCapture();
camera.Disconnect();
}
void CameraManager::connectCamera( int index ) {
// Don't connect twice.
if ( camera_index == index )
return;
Error error; // general purpose error object
if ( camera.IsConnected() ) {
error = camera.Disconnect();
if ( error != PGRERROR_OK ) {
throw error;
return;
}
}
qDebug() << "Connecting to camera: " << index << '\n';
// get PGRGuid of the Camera
BusManager bus_mgr;
error = bus_mgr.GetCameraFromIndex( index, &camera_guid );
if ( error != PGRERROR_OK ) {
throw error;
return;
}
// connectCamera
error = camera.Connect( &camera_guid );
if ( error != PGRERROR_OK ) {
throw error;
return;
}
cam_config = FlyCapture2::FC2Config();
cam_config.grabMode = GrabMode::BUFFER_FRAMES;
cam_config.numBuffers = 10; // We gonna move them into our own data structure.
cam_config.highPerformanceRetrieveBuffer = true;
camera.SetConfiguration( &cam_config );
camera_index = index;
}
// The capture callback is a wrapper to emit the frameCaptured signal.
void CameraManager::imageGrabbed( FlyCapture2::Image * image ) {
//thread Safe
static QMutex mutex;
mutex.lock();
image_buffer->append( image );
emit frameCaptured( image_buffer->front() );
delete image_buffer->front();
image_buffer->removeFirst();
if(!is_capturing && image_buffer->empty())
emit finishedCapturing;
mutex.unlock();
return;
}
bool CameraManager::stopCapture() {
if ( !is_capturing )
return false;
grabber->stopCapturing();
Error error = camera.StopCapture();
if ( error != PGRERROR_OK ) {
throw error;
return false;
}
is_capturing = false;
return image_buffer->empty();
}
void CameraManager::startCapture() {
// Don't capture twice!
if ( is_capturing )
return;
Error error;
error = camera.StartCapture();
if ( error != PGRERROR_OK ) {
throw error;
return;
}
// Just my own async image grabbing!
// TODO: ERRORS!
grabber = new ImageGrabber( this );
grabber->setCamera( &camera );
connect( grabber, &ImageGrabber::imageCaptured, this, &CameraManager::imageGrabbed, Qt::QueuedConnection );
connect( grabber, &ImageGrabber::finished, grabber, &ImageGrabber::deleteLater );
grabber->start();
is_capturing = true;
}

View file

@ -25,7 +25,7 @@ public:
// Starts capturing images. When an image has been captured, the signal frameCaptured is emited.
// Throws FlyCapture2::Error
void startCapture();
void stopCapture();
bool stopCapture();
//TODO remove, if not needed
const FlyCapture2::Camera* getCamera() const {
@ -76,6 +76,7 @@ private slots:
signals:
void frameCaptured(FlyCapture2::Image* image) const;
void stopped() const;
};
#endif // CAMERAMANAGER_H

View file

@ -0,0 +1,83 @@
#ifndef CAMERAMANAGER_H
#define CAMERAMANAGER_H
/*
This Class is an abstraction over the FlyCapture2 API for handeling the connection to the camera and
hiding the internals from the GUI code, to make reusability easier, although FlyCapture code is needed outside.
*/
// TODO: implement camera arrival removal
#include <QObject>
#include <QVector>
#include <QThread>
#include "FlyCapture2.h"
#include "imagegrabber.h"
class CameraManager : public QObject
{
Q_OBJECT
public:
explicit CameraManager(QObject *parent = 0);
~CameraManager();
// Starts capturing images. When an image has been captured, the signal frameCaptured is emited.
// Throws FlyCapture2::Error
void startCapture();
bool stopCapture();
//TODO remove, if not needed
const FlyCapture2::Camera* getCamera() const {
return &camera;
};
bool isConnected() {
return camera.IsConnected();
}
// Get the number of connected cameras.
unsigned int numCameras() {
FlyCapture2::BusManager bus_mgr;
bus_mgr.GetNumOfCameras(&num_cameras);
return num_cameras;
};
// Connect camera from index. Once successfull it emits the cameraConnected signal.
// Emits Error signal in case of an error.
void connectCamera(int);
bool isCapturing() {
return is_capturing;
}
private:
FlyCapture2::Camera camera;
FlyCapture2::FC2Config cam_config;
unsigned int num_cameras;
// GUID of the camera, which is currently being used
FlyCapture2::PGRGuid camera_guid;
// Index of the current camera
int camera_index;
// State Variable
bool is_capturing;
QVector<FlyCapture2::Image *> *image_buffer;
// Capture Thread
ImageGrabber *grabber;
private slots:
void imageGrabbed(FlyCapture2::Image *image);
signals:
void frameCaptured(FlyCapture2::Image* image) const;
void finishedCapturing() const;
};
#endif // CAMERAMANAGER_H

View file

@ -62,6 +62,10 @@ MainWindow::MainWindow( QWidget * parent ) :
// Start recording
connect( ui->startButton, &QPushButton::clicked, this, &MainWindow::startStopRecording );
// Stoppen Capturing, buffer empty...
connect( &camMan, &CameraManager::finishedCapturing, this, &MainWindow::startStopRecording, Qt::DirectConnection );
}
MainWindow::~MainWindow() {
@ -91,6 +95,10 @@ void MainWindow::setStatus( STATUS status ) {
ui->startButton->setText( "Stop" );
ui->recStats->show();
break;
case STOPPING:
ui->startButton->setText( "Stopping..." );
disableRecOptions();
break;
}
}
@ -266,13 +274,20 @@ void MainWindow::startStopRecording() {
} else {
// Stop Capture!
bool stopped;
try {
camMan.stopCapture();
stop = camMan.stopCapture();
} catch ( FlyCapture2::Error e ) {
showError( e );
return;
}
if ( !stopped ) {
setStatus(STOPPING);
// We just wait...
return;
}
try {
recorder.stopRecording();
} catch ( FlyCapture2::Error e ) {

View file

@ -40,7 +40,8 @@ class MainWindow : public QMainWindow {
enum STATUS {
WAITING,
CONNECTED,
RECORDING
RECORDING,
STOPPING
};
void setStatus(STATUS status);