From e112ecdeb97d647576bbf7270190a4f147d8f55d Mon Sep 17 00:00:00 2001 From: xufuji456 Date: Wed, 19 Oct 2022 15:12:39 +0800 Subject: [PATCH] Feature: add filter of blur --- .../factory/BeautyFilterFactory.java | 3 ++ .../factory/BeautyFilterType.java | 1 + .../filter/advance/BeautyBlurFilter.java | 33 +++++++++++++++++++ CameraFilter/src/main/res/raw/zoomblur.glsl | 24 ++++++++++++++ .../ffmpeg/activity/CameraFilterActivity.kt | 1 + .../com/frank/ffmpeg/util/FilterTypeUtil.kt | 1 + app/src/main/res/values-en/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 8 files changed, 65 insertions(+) create mode 100644 CameraFilter/src/main/java/com/frank/camerafilter/filter/advance/BeautyBlurFilter.java create mode 100644 CameraFilter/src/main/res/raw/zoomblur.glsl diff --git a/CameraFilter/src/main/java/com/frank/camerafilter/factory/BeautyFilterFactory.java b/CameraFilter/src/main/java/com/frank/camerafilter/factory/BeautyFilterFactory.java index 7fe4973..8a87005 100644 --- a/CameraFilter/src/main/java/com/frank/camerafilter/factory/BeautyFilterFactory.java +++ b/CameraFilter/src/main/java/com/frank/camerafilter/factory/BeautyFilterFactory.java @@ -2,6 +2,7 @@ package com.frank.camerafilter.factory; import android.content.Context; +import com.frank.camerafilter.filter.advance.BeautyBlurFilter; import com.frank.camerafilter.filter.advance.BeautyHueFilter; import com.frank.camerafilter.filter.advance.BeautySketchFilter; import com.frank.camerafilter.filter.BaseFilter; @@ -16,6 +17,8 @@ public class BeautyFilterFactory { switch (type) { case SKETCH: return new BeautySketchFilter(context); + case BLUR: + return new BeautyBlurFilter(context); case HUE: return new BeautyHueFilter(context); case WHITE_BALANCE: diff --git a/CameraFilter/src/main/java/com/frank/camerafilter/factory/BeautyFilterType.java b/CameraFilter/src/main/java/com/frank/camerafilter/factory/BeautyFilterType.java index 6bf3785..8be6626 100644 --- a/CameraFilter/src/main/java/com/frank/camerafilter/factory/BeautyFilterType.java +++ b/CameraFilter/src/main/java/com/frank/camerafilter/factory/BeautyFilterType.java @@ -2,6 +2,7 @@ package com.frank.camerafilter.factory; public enum BeautyFilterType { NONE, + BLUR, HUE, WHITE_BALANCE, SKETCH diff --git a/CameraFilter/src/main/java/com/frank/camerafilter/filter/advance/BeautyBlurFilter.java b/CameraFilter/src/main/java/com/frank/camerafilter/filter/advance/BeautyBlurFilter.java new file mode 100644 index 0000000..ddc531d --- /dev/null +++ b/CameraFilter/src/main/java/com/frank/camerafilter/filter/advance/BeautyBlurFilter.java @@ -0,0 +1,33 @@ +package com.frank.camerafilter.filter.advance; + +import android.content.Context; +import android.opengl.GLES30; + +import com.frank.camerafilter.R; +import com.frank.camerafilter.filter.BaseFilter; +import com.frank.camerafilter.util.OpenGLUtil; + +public class BeautyBlurFilter extends BaseFilter { + + private int blurSize; + + public BeautyBlurFilter(Context context) { + super(NORMAL_VERTEX_SHADER, OpenGLUtil.readShaderFromSource(context, R.raw.zoomblur)); + } + + protected void onInit() { + super.onInit(); + blurSize = GLES30.glGetUniformLocation(getProgramId(), "blurSize"); + } + + protected void onInitialized() { + super.onInitialized(); + setFloat(blurSize, 0.3f); + } + + @Override + public void onInputSizeChanged(int width, int height) { + super.onInputSizeChanged(width, height); + } + +} diff --git a/CameraFilter/src/main/res/raw/zoomblur.glsl b/CameraFilter/src/main/res/raw/zoomblur.glsl new file mode 100644 index 0000000..c9bb348 --- /dev/null +++ b/CameraFilter/src/main/res/raw/zoomblur.glsl @@ -0,0 +1,24 @@ +varying highp vec2 textureCoordinate; + +uniform sampler2D inputImageTexture; + +uniform highp vec2 blurCenter; +uniform highp float blurSize; + +void main() +{ + // TODO: Do a more intelligent scaling based on resolution here + highp vec2 samplingOffset = 1.0/100.0 * (blurCenter - textureCoordinate) * blurSize; + + lowp vec4 fragmentColor = texture2D(inputImageTexture, textureCoordinate) * 0.18; + fragmentColor += texture2D(inputImageTexture, textureCoordinate + samplingOffset) * 0.15; + fragmentColor += texture2D(inputImageTexture, textureCoordinate + (2.0 * samplingOffset)) * 0.12; + fragmentColor += texture2D(inputImageTexture, textureCoordinate + (3.0 * samplingOffset)) * 0.09; + fragmentColor += texture2D(inputImageTexture, textureCoordinate + (4.0 * samplingOffset)) * 0.05; + fragmentColor += texture2D(inputImageTexture, textureCoordinate - samplingOffset) * 0.15; + fragmentColor += texture2D(inputImageTexture, textureCoordinate - (2.0 * samplingOffset)) * 0.12; + fragmentColor += texture2D(inputImageTexture, textureCoordinate - (3.0 * samplingOffset)) * 0.09; + fragmentColor += texture2D(inputImageTexture, textureCoordinate - (4.0 * samplingOffset)) * 0.05; + + gl_FragColor = fragmentColor; +} \ No newline at end of file diff --git a/app/src/main/java/com/frank/ffmpeg/activity/CameraFilterActivity.kt b/app/src/main/java/com/frank/ffmpeg/activity/CameraFilterActivity.kt index 8110316..b3767d2 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/CameraFilterActivity.kt +++ b/app/src/main/java/com/frank/ffmpeg/activity/CameraFilterActivity.kt @@ -15,6 +15,7 @@ class CameraFilterActivity : BaseActivity() { private val filterType: Array = arrayOf( BeautyFilterType.NONE, + BeautyFilterType.BLUR, BeautyFilterType.HUE, BeautyFilterType.WHITE_BALANCE, BeautyFilterType.SKETCH diff --git a/app/src/main/java/com/frank/ffmpeg/util/FilterTypeUtil.kt b/app/src/main/java/com/frank/ffmpeg/util/FilterTypeUtil.kt index d5317f3..9133e29 100644 --- a/app/src/main/java/com/frank/ffmpeg/util/FilterTypeUtil.kt +++ b/app/src/main/java/com/frank/ffmpeg/util/FilterTypeUtil.kt @@ -13,6 +13,7 @@ object FilterTypeUtil { fun filterTypeToNameId(type: BeautyFilterType): Int { return when (type) { BeautyFilterType.NONE -> R.string.camera_filter_none + BeautyFilterType.BLUR -> R.string.camera_filter_blur BeautyFilterType.HUE -> R.string.camera_filter_hue BeautyFilterType.WHITE_BALANCE -> R.string.camera_filter_balance BeautyFilterType.SKETCH -> R.string.camera_filter_sketch diff --git a/app/src/main/res/values-en/strings.xml b/app/src/main/res/values-en/strings.xml index d31c30f..48af113 100644 --- a/app/src/main/res/values-en/strings.xml +++ b/app/src/main/res/values-en/strings.xml @@ -116,5 +116,6 @@ Hue White balance Sketch + Blur diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3b7ca55..a5a07c5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -116,5 +116,6 @@ Hue 白平衡 素描 + 模糊