Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AgiMaulana/e719c057bfdabbb6920125111d1b74f1 to your computer and use it in GitHub Desktop.
Save AgiMaulana/e719c057bfdabbb6920125111d1b74f1 to your computer and use it in GitHub Desktop.
#include "videoplayer.h"
void VideoPlayer::run()
{
int delay = (1000/frameRate);
while(!stop){
if (!capture.read(frame))
{
stop = true;
}
if (frame.channels()== 3){
cv::cvtColor(frame, RGBframe, CV_BGR2RGB);
img = QImage((const unsigned char*)(RGBframe.data),
RGBframe.cols,RGBframe.rows,QImage::Format_RGB888);
}
else
{
img = QImage((const unsigned char*)(frame.data),
frame.cols,frame.rows,QImage::Format_Indexed8);
}
emit processedImage(img);
this->msleep(delay);
}
}
void VideoPlayer::msleep(int ms)
{
struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 };
nanosleep(&ts, NULL);
}
VideoPlayer::VideoPlayer(QObject *parent) : QThread(parent)
{
stop = true;
}
VideoPlayer::~VideoPlayer()
{
mutex.lock();
stop = true;
capture.release();
condition.wakeOne();
mutex.unlock();
wait();
}
bool VideoPlayer::loadVideo(int filename)
{
capture.open(filename);
if (capture.isOpened())
{
frameRate = (int) capture.get(CV_CAP_PROP_FPS);
return true;
}
else
return false;
}
bool VideoPlayer::loadVideo(std::string filename)
{
capture.open(filename);
if (capture.isOpened())
{
frameRate = (int) capture.get(CV_CAP_PROP_FPS);
return true;
}
else
return false;
}
void VideoPlayer::Play()
{
if (!isRunning()) {
if (isStopped()){
stop = false;
}
start(LowPriority);
}
}
void VideoPlayer::Stop()
{
stop = true;
}
bool VideoPlayer::isStopped() const
{
return this->stop;
}
void VideoPlayer::release()
{
Stop();
capture.release();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment