我知道如何在 QML 中截取整个窗口的屏幕截图 https://stackoverflow.com/questions/33165733/qquickwindowgrabwindow-scene-graph-already-in-use?lq=1.
我有一个Video
QML 窗口中的元素。该视频显示在Rectangle
.
截图的方式是什么that Rectangle
而不是整个窗口?
这是一个非常有趣的问题。作为一个快速有效的解决方案,我可以建议您使用grabToImage http://doc.qt.io/qt-5/qml-qtquick-item.html#grabToImage-method的方法Item http://doc.qt.io/qt-5/qml-qtquick-item.html。它采用第一个参数作为回调函数,第二个参数作为要保存的路径。
我写了一个小函数来抓取任何Item
:
// what -- name of item needed to be grabbed
// where -- string
function render(what, where) {
// Find existent item with given name `what`
var i = 0
var found = false
for (i = 0; i < window.contentItem.children.length; i++) {
if (window.contentItem.children[i].objectName === what) {
// We found respective item
found = true
break
}
}
if (found) {
console.log("We found item " + what + ". Grabbing it to " + where)
var item = window.contentItem.children[i]
// Grab image and save it (via callback f-ion)
item.grabToImage( function(result) { result.saveToFile(where) })
} else {
console.warn("No item called " + what)
}
}
所以你可以在 QML/QtQuick 端使用它,就像在 Qt 上一样(使用QMetaObject::invokeMethod http://doc.qt.io/qt-4.8/qmetaobject.html#invokeMethod).
还有QQuickItem::grabToImage http://doc.qt.io/qt-5/qquickitem.html#grabToImage方法,但我很高兴看到任何适当的用法示例。
另一种抓取方式是使用ShaderEffectSource http://doc.qt.io/qt-5/qml-qtquick-shadereffectsource.html并按您的意愿使用。
我上面写的所有内容都是作为一个项目准备的,位于github https://github.com/troyane/StackOverflow-pro/tree/master/grab_qml_item。代码已注释,因此希望一切都会清楚。你可以拿它来做一些黑客活动。也欢迎拉请求。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)