[참고]
Tutorial Qt Creator - QCamera(WebCam en C++)
https://www.youtube.com/watch?v=9oQZPqi47Y8
$ sudo apt-get update
$ sudo apt-get install qtmultimedia5-dev // 우분투 16.04에 설치하기(QCamera 모듈 불러오려면 설치 필요)
(Qt5에서 실행)
1. Welcome - Project - New Project - Application - Qt Widgets Application
2. Name : CameraTest - 작업폴더 지정 - Next
3. Kit Selection(데스크탑 or 임베디드 or 기타 ) 선택하기(동시선택 가능) - Next
4. Details - Class Information - Base class: - QWidget 선택 - Next - Finish
5. code 작성
"CameraTest.pro"
QT += core gui multimedia multimediawidgets // 멀티미디어 모듈을 모듈화
(...생략...) // 자동생성
QMAKE_CXXFLAGS += -std=gnu++14 // 표준 2014 추가
"widget.h"
(...생략...) //자동생성
class QCamera;
class QCameraViewfinder;
class QCameraImageCaputre;
class QVBoxLayout;
class Widget : public Qwidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr); // 0을 nullptr로 변경
~Widget();
private:
Ui::Widget *ui;
QCamera *mCamera;
QCameraViewfinder *mCameraViewfinder;
QCameraImageCapture *mCameraImageCaputre;
QVBoxLayout *mLayout;
};
#endif
"widget.cpp"
#include "widget.h"
#include "ui_widget.h"
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QVBoxLayout>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
mCamera = new QCamera(this);
mCameraViewfinder = new QCameraViewfinder(this);
mCameraImageCaputre = new QCameraImageCapture(mCamera, this);
mLayout = new QVBoxLayout;
mLayout ->addItem(mCameraViewfinder);
}
Widget::~Widget()
{
delete ui;
}
"widget.ui" (Design 영역. 코딩이 아닌 Drag&Drop으로 UI 만들기)
전 Object Name : optionPushButton 으로 함
"widget.h"
(...생략...) //자동생성
class QCamera;
class QCameraViewfinder;
class QCameraImageCaputre;
class QVBoxLayout;
class QMenu;
class QAction;
class Widget : public Qwidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr); // 0을 nullptr로 변경
~Widget();
private:
Ui::Widget *ui;
QCamera *mCamera;
QCameraViewfinder *mCameraViewfinder;
QCameraImageCapture *mCameraImageCaputre;
QVBoxLayout *mLayout;
QMenu *mOpcionesMenu;
QAction *mEncenderAction;
QAction *mApagarAction;
QAction *mCapturarAction;
};
#endif
"widget.cpp"
#include "widget.h"
#include "ui_widget.h"
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QVBoxLayout>
#include <QMenu>
#include <QAction>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
mCamera = new QCamera(this);
mCameraViewfinder = new QCameraViewfinder(this);
mCameraImageCaputre = new QCameraImageCapture(mCamera, this);
mLayout = new QVBoxLayout;
mOpcionesMenu = new QMenu("Option", this);
mEncenderAction = new QAction("Encender", this);
mApagarAction = new QAction("Apagar", this);
mCapturarAction = new QAction("Capturar", this);
mOptionMenu->addAction({mEncenderAction, mApagarAction, mCapturarAction});
ui->optionPushButton->setMenu(mOptionMenu);
mCamera->setViewfinder(mCameraViewfinder);
mLayout->addWidget(mCameraViewfinder); //Item을 Widget으로 수정
mLayout->setMargin(0);
ui->scrollArea->setLayout(mLayout);
connect(mEncenderAction, &QAction::triggered, [&](){
mCamera->start();
});
connect(mAagarAction, &QAction::triggered, [&](){
mCamera->stop();
});
connect(mCapturarAction, &QAction::triggered, [&](){
});
}
Widget::~Widget()
{
delete ui;
}
"main.cpp"
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyple("fusion")
Widget w;
w.show();
return a.exec();
}
"widget.cpp"
#include "widget.h"
#include "ui_widget.h"
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QVBoxLayout>
#include <QMenu>
#include <QAction>
#include <QFileDialog>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
mCamera = new QCamera(this);
mCameraViewfinder = new QCameraViewfinder(this);
mCameraImageCaputre = new QCameraImageCapture(mCamera, this);
mLayout = new QVBoxLayout;
mOpcionesMenu = new QMenu("Option", this);
mEncenderAction = new QAction("Encender", this);
mApagarAction = new QAction("Apagar", this);
mCapturarAction = new QAction("Capturar", this);
mOptionMenu->addAction({mEncenderAction, mApagarAction, mCapturarAction});
ui->optionPushButton->setMenu(mOptionMenu);
mCamera->setViewfinder(mCameraViewfinder);
mLayout->addWidget(mCameraViewfinder); //Item을 Widget으로 수정
mLayout->setMargin(0);
ui->scrollArea->setLayout(mLayout);
connect(mEncenderAction, &QAction::triggered, [&](){
mCamera->start();
});
connect(mAagarAction, &QAction::triggered, [&](){
mCamera->stop();
});
connect(mCapturarAction, &QAction::triggered, [&](){
auto filename = QFileDialog::getSaveFileName(this, "Capturar", "/", "Imgae(*.jpg;*.jpeg)");
if (filename.isEmpty()){
return;
}
mCameraImageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
QImageEncoderSettings imageEncorderSettings;
imageEncorderSettings.setCodec("image/jpeg");
imageEncorderSettings.setResolution(1288, 964);
mCameraImageCapture->setEncodingSettings(imageEncorderSettings);
mCamera->setCaptureMode(QCamera::CaptureStillImage);
mCamera->start();
mCamera->searchAndLock();
mCameraImageCapture->capture(filename);
mCamera->unlock();
});
}
Widget::~Widget()
{
delete ui;
}
오류 발생! 동영상 보고 코드 다시 확인해보기!
'#C&C++ #영상비전#Qt' 카테고리의 다른 글
#qt 학습 사이트 (0) | 2018.09.13 |
---|---|
#opencv 2.4.13 설치하기(c/c++) (0) | 2018.09.05 |
#Qt #Qt 개발자 커뮤니티 (0) | 2018.08.28 |
#Qt버전 확인 #qt 설치 #qmake (0) | 2018.08.24 |
#Qt #QVision환경설정 (0) | 2018.08.24 |