1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Switch self cam view to OpenCV

This commit is contained in:
Tux3 / Mlkj / !Lev.uXFMLA 2014-08-29 13:40:37 +02:00
parent 99a4b03cfd
commit f4b1b64ae6
4 changed files with 37 additions and 8 deletions

View File

@ -45,7 +45,7 @@ win32 {
} macx {
LIBS += -L$$PWD/libs/lib/ -ltoxcore -ltoxav -lsodium -lvpx -framework OpenAL
} else {
LIBS += -L$$PWD/libs/lib/ -ltoxcore -ltoxav -lsodium -lvpx -lopenal
LIBS += -L$$PWD/libs/lib/ -ltoxcore -ltoxav -lsodium -lvpx -lopenal -lopencv_core -lopencv_highgui -lopencv_imgproc
}
#### Static linux build

View File

@ -20,6 +20,8 @@
#include <QVideoEncoderSettings>
#include <QVideoEncoderSettingsControl>
using namespace cv;
static inline void fromYCbCrToRGB(
uint8_t Y, uint8_t Cb, uint8_t Cr,
uint8_t& R, uint8_t& G, uint8_t& B)
@ -75,7 +77,7 @@ void Camera::suscribe()
if (refcount <= 0)
{
refcount = 1;
camera->start();
cap.open(0);
}
else
refcount++;
@ -87,16 +89,23 @@ void Camera::unsuscribe()
if (refcount <= 0)
{
camera->stop();
cap.release();
refcount = 0;
}
}
QVideoFrame Camera::getLastFrame()
QVideoFrame Camera::getLastVideoFrame()
{
return lastFrame;
}
Mat Camera::getLastFrame()
{
Mat frame;
cap >> frame;
return frame;
}
bool Camera::start(const QVideoSurfaceFormat &format)
{
if(supportedFormats.contains(format.pixelFormat()))

View File

@ -21,6 +21,7 @@
#include <QVideoFrame>
#include <QAbstractVideoSurface>
#include "vpx/vpx_image.h"
#include "opencv2/opencv.hpp"
/**
* This class is a wrapper to share a camera's captured video frames
@ -39,7 +40,8 @@ public:
Camera();
void suscribe(); ///< Call this once before trying to get frames
void unsuscribe(); ///< Call this once when you don't need frames anymore
QVideoFrame getLastFrame(); ///< Get the last captured frame
cv::Mat getLastFrame(); ///< Get the last captured frame
QVideoFrame getLastVideoFrame(); ///< Get the last captured frame
QImage getLastImage(); ///< Convert the last frame to a QImage (can be expensive !)
vpx_image getLastVPXImage(); ///< Convert the last frame to a vpx_image (can be expensive !)
bool isFormatSupported(const QVideoSurfaceFormat & format) const;
@ -58,6 +60,8 @@ private:
QVideoFrame lastFrame;
int frameFormat;
QList<QVideoFrame::PixelFormat> supportedFormats;
cv::VideoCapture cap;
};
#endif // CAMERA_H

View File

@ -24,6 +24,8 @@
#include "videosurface.h"
#include "widget.h"
using namespace cv;
SelfCamView::SelfCamView(Camera* Cam, QWidget* parent)
: QWidget(parent), displayLabel{new QLabel},
mainLayout{new QHBoxLayout()}, cam(Cam)
@ -60,8 +62,22 @@ void SelfCamView::showEvent(QShowEvent* event)
event->accept();
}
void SelfCamView::updateDisplay()
{
displayLabel->setPixmap(QPixmap::fromImage(cam->getLastImage()));
QImage Mat2QImage(const cv::Mat3b &src) {
QImage dest(src.cols, src.rows, QImage::Format_ARGB32);
for (int y = 0; y < src.rows; ++y) {
const cv::Vec3b *srcrow = src[y];
QRgb *destrow = (QRgb*)dest.scanLine(y);
for (int x = 0; x < src.cols; ++x) {
destrow[x] = qRgba(srcrow[x][2], srcrow[x][1], srcrow[x][0], 255);
}
}
return dest;
}
void SelfCamView::updateDisplay()
{
Mat frame = cam->getLastFrame();
displayLabel->setPixmap(QPixmap::fromImage(Mat2QImage(frame)));
}