通过黑白(和灰色)alpha信息视频图像为视频添加透明度 [英] Adding transparency to a video from black and white (and gray) alpha information video images

查看:270
本文介绍了通过黑白(和灰色)alpha信息视频图像为视频添加透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android中创建具有透明性(和半透明性)的视频.源视频的图像分为两幅图像,最上面的一个带有颜色信息(看起来像普通视频图像),最下面的一个带有alpha信息(相同的形状,但只有黑白和灰色,黑色表示透明) .

I'd like to create a video with transparency (and semi transparency) in android. The images of the source video are split into two images, the top one with color information (looks like the normal video image), the bottom one with alpha information (the same shapes but only in black and white and gray, black means transparent).

这是iOS的解决方案: https://medium.com/@quentinfasquel/ios-transparent- video-with-coreimage-52cfb2544d54

This is the solution for iOS: https://medium.com/@quentinfasquel/ios-transparent-video-with-coreimage-52cfb2544d54

在Android中达到此目的的最佳方法是什么?

What would be the best way to to this in android?

这可以使用javacv,FFmpegFrameGrabber,FFmpegFrameFilter等解决吗?

Can this be solved with javacv, FFmpegFrameGrabber, FFmpegFrameFilter etc.?

还是使用OpenGL ES和着色器更好?

Or better with OpenGL ES and shaders?

有样品吗?

谢谢!

推荐答案

我们使用完全相同的方法为我们的android应用中的视频添加透明度.我们将OpenGL ES与着色器一起使用,并且可以完美地工作. 您需要做的就是将这些视频渲染为表面纹理(可以使用ExoPlayer轻松完成),然后可以从OpenGL作为常规纹理对其进行访问.对于着色器,您可以从类似这样的内容开始

We use exactly the same approach to add transparency to videos in our android app. We use OpenGL ES with shaders and it works flawlessly. What you need to do is just render those video to surface texture (can be easily done using ExoPlayer) and afterwards it can be accessed from OpenGL as regular texture. For the shader you can start from something like this

private static final String VERTEX_SHADER = "attribute vec4 position;\n" +
        "attribute vec4 inputTextureCoordinate;\n" +
        " \n" +
        "varying vec2 textureCoordinate;\n" +
        "varying vec2 textureCoordinate2;\n" +
        " \n" +
        "void main()\n" +
        "{\n" +
        "    gl_Position = position;\n" +
        "    textureCoordinate.x = inputTextureCoordinate.x;\n" +
        "    textureCoordinate.y = inputTextureCoordinate.y * 0.5;\n" +
        "    textureCoordinate2.x = inputTextureCoordinate.x;\n" +
        "    textureCoordinate2.y = inputTextureCoordinate.y * 0.5 + 0.5;\n" +
        "}";


public static final String FRAGMENT_SHADER = "#extension GL_OES_EGL_image_external : require\n"+
        "varying highp vec2 textureCoordinate;\n"+
        "varying highp vec2 textureCoordinate2;\n"+
        "uniform samplerExternalOES inputImageTexture;\n" +
        "void main() {\n"+
        "    lowp vec4 rgbcolor = texture2D(inputImageTexture, textureCoordinate);\n"+
        "    lowp vec4 alphaValue = texture2D(inputImageTexture, textureCoordinate2);\n"+
        "    gl_FragColor.a = alphaValue.g;\n"+
        "    gl_FragColor.rgb = rgbcolor.rgb * alphaValue.g;\n" +
        "}";

并且不要忘记将其渲染到TextureView中并使用TextureView.setOpaque(false);

and dont forget to render this into a TextureView and enable transparency with TextureView.setOpaque(false);

这篇关于通过黑白(和灰色)alpha信息视频图像为视频添加透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆