# 基于exoplayer播放器的高斯模糊视频滤镜

11 篇文章 0 订阅

exoPlayer播放器自带滤镜，所以用这个播放器来做。

 radius 偏移量 blurX X轴方向偏移次数 blurY Y轴方向偏移次数 trans 亮度

原理大概是：

1.关键代码，滤镜

#GaussianBlurEffect
#extension GL_OES_EGL_image_external : require
precision mediump float;
varying vec2 vTextureCoord;
uniform samplerExternalOES sTexture;
const float resolution=1024.0;
vec2 dir = vec2(1.0,1.0);
void main() {
vec4 sum = vec4(0.0);
vec2 tc = vTextureCoord;
float hstep = dir.x;
float vstep = dir.y;
int x = blurX;
int y = blurY;
for(int i = x;i > 0;i--){
for(int j = y; j > 0; j--){
sum = texture2D(sTexture, vec2(tc.x + float(i)*blur*hstep, tc.y + float(j)*blur*vstep)) *trans;
sum = texture2D(sTexture, vec2(tc.x - float(i)*blur*hstep, tc.y + float(j)*blur*vstep)) *trans;
sum = texture2D(sTexture, vec2(tc.x - float(i)*blur*hstep, tc.y - float(j)*blur*vstep)) *trans;
sum = texture2D(sTexture, vec2(tc.x + float(i)*blur*hstep, tc.y - float(j)*blur*vstep)) *trans;
}
}
vec4 cc= texture2D(sTexture,vTextureCoord );

gl_FragColor =vec4(sum.rgb, cc.a);
}

#NoEffect
#extension GL_OES_EGL_image_external : require
precision mediump float;
varying vec2 vTextureCoord;
uniform samplerExternalOES sTexture;
void main() {
gl_FragColor = texture2D(sTexture, vTextureCoord);
}

GLES20.glCreateProgram();//创建
GLES20.glAttachShader(program, pixelShader);

exoPlayer是用textureView来显示视频，所以 应该在xml文件中，加上textureView. (ImageView是一个开始播放的按钮)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hyq.hm.openglexo.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextureView
android:id="@+id/texture_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/video_player"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:src="@drawable/ic_play"
android:onClick="playVideo"
/>
</RelativeLayout>

</android.support.constraint.ConstraintLayout>

public class MainActivity extends AppCompatActivity {

private View videoPlayerView;//播放器 播放按钮View

private TextureView textureView;//纹理 播放视频用

private SimpleExoPlayer player;//播放器

private Handler mainHandler;

private boolean isPlayer = false;

private EGLUtils mEglUtils;//EGL工具类
private GLFramebuffer mFramebuffer;//滤镜代码，以及绑定和绘制的方法

private String uri = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

videoPlayerView = findViewById(R.id.video_player);

mainHandler = new Handler();

textureView = findViewById(R.id.texture_view);
textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
//初始化SurfaceTexture, 准备就绪
init(new Surface(surface),uri);
}

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
//SurfaceTexture改变大小时调用
}

@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
//SurfaceTexture摧毁时调用
return false;
}

@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
//SurfaceTexture更新时调用
}
});

}
public void init(Surface surface,String uri){
//        Uri url = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath() +"/HMSDK/video/1531383835814.mp4");//本地指定视频
Uri url = Uri.parse(uri);//网络视频地址
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();

TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);

DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, "ExoPlayerTime"), bandwidthMeter);

MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(url, mainHandler,null);

@Override
public Surface onSurface(Surface surface,int width,int height) {
mEglUtils = new EGLUtils();
mEglUtils.initEGL(surface);
mFramebuffer = new GLFramebuffer();//滤镜对象
mFramebuffer.initFramebuffer(textureView.getWidth(),
textureView.getHeight(),
width,
height);

return new Surface(mFramebuffer.getSurfaceTexture());
}

@Override
public void onVideoTimeChanged(long time) {//每一帧调用一次
mFramebuffer.drawFrame();

mEglUtils.swap();
}

@Override
public void onRelease() {
if(mEglUtils != null){
mEglUtils.release();
}

}
});
player.setVideoSurface(surface);
player.prepare(videoSource);
}

public void playVideo(View view){
if(player.getContentPosition() >= player.getDuration()){
player.seekTo(0);
}
videoPlayerView.setVisibility(View.INVISIBLE);
isPlayEnd();
}
private Handler seekBarHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
isPlayEnd();
}else{
if(!isPlayer){
videoPlayerView.setVisibility(View.VISIBLE);
}
}
}
};
private void isPlayEnd(){
seekBarHandler.removeMessages(100);
Message message = seekBarHandler.obtainMessage();
message.what = 100;
seekBarHandler.sendMessageDelayed(message,100);
}

@Override
protected void onResume() {
super.onResume();
if(player != null){
if(isPlayer){
isPlayer = false;
isPlayEnd();
}
}
}

@Override
protected void onPause() {
super.onPause();
if(player != null){
isPlayer = true;
}

}
}

@Override
protected void onDestroy() {
super.onDestroy();
if(player != null){
player.stop();
player.release();
player = null;
}
}
}


xml文件中那个src

<vector android:height="24dp" android:viewportHeight="1024.0"
android:viewportWidth="1024.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#d81e06" android:pathData="M512,0C230.4,0 0,230.4 0,512c0,281.6 230.4,512 512,512 117.8,0 227.8,-38.4 320,-110.1 10.2,-7.7 12.8,-23 5.1,-35.8 -7.7,-10.2 -23,-12.8 -35.8,-5.1C719.4,939.5 617,972.8 512,972.8 256,972.8 51.2,768 51.2,512 51.2,256 256,51.2 512,51.2 768,51.2 972.8,256 972.8,512c0,87 -25.6,171.5 -69.1,243.2 -7.7,12.8 -2.6,28.2 7.7,33.3 12.8,7.7 28.2,2.6 33.3,-7.7 51.2,-79.4 76.8,-174.1 76.8,-271.4C1024,230.4 793.6,0 512,0z"/>
<path android:fillColor="#d81e06" android:pathData="M714.2,458.2c-17.9,-15.4 -245.8,-222.7 -245.8,-222.7 -10.2,-10.2 -25.6,-7.7 -35.8,2.6 -5.1,5.1 -7.7,12.8 -7.7,17.9 0,0 0,0 0,0 0,0 0,499.2 0,512 0,15.4 10.2,25.6 25.6,25.6 5.1,0 12.8,-2.6 15.4,-7.7 2.6,-2.6 217.6,-186.9 240.6,-207.4 23,-20.5 33.3,-38.4 33.3,-64C742.4,491.5 732.2,473.6 714.2,458.2zM681,535c-7.7,5.1 -204.8,176.6 -204.8,176.6l0,-399.4c0,0 186.9,166.4 202.2,181.8C696.3,512 698.9,519.7 681,535z"/>
</vector>


<uses-permission android:name="android.permission.INTERNET" />

• 0
点赞
• 0
评论
• 1
收藏
• 一键三连
• 扫一扫，分享海报

08-21 911
11-15 207
04-19 4919
03-29 66
08-09 1669
04-18 2128
01-23 1万+
07-25 1739
09-12 3588
07-23 4272