使用QSCreen::grabWindow()函数在qt中进行窗口预览
我想制作一个screen caster应用程序,直接将窗口或屏幕捕获到QQuickImage。为此,我制作了这个标题,它有一个thread对象,当收到信号时,它会频繁更新屏幕截图(抱歉,文件是用糟糕的语言编写的) 用我的qml图像,我做到了这一点使用QSCreen::grabWindow()函数在qt中进行窗口预览,qt,qml,qt5,qthread,Qt,Qml,Qt5,Qthread,我想制作一个screen caster应用程序,直接将窗口或屏幕捕获到QQuickImage。为此,我制作了这个标题,它有一个thread对象,当收到信号时,它会频繁更新屏幕截图(抱歉,文件是用糟糕的语言编写的) 用我的qml图像,我做到了这一点 Image { id: image anchors.fill: parent source: "images/kalaripayattu.svg" fillMode:
Image {
id: image
anchors.fill: parent
source: "images/kalaripayattu.svg"
fillMode: Image.PreserveAspectFit
cache:false
Component.onCompleted: {
justThread.newFile.connect(updateFunction)
}
function updateFunction(filename){
source = "file:./"+filename;
justThread.loaded()
}
}
JustThread{
signal stopThread
id:justThread
onStopThread: {
stop()
}
Component.onCompleted: {
startThread()
changePrint(id)
}
}
Component.onCompleted:{
applicationWindow.closing.connect(justThread.stopThread)
}
但它有一个非常可怕的fps。
有什么办法可以提高fps吗?
有什么简单的方法可以做到这一点吗?
Image
并不是专门用来显示频繁变化的图像的。你想要的是
下面是我要做的:
您需要创建一个QObject派生类实例,并将其设置为VideoOutput的源:
您的类需要具有Q\u属性(QAbstractVideoSurface*videoSurface READ videoSurface WRITE setVideoSurface NOTIFY videoSurfaceChanged)
在setVideoSurface
方法中,您需要使用正确的格式调用QAbstractVideoSurface的start方法(我想您的大小和pixelformat,pixelformat应该是QVideoFrame::format_ARGB32,用于桌面)
然后,当您想要更新视频输出(例如通过QTimer)时,可以使用从QScreen::grabWindow中获得的QPixmap构造的QVideoFrame调用QAbstractVideoSurface的当前方法
为此,您可以使用将QPixmap转换为QImage,然后使用将其转换为QVideoFrame。我将使用。一种更有效的方法是创建从QQuickItem派生的自定义项,用于绘制图像。是的,但是如果我想经常更新图像,我应该怎么做。我认为这样做没有问题。这取决于你选择哪种方法。您可以发送一个信号的情况下,图像供应商或更新它直接在案件的QQuickltem。非常好的解决方案!很抱歉,回复晚了。我在开会。非常感谢你的回答。我明天会检查一下。但是在尝试qApp->primaryScreen()->grabWindow(xwindowid)时,它在单个窗口中不起作用。toImage()不起作用不是一个好的诊断。什么和如何不起作用?您确定windowId吗?grabWindow返回的pixmap有效吗?toImage的图像呢?也许是转换成QVideoFrame失败了。我想这是因为
QVideoSurfaceFormat
的错误。下面是我设置格式的步骤。1) 使用pixmap=grabScreen(winId).toImage()
将pixmap转换为图像使用QVideoSurfaceFormat*format=new QSurfaceVideoFormat(pixmap.size(),QVideoFrame::pixelformatfromageformat(pixmap.format())
创建新格式,并开始使用surface->start(*格式)
然后start methode返回0,不显示任何消息。
Image {
id: image
anchors.fill: parent
source: "images/kalaripayattu.svg"
fillMode: Image.PreserveAspectFit
cache:false
Component.onCompleted: {
justThread.newFile.connect(updateFunction)
}
function updateFunction(filename){
source = "file:./"+filename;
justThread.loaded()
}
}
JustThread{
signal stopThread
id:justThread
onStopThread: {
stop()
}
Component.onCompleted: {
startThread()
changePrint(id)
}
}
Component.onCompleted:{
applicationWindow.closing.connect(justThread.stopThread)
}