相机之使用OpenGL预览
相机之使用OpenGL拍照
相机之使用OpenGL录像
相机之为录像添加音频
相机之大眼
相机之贴纸
相机之口红
片段着色器
precision mediump float;
uniform sampler2D vTexture;
varying vec2 v_TextureCoord;
// 图像宽度
uniform float width;
// 图像高度
uniform float height;
// 磨皮程度,取值1/3~1
uniform float smoothRatio;
// 用于存储采样点四周的点坐标
vec2 blurCoord[20];
// 用于与采样点相乘,计算灰度图
const vec3 W = vec3(0.299, 0.587, 0.114);
// 强光处理,传入颜色分量
float hardLight(float colorChannel){
if (colorChannel <= 0.5){
colorChannel = colorChannel * colorChannel * 2.0;
} else {
colorChannel = 1.0 - ((1.0 - colorChannel)*(1.0 - colorChannel) * 2.0);
}
return colorChannel;
}
void main() {
vec2 offset = vec2(1.0 / width, 1.0 / height);
// 获取要采样的点的四周的点
blurCoord[0] = v_TextureCoord.xy + offset * vec2(0.0, -10.0);
blurCoord[1] = v_TextureCoord.xy + offset * vec2(0.0, 10.0);
blurCoord[2] = v_TextureCoord.xy + offset * vec2(-10.0, 0.0);
blurCoord[3] = v_TextureCoord.xy + offset * vec2(10.0, 0.0);
blurCoord[4] = v_TextureCoord.xy + offset * vec2(5.0, -8.0);
blurCoord[5] = v_TextureCoord.xy + offset * vec2(5.0, 8.0);
blurCoord[6] = v_TextureCoord.xy + offset * vec2(-5.0, 8.0);
blurCoord[7] = v_TextureCoord.xy + offset * vec2(-5.0, -8.0);
blurCoord[8] = v_TextureCoord.xy + offset * vec2(8.0, -5.0);
blurCoord[9] = v_TextureCoord.xy + offset * vec2(8.0, 5.0);
blurCoord[10] = v_TextureCoord.xy + offset * vec2(-8.0, 5.0);
blurCoord[11] = v_TextureCoord.xy + offset * vec2(-8.0, -5.0);
blurCoord[12] = v_TextureCoord.xy + offset * vec2(0.0, -6.0);
blurCoord[13] = v_TextureCoord.xy + offset * vec2(0.0, 6.0);
blurCoord[14] = v_TextureCoord.xy + offset * vec2(6.0, 0.0);
blurCoord[15] = v_TextureCoord.xy + offset * vec2(-6.0, 0.0);
blurCoord[16] = v_TextureCoord.xy + offset * vec2(-4.0, -4.0);
blurCoord[17] = v_TextureCoord.xy + offset * vec2(-4.0, 4.0);
blurCoord[18] = v_TextureCoord.xy + offset * vec2(4.0, -4.0);
blurCoord[19] = v_TextureCoord.xy + offset * vec2(4.0, 4.0);
// 当前采样点的颜色
vec3 currentColor = texture2D(vTexture, v_TextureCoord).rgb;
// 计算当前采样点和其四周的点的颜色的G分量的加权平均值,当前采样点的比重最大,因此乘以20.0
float sampleColor = currentColor.g * 20.0;
for (int i = 0; i < 20; i++){
sampleColor += texture2D(vTexture, blurCoord[i].xy).g;
}
// G分量的加权平均值,也就是模糊后的G分量值
sampleColor = sampleColor / 40.0;
// 高反差保留 = 原图 - 高斯模糊图,如果直接使用模糊后的颜色值,会将整个图像模糊掉,眉眼口鼻都会模糊,因此要保留高反差的地方
float highPassG = currentColor.g - sampleColor + 0.5;
// 强光处理,使反差值扩大,可以使得噪声更加突出
for (int i = 0; i < 5; i++) {
highPassG = hardLight(highPassG);
}
// 当前采样点的灰度值
float luminance = dot(currentColor, W);
// 将灰度值作为阈值,用来排除非皮肤部分,第二个参数可调(1/3~1),值越小,alpha越大,磨皮效果越明显,修改该值可作为美颜程度
float alpha = pow(luminance, smoothRatio);
// 根据灰度值计算,将原图与结果图合成
vec3 smoothColor = currentColor + (currentColor - vec3(highPassG)) * alpha * 0.1;
vec3 resultColor = mix(smoothColor.rgb, max(smoothColor, currentColor), alpha);
gl_FragColor = vec4(smoothColor, 1.0);
}
着色器程序
class BeautyFilter(context: Context, width: Int, height: Int) :
FboFilter(context, R.raw.base_vertex, R.raw.beauty_frag, width, height) {
private var smoothRatio: Float = 0f
private val widthLocation = GLES20.glGetUniformLocation(mProgram, "width")
private val heightLocation = GLES20.glGetUniformLocation(mProgram, "height")
private val smoothRatioLocation = GLES20.glGetUniformLocation(mProgram, "smoothRatio")
override fun onDrawInFBO(textureId: Int) {
// 先将textureId的图像画到这一个FBO中
//激活纹理单元0
GLES20.glActiveTexture(GLES20.GL_TEXTURE0)
//将textureId纹理绑定到纹理单元0
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId)
//将纹理单元0传给vTexture,告诉vTexture采样器从纹理单元0读取数据
GLES20.glUniform1i(vTexture, 0)
//在textureId纹理上画出图像
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4)
// 传入宽高
GLES20.glUniform1f(widthLocation, width.toFloat())
GLES20.glUniform1f(heightLocation, height.toFloat())
// 传入磨皮程度
GLES20.glUniform1f(smoothRatioLocation, smoothRatio)
//解除绑定
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0)
}
/**
* 设置美颜磨皮程度
* @param ratio Float
*/
fun setSmoothRatio(ratio: Float) {
// 将ratio转换为[1,1/3]范围内,越小磨皮效果越大
val correctRatio = ratio / (-3f / 2f) + 1
Log.d("TAG", "setSmoothRatio: correctRatio=${correctRatio}")
this.smoothRatio = correctRatio
}
}