Skip to content

Commit da35136

Browse files
committed
initial base app
1 parent 76febf4 commit da35136

7 files changed

+382
-15
lines changed

Animeme.pro

+10-4
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@ DEFINES += QT_DEPRECATED_WARNINGS
2222
# You can also select to disable deprecated APIs only up to a certain version of Qt.
2323
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
2424

25+
INCLUDEPATH += /usr/include/opencv2
26+
27+
LIBS += `pkg-config --cflags --libs opencv`
28+
2529
SOURCES += \
26-
main.cpp \
27-
mainwindow.cpp
30+
main.cpp \
31+
mainwindow.cpp \
32+
loopplayer.cpp
2833

2934
HEADERS += \
30-
mainwindow.h
35+
mainwindow.h \
36+
loopplayer.h
3137

3238
FORMS += \
33-
mainwindow.ui
39+
mainwindow.ui

loopplayer.cpp

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include "loopplayer.h"
2+
3+
#include <iostream>
4+
5+
LoopPlayer::LoopPlayer(QObject *parent) : QThread(parent)
6+
{
7+
stop = true;
8+
}
9+
10+
LoopPlayer::~LoopPlayer()
11+
{
12+
mutex.lock();
13+
stop = true;
14+
condition.wakeOne();
15+
mutex.unlock();
16+
wait();
17+
}
18+
19+
bool LoopPlayer::loadVideo(std::string filename)
20+
{
21+
cv::VideoCapture capture;
22+
capture.open(filename);
23+
if (capture.isOpened())
24+
{
25+
frameRate = (int)capture.get(CV_CAP_PROP_FPS);
26+
27+
cv::Mat frame;
28+
while (capture.read(frame))
29+
{
30+
if (frame.channels() == 3)
31+
{
32+
cv::cvtColor(frame, frame, CV_BGR2RGB);
33+
}
34+
frames.push_back(frame.clone());
35+
}
36+
37+
capture.release();
38+
return true;
39+
}
40+
41+
return false;
42+
}
43+
44+
void LoopPlayer::play()
45+
{
46+
if (!isRunning())
47+
{
48+
if (isStopped())
49+
{
50+
stop = false;
51+
}
52+
53+
start(LowPriority);
54+
}
55+
}
56+
57+
void LoopPlayer::run()
58+
{
59+
int delay = 1000.0 / frameRate;
60+
61+
auto it = frames.begin();
62+
while (!stop) {
63+
if (it == frames.end())
64+
{
65+
it = frames.begin();
66+
}
67+
68+
cv::Mat frame = *it;
69+
if (frame.channels() == 3)
70+
{
71+
img = QImage((const unsigned char*)(frame.data),
72+
frame.cols,
73+
frame.rows,
74+
QImage::Format_RGB888);
75+
}
76+
else
77+
{
78+
img = QImage((const unsigned char*)(frame.data),
79+
frame.cols,
80+
frame.rows,
81+
QImage::Format_Indexed8);
82+
}
83+
84+
emit processedImage(img);
85+
this->msleep(delay);
86+
87+
it++;
88+
}
89+
}
90+
91+
void LoopPlayer::pause()
92+
{
93+
stop = true;
94+
}
95+
96+
void LoopPlayer::msleep(int ms)
97+
{
98+
struct timespec ts =
99+
{
100+
ms / 1000, (ms % 1000) * 1000 * 1000
101+
};
102+
103+
nanosleep(&ts, NULL);
104+
}
105+
106+
bool LoopPlayer::isStopped() const
107+
{
108+
return stop;
109+
}
110+

loopplayer.h

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef LOOPPLAYER_H
2+
#define LOOPPLAYER_H
3+
4+
#include <vector>
5+
#include <QMutex>
6+
#include <QThread>
7+
#include <QImage>
8+
#include <QWaitCondition>
9+
#include <opencv2/core/core.hpp>
10+
#include <opencv2/imgproc/imgproc.hpp>
11+
#include <opencv2/highgui/highgui.hpp>
12+
13+
class LoopPlayer : public QThread
14+
{ Q_OBJECT
15+
private:
16+
bool stop;
17+
QMutex mutex;
18+
QWaitCondition condition;
19+
std::vector<cv::Mat> frames;
20+
int frameRate;
21+
QImage img;
22+
23+
signals:
24+
void processedImage(const QImage &image);
25+
26+
protected:
27+
void run();
28+
void msleep(int ms);
29+
public:
30+
LoopPlayer(QObject *parent = 0);
31+
~LoopPlayer();
32+
bool loadVideo(std::string filename);
33+
void play();
34+
void pause();
35+
bool isStopped() const;
36+
};
37+
38+
#endif // LOOPPLAYER_H

main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ int main(int argc, char *argv[])
55
{
66
QApplication a(argc, argv);
77
MainWindow w;
8+
w.setAttribute(Qt::WA_DeleteOnClose);
89
w.show();
910

1011
return a.exec();

mainwindow.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,72 @@ MainWindow::MainWindow(QWidget *parent) :
55
QMainWindow(parent),
66
ui(new Ui::MainWindow)
77
{
8+
player = new LoopPlayer();
9+
QObject::connect(player, SIGNAL(processedImage(QImage)), this, SLOT(updateLoopPlayerUI(QImage)));
10+
11+
if (!player->loadVideo("/home/shin/Videos/59adf78ac4c0bc657943222d.mp4"))
12+
{
13+
QMessageBox msgBox;
14+
msgBox.setText("The selected video could not be opened!");
15+
msgBox.exec();
16+
}
17+
18+
player->play();
19+
820
ui->setupUi(this);
921
}
1022

1123
MainWindow::~MainWindow()
1224
{
25+
delete player;
1326
delete ui;
1427
}
28+
29+
void MainWindow::updateLoopPlayerUI(QImage img)
30+
{
31+
if (!img.isNull())
32+
{
33+
ui->playerScreen->setAlignment(Qt::AlignCenter);
34+
ui->playerScreen->setPixmap(QPixmap::fromImage(img)
35+
.scaled(
36+
ui->playerScreen->size(),
37+
Qt::KeepAspectRatio,
38+
Qt::FastTransformation)
39+
);
40+
}
41+
}
42+
43+
void MainWindow::on_openFileButton_clicked()
44+
{
45+
QString filename = QFileDialog::getOpenFileName(this,
46+
tr("Open Video"),
47+
".",
48+
tr("Video Files (*.avi *.mpg *.mp4")
49+
);
50+
51+
if (!filename.isEmpty())
52+
{
53+
if (!player->loadVideo(filename.toStdString().data()))
54+
{
55+
QMessageBox msgBox;
56+
msgBox.setText("The selected video could not be opened!");
57+
msgBox.exec();
58+
}
59+
60+
player->play();
61+
}
62+
}
63+
64+
void MainWindow::on_pushButton_2_clicked()
65+
{
66+
if (player->isStopped())
67+
{
68+
player->play();
69+
ui->pushButton_2->setText(tr("Pause"));
70+
}
71+
else
72+
{
73+
player->pause();
74+
ui->pushButton_2->setText(tr("Play"));
75+
}
76+
}

mainwindow.h

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#define MAINWINDOW_H
33

44
#include <QMainWindow>
5+
#include <QFileDialog>
6+
#include <QMessageBox>
7+
#include "loopplayer.h"
58

69
namespace Ui {
710
class MainWindow;
@@ -15,8 +18,14 @@ class MainWindow : public QMainWindow
1518
explicit MainWindow(QWidget *parent = 0);
1619
~MainWindow();
1720

21+
private slots:
22+
void updateLoopPlayerUI(QImage img);
23+
void on_openFileButton_clicked();
24+
void on_pushButton_2_clicked();
25+
1826
private:
1927
Ui::MainWindow *ui;
28+
LoopPlayer *player;
2029
};
2130

2231
#endif // MAINWINDOW_H

0 commit comments

Comments
 (0)