借鉴前人经验 http://blog.csdn.net/luoyayun361/article/details/61936032
利用
继承QQuickImageProvider类的方法来完成opencv读取摄像头每一帧,并利用QImage绘制并发送给qml端的Image
直接上代码
#ifndef IMAGEPROVIDER_H
#define IMAGEPROVIDER_H
#include
#include
class ImageProvider : public QQuickImageProvider
{
public:
ImageProvider();
QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize);
QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize);
QImage img;
};
#endif // IMAGEPROVIDER_H
#include "imageprovider.h"
#include
ImageProvider::ImageProvider()
: QQuickImageProvider(QQuickImageProvider::Image)
/本例中未使用Pixmap来绘制图像,若要使用其,只需要将Image替换为Pixmap
{
}
QImage ImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
qDebug()<<"requestImage";
return this->img;
}
QPixmap ImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
{
qDebug()<<"requestPixmap";
return QPixmap::fromImage(this->img);
}
#ifndef IMAGESHOW_H
#define IMAGESHOW_H
#include
#include
using namespace cv;
class ShowImage : public QObject
{
Q_OBJECT
public:
explicit ShowImage(QObject *parent = 0);
ImageProvider *m_pImgProvider;
QImage MattoQImage(Mat cvImg);
public slots:
void setImage();
void closeCamera();
signals:
void callQmlRefeshImg();
private:
Mat src_frame;
// QImage image;
VideoCapture capture;
};
#endif // IMAGESHOW_H
#include
#include
ShowImage::ShowImage(QObject *parent) :
QObject(parent)
{
m_pImgProvider = new ImageProvider();
capture=VideoCapture(0);
}
void ShowImage::closeCamera()
{
capture.release();
}
void ShowImage::setImage()
{
capture>>src_frame;
m_pImgProvider->img = MattoQImage(src_frame);
emit callQmlRefeshImg();
qDebug()<<"setImage";
}
QImage ShowImage::MattoQImage(Mat cvImg)
{
QImage qImg;
if(cvImg.channels()==3)
{
cvtColor(cvImg,cvImg,CV_BGR2RGB);
qImg =QImage((const unsigned char*)(cvImg.data),
cvImg.cols, cvImg.rows,
cvImg.cols*cvImg.channels(),
QImage::Format_RGB888);
}
else if(cvImg.channels()==1) {
qImg =QImage((const unsigned char*)(cvImg.data),
cvImg.cols,cvImg.rows,
cvImg.cols*cvImg.channels(),
QImage::Format_Indexed8);
}
else
{
qImg =QImage((const unsigned char*)(cvImg.data),
cvImg.cols,cvImg.rows,
cvImg.cols*cvImg.channels(),
QImage::Format_RGB888);
}
return qImg;
}
#include
#include
#include
#include
#include
#include
using namespace cv;
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
ShowImage *CodeImage = new ShowImage();
QQmlApplicationEngine W;
W.rootContext()->setContextProperty("CodeImage",CodeImage);
W.addImageProvider(QLatin1String("CodeImg"), CodeImage->m_pImgProvider);
W.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World");
Timer{
id:count;
interval: 25;
running: false;
repeat: true
onTriggered: {
CodeImage.setImage();
}
}
Button{
id:open;
text:"打开摄像头"
width:100;
height:30;
anchors.left: parent.left;
anchors.top:parent.top;
anchors.leftMargin: 10;
anchors.topMargin: 10;
onClicked:{
count.running=true;
}
}
Button{
id:quit;
text:"关闭摄像头"
width:100;
height:30;
anchors.left: parent.left;
anchors.top:open.bottom;
anchors.leftMargin: 10;
anchors.topMargin: 10;
onClicked:{
count.running=false;
CodeImage.closeCamera();
Qt.quit();
}
}
Image{
id:img;
width:400;
height:400;
anchors.centerIn: parent;
cache:false;
}
Connections{
target: CodeImage
onCallQmlRefeshImg:{
img.source = "";
img.source = "image://CodeImg";
console.log("no");
}
}
}