使用Camera2+ opengl动态修改图像饱和度

2018-10-09  本文已影响124人  vb12

示例效果:

效果图

参考如下示例完成:
https://github.com/aleksas/androidGlesCamera2

片元着色器:

#extension GL_OES_EGL_image_external : require
precision mediump float;
varying vec2 aCoordinate;
uniform samplerExternalOES vTexture;

uniform lowp float saturation;
// Values from "Graphics Shaders: Theory and Practice" by Bailey and Cunningham
const mediump vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);
void main(){
    lowp vec4 textureColor = texture2D(vTexture, aCoordinate);
    lowp float luminance = dot(textureColor.rgb, luminanceWeighting);
    lowp vec3 greyScaleColor = vec3(luminance);
    gl_FragColor = vec4(mix(greyScaleColor, textureColor.rgb, saturation), textureColor.w);
}

使用GLSurfaceView显示, 所有对egl环境就不需要自己初始化了, 只需要关注生成一个纹理Texture:

GLES20.glGenTextures(1, hTex, 0)
        GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, hTex[0])
        GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE)
        GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE)
        GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST)
        GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST)

....

        mSurfaceTexture = SurfaceTexture(hTex[0])
        mSurfaceTexture.setDefaultBufferSize(mSurfaceView.width, mSurfaceView.height)
        mSurfaceTexture.setOnFrameAvailableListener(this)

然后把这个创建的mSurfaceTexture 对象传给Camera:

            val surface = Surface(surfaceTexture)

            // We set up a CaptureRequest.Builder with the output Surface.
            mPreviewRequestBuilder = mCameraDevice!!.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
            mPreviewRequestBuilder!!.addTarget(surface)

因为使用了Camera2, 所以摄像头操作操作方式有变化, 还需要适应.

关于Camera2 可以参考: https://developer.android.com/reference/android/hardware/camera2/package-summary?utm_campaign=adp_series_how_to_camera2_031016&utm_source=medium&utm_medium=blog

demo地址:https://github.com/shaopx/opengl_camera2_shader_demo

上一篇下一篇

猜你喜欢

热点阅读