Created
April 11, 2018 21:32
-
-
Save glum-psyche/0b0e9726ed17c5c74f30682979c2d69e to your computer and use it in GitHub Desktop.
Camera capture via Qt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TEMPLATE = app | |
QT += \ | |
widgets \ | |
multimedia | |
CONFIG += c++11 c++14 | |
SOURCES += \ | |
main.cpp \ | |
shooterview.cpp | |
HEADERS += \ | |
shooterview.h |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QApplication> | |
#include "shooterview.h" | |
int main(int argc, char** argv) | |
{ | |
QApplication app(argc, argv); | |
ShooterView shooter; | |
shooter.show(); | |
shooter.start(); | |
return app.exec(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "shooterview.h" | |
#include <QTimer> | |
#include <QLabel> | |
#include <QVBoxLayout> | |
#include <QCamera> | |
#include <QCameraImageCapture> | |
#include <QEventLoop> | |
ShooterView::ShooterView(QWidget *parent) : | |
QWidget(parent), | |
m_timer(new QTimer(this)), | |
m_image(new QLabel(this)) | |
{ | |
resize(640, 480); | |
m_timer->setInterval(10000); | |
m_timer->setSingleShot(false); | |
connect(m_timer.data(), &QTimer::timeout, this, &ShooterView::shoot); | |
QVBoxLayout *layout = new QVBoxLayout(this); | |
layout->addWidget(m_image); | |
} | |
ShooterView::~ShooterView() | |
{ | |
} | |
void ShooterView::start() | |
{ | |
m_timer->start(); | |
} | |
void ShooterView::stop() | |
{ | |
m_timer->stop(); | |
} | |
void ShooterView::shoot() | |
{ | |
qDebug() << "SHOOT"; | |
QScopedPointer<QCamera, QScopedPointerDeleteLater> camera(new QCamera); | |
/*QScopedPointer<QCameraViewfinder, QScopedPointerDeleteLater> | |
viewfinder = new QCameraViewfinder(); | |
viewfinder->show(); | |
camera->setViewfinder(viewfinder);*/ | |
QScopedPointer<QCameraImageCapture, QScopedPointerDeleteLater> | |
imageCapture(new QCameraImageCapture(camera.data())); | |
connect(imageCapture.data(), &QCameraImageCapture::imageCaptured, | |
this, &ShooterView::handleImage); | |
camera->setCaptureMode(QCamera::CaptureStillImage); | |
imageCapture->setCaptureDestination(QCameraImageCapture::CaptureToBuffer); | |
QEventLoop loop; | |
connect(imageCapture.data(), &QCameraImageCapture::readyForCaptureChanged, | |
&loop, &QEventLoop::quit); | |
connect(this, &ShooterView::imageReady, &loop, &QEventLoop::quit); | |
camera->start(); | |
//on half pressed shutter button | |
camera->searchAndLock(); | |
loop.exec(); | |
//on shutter button pressed | |
imageCapture->capture(); | |
loop.exec(); | |
//on shutter button released | |
camera->unlock(); | |
} | |
void ShooterView::handleImage(int id, const QImage& image) | |
{ | |
qDebug() << "ID:" << id << "IMAGE:" << image.size(); | |
emit imageReady(); | |
m_image->setPixmap(QPixmap::fromImage(image)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SHOOTERVIEW_H | |
#define SHOOTERVIEW_H | |
#include <QWidget> | |
#include <QPointer> | |
QT_FORWARD_DECLARE_CLASS(QTimer) | |
QT_FORWARD_DECLARE_CLASS(QLabel) | |
class ShooterView : public QWidget | |
{ | |
Q_OBJECT | |
public: | |
explicit ShooterView(QWidget *parent = nullptr); | |
virtual ~ShooterView(); | |
void start(); | |
void stop(); | |
private slots: | |
void shoot(); | |
void handleImage(int id, const QImage &image); | |
signals: | |
void imageReady(); | |
private: | |
QScopedPointer<QTimer, QScopedPointerDeleteLater> m_timer; | |
QPointer<QLabel> m_image; | |
}; | |
#endif // SHOOTERVIEW_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment