曾写了1个博客:
我们为何不习惯使用qml? 最后有1个悬念,到底如何在C++代码中获得qml中camera的数据呢?
QAbstractVideoFilter
The QAbstractVideoFilter class represents a filter that is applied to the video frames received by a VideoOutput type.
https://doc-snapshots.qt.io/qt5-dev/qabstractvideofilter.html#details
QVideoFilterRunnable
The QVideoFilterRunnable class represents the implementation of a filter that owns all graphics and computational resources, and performs the actual filtering or calculations.
上面两个类自己去看官方文档吧,翻译不好。
开始撸代码!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
写1个自己的类叫CameraFilter吧:
camerafilter.h
#ifndef CAMERAFILTER
#define CAMERAFILTER
#include <QVideoFilterRunnable>
class CameraFilter : public QAbstractVideoFilter
{
Q_OBJECT
public:
CameraFilter();
~CameraFilter();
QVideoFilterRunnable *createFilterRunnable();
private:
signals:
void finished(QObject *result);
void cameraFrameSignal(QVideoFrame);
public slots:
};
class CameraFilterRunnable : public QVideoFilterRunnable
{
public:
CameraFilterRunnable(CameraFilter* filter = NULL);
~CameraFilterRunnable();
QVideoFrame run(QVideoFrame *input, const QVideoSurfaceFormat &surfaceFormat, RunFlags flags);
private:
CameraFilter *m_filter;
};
#endif // CAMERAFILTER
camerafilter.cc
#include "camerafilter.h"
#include "qdebug.h"
CameraFilter::CameraFilter()
{
}
CameraFilter::~CameraFilter()
{
}
QVideoFilterRunnable *CameraFilter::createFilterRunnable()
{
return new CameraFilterRunnable(this);
}
CameraFilterRunnable::CameraFilterRunnable(CameraFilter* filter)
{
m_filter = filter;
}
CameraFilterRunnable::~CameraFilterRunnable()
{
}
QVideoFrame CameraFilterRunnable::run(QVideoFrame *input, const QVideoSurfaceFormat &surfaceFormat, RunFlags flags)
{
QVideoFrame cloneFrame(*input);
emit m_filter->cameraFrameSignal(cloneFrame);
return cloneFrame;
}
不要骂娘,上面的代码很简单的,其实官方的文档中也有类似的代码片断,不懂的自己看奥。
是通过:
emit m_filter->cameraFrameSignal(cloneFrame);
我们自己写1个slot,然后对Frame进行相应的处理!
接下来才是高潮!!!!
第1步、让qml中理解我们自己的类CameraFilter(Defining QML Types from C++) :
在C++中注册:
qmlRegisterType<CameraFilter>("CameraFilter", 1, 0, "CameraFilter");
在qml中导入:
import CameraFilter 1.0
第2步、在qml中干点大事儿:
Camera {
objectName: "CameraObject"
id: camera
imageProcessing.whiteBalanceMode: CameraImageProcessing.WhiteBalanceFlash
exposure {
exposureCompensation: -1.0
exposureMode: Camera.ExposurePortrait
}
captureMode: Camera.CaptureVideo
flash.mode: Camera.FlashRedEyeReduction
}
VideoOutput {
width: 160
height: 90
id: video_output
objectName: "VidelOutputObject"
source: camera
anchors.fill: parent
focus : visible // to receive focus and capture key events when visible
visible: false
filters: [filter]
fillMode: VideoOutput.PreserveAspectFit
}
CameraFilter {
objectName: "cameraFilterObject"
id: filter
}