Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt使用ShaderEffect进行QImage渲染 我想知道是否可以通过Qt快速或C++来在图像上使用着色器效果,然后用效果来保存它。_Qt_Opengl_Shader - Fatal编程技术网

Qt使用ShaderEffect进行QImage渲染 我想知道是否可以通过Qt快速或C++来在图像上使用着色器效果,然后用效果来保存它。

Qt使用ShaderEffect进行QImage渲染 我想知道是否可以通过Qt快速或C++来在图像上使用着色器效果,然后用效果来保存它。,qt,opengl,shader,Qt,Opengl,Shader,这样做的原因是为了实现opengl辅助的图像效果处理(即浮雕、像素化等) 我试过这个: import QtQuick 2.4 import QtQuick.Controls 1.3 import QtQuick.Dialogs 1.2 ApplicationWindow { id: root title: qsTr("Hello World") width: 640 height: 480 visible: true Image {

这样做的原因是为了实现opengl辅助的图像效果处理(即浮雕、像素化等)

我试过这个:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Dialogs 1.2

ApplicationWindow {
    id: root
    title: qsTr("Hello World")
    width: 640
    height: 480
    visible: true

    Image {
        id: effectImage
        width: 200
        height: 200
        sourceSize.width: width
        sourceSize.height: height

        anchors.centerIn: parent
        source: "qrc:/myimage.png"
        layer.enabled: true
        layer.effect: ShaderEffect {
            fragmentShader: "
                uniform lowp sampler2D source; // this item
                uniform lowp float qt_Opacity; // inherited opacity of this item
                varying highp vec2 qt_TexCoord0;
                void main() {
                    lowp vec4 p = texture2D(source, qt_TexCoord0);
                    lowp float g = dot(p.xyz, vec3(0.344, 0.5, 0.156));
                    gl_FragColor = vec4(g, g, g, p.a) * qt_Opacity;
                }"
        }
    }
    Button {
        id: grabIt
        anchors.top: effectImage.bottom
        text: qsTr("Grab")
        onClicked: {
            effectImage.grabToImage(function(result) {
                console.debug("hallo");
                result.saveToFile("test.jpg","jpg");
            });
        }
    }
}
在图像上应用简单的灰度效果。它在屏幕上工作,但grabToImage()不应用该效果


有什么方法可以做到这一点吗?

好的,很简单,我用一个Item元素包装图像并从中获取图像,从而解决了这个问题:

    import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Dialogs 1.2

ApplicationWindow {
    id: root
    title: qsTr("Hello World")
    width: 640
    height: 480
    visible: true

    Item {
        id: effectImage
        width: 200
        height: 200
        anchors.centerIn: parent
        Image {
            anchors.fill: parent
            sourceSize.width: width
            sourceSize.height: height
            source: "qrc:/myimage.png"
            layer.enabled: true
            layer.effect: ShaderEffect {
                fragmentShader: "
                    uniform lowp sampler2D source; // this item
                    uniform lowp float qt_Opacity; // inherited opacity of this item
                    varying highp vec2 qt_TexCoord0;
                    void main() {
                        lowp vec4 p = texture2D(source, qt_TexCoord0);
                        lowp float g = dot(p.xyz, vec3(0.344, 0.5, 0.156));
                        gl_FragColor = vec4(g, g, g, p.a) * qt_Opacity;
                    }"
            }
        }
    }
    Button {
        id: grabIt
        anchors.top: effectImage.bottom
        text: qsTr("Grab")
        onClicked: {
            effectImage.grabToImage(function(result) {
                console.debug("hallo");
                result.saveToFile("test.jpg","jpg");
            });
        }
    }
}
希望这对其他人有帮助:-)