Android:使用视频动态模糊表面

Mic*_*ael 6 android surfaceview exoplayer

我正在构建一个Android应用程序,其中ExoPlayer在SurfaceView的表面上播放视频,我正在调查是否可以动态模糊播放视频.

由于SurfaceView的表面部分未出现在位图中,因此模糊技术涉及首先生成视图的位图以进行模糊将无法工作.

表面和视图过去在旧版Android中具有内置模糊效果(例如Surface.FX_SURFACE_BLUR),但似乎在较新的API中已被弃用.

任何人都可以分享一些表面如何动态模糊的见解吗?谢谢.

Mic*_*ael 7

StackOverflow上有很多问题,需要做一些小小的工作.我将介绍我使用的方法,希望它对某人有用.

如果这是视频帧的静态模糊,则在a中播放视频就足够了TextureView,使用该.getBitmap()函数并使用Renderscript等工具模糊生成的Bitmap.但是,.getBitmap()在主UI线程上执行,因此滞后于它试图复制的帧的视频.

要为每个帧执行模糊,最好的方法似乎是将GLSurfaceView与自定义渲染器一起使用.我使用VidEffects中提供的代码从这个答案中指出了一个很好的起点.

具有大半径的模糊可能是非常计算密集的.这就是为什么我首先使用两个单独的片段着色器(一个用于水平模糊,一个用于垂直模糊结果)来处理模糊.实际上我最终只使用一个片段着色器来应用7x7高斯内核.要记住,如果你的一个非常重要的事情GLSurfaceView是大是调用setFixedSize()GLSurfaceViewSurfaceHolder以使其分辨率小于屏幕的下部.结果看起来并非像素化,因为它无论如何都是模糊的,但性能提升非常显着.

我制作的模糊设法在大多数设备上播放24fps,并setFixedSize()指定其分辨率为100x70.


Kie*_*all 6

如果有人希望单次通过片段着色器来完成圆,则...以下代码实现了VidEffectsShaderInterface可用代码中的定义。我从ShaderToy.com上的此示例改编而成

public class BlurEffect2 implements ShaderInterface {

    private final int mMaskSize;
    private final int mWidth;
    private final int mHeight;

    public BlurEffect2(int maskSize, int width, int height) {
        mMaskSize = maskSize;
        mWidth = width;
        mHeight = height;
    }

    @Override
    public String getShader(GLSurfaceView mGlSurfaceView) {

        float hStep = 1.0f / mWidth;
        float vStep = 1.0f / mHeight;

        return  "#extension GL_OES_EGL_image_external : require\n" +          
                "precision mediump float;\n" +
                //"in" attributes from our vertex shader
                "varying vec2 vTextureCoord;\n" +

                //declare uniforms
                "uniform samplerExternalOES sTexture;\n" +

                "float normpdf(in float x, in float sigma) {\n" +
                "    return 0.39894 * exp(-0.5 * x * x / (sigma * sigma)) / sigma;\n" +
                "}\n" +


                "void main() {\n" +
                "    vec3 c = texture2D(sTexture, vTextureCoord).rgb;\n" +

                //declare stuff
                "    const int mSize = " + mMaskSize + ";\n" +
                "    const int kSize = (mSize - 1) / 2;\n" +
                "    float kernel[ mSize];\n" +
                "    vec3 final_colour = vec3(0.0);\n" +

                //create the 1-D kernel
                "    float sigma = 7.0;\n" +
                "    float Z = 0.0;\n" +
                "    for (int j = 0; j <= kSize; ++j) {\n" +
                "        kernel[kSize + j] = kernel[kSize - j] = normpdf(float(j), sigma);\n" +
                "    }\n" +

                //get the normalization factor (as the gaussian has been clamped)
                "    for (int j = 0; j < mSize; ++j) {\n" +
                "        Z += kernel[j];\n" +
                "    }\n" +

                //read out the texels
                "    for (int i = -kSize; i <= kSize; ++i) {\n" +
                "        for (int j = -kSize; j <= kSize; ++j) {\n" +
                "            final_colour += kernel[kSize + j] * kernel[kSize + i] * texture2D(sTexture, (vTextureCoord.xy + vec2(float(i)*" + hStep + ", float(j)*" + vStep + "))).rgb;\n" +
                "        }\n" +
                "    }\n" +

                "    gl_FragColor = vec4(final_colour / (Z * Z), 1.0);\n" +
                "}";
    }

}
Run Code Online (Sandbox Code Playgroud)

就像迈克尔在上面指出的那样,您可以通过设置SurfaceViewusing 的大小来提高性能setFixedSize

@BindView(R.id.video_snap)
VideoSurfaceView mVideoView;

@Override
public void showVideo(String cachedPath) {
    mImageView.setVisibility(View.GONE);
    mVideoView.setVisibility(View.VISIBLE);

    //Get width and height of the video
    final MediaMetadataRetriever mRetriever = new MediaMetadataRetriever();
    mRetriever.setDataSource(cachedPath);
    int width = Integer.parseInt(mRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
    int height = Integer.parseInt(mRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));

    //divide the width and height by 10
    width /= 10;
    height /= 10;

    //set the size of the surface to play on to 1/10 the width and height
    mVideoView.getHolder().setFixedSize(width, height);

    //Set up the media player
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setLooping(true);

    try {
        mMediaPlayer.setDataSource(cachedPath);
    } catch (Exception e) {
        Timber.e(e, e.getMessage());
    }

    //init and start the video player with the mask size set to 17
    mVideoView.init(mMediaPlayer, new BlurEffect2(17, width, height));
    mVideoView.onResume();
}
Run Code Online (Sandbox Code Playgroud)