一. 本文你将了解到:
- 自定义属于自己的SurfaceView,自定义EGLHelper 共享EGLContext, 效仿GLSurfaceView来做。【这么做的目的是为将来 多个Surface 渲染同一个纹理做提前准备】
- 通过Render渲染器 使用VBO FBO 投影矩阵 实现 “离屏渲染” 【离屏渲染主要使用FBO】
- 摄像头旋转不同角度,通过矩阵变换使图像也跟着旋转。
二. 前期准备工作:
1.准备OpenGL 要是用的shader.glsl文件 ,仔细对比Camera用的着色器和离屏渲染用的着色器存在差异。
vertex_shader_camera.glsl 顶点着色器
attribute vec4 v_Position;
attribute vec2 f_Position;
varying vec2 ft_Position;
uniform mat4 u_Matrix;
void main() {
ft_Position = f_Position;
gl_Position = v_Position * u_Matrix;
}
fragment_shader_camera.glsl 片元着色器
#extension GL_OES_EGL_image_external : require
precision mediump float;
varying vec2 ft_Position;
uniform samplerExternalOES sTexture;
void main() {
gl_FragColor=texture2D(sTexture, ft_Position);
}
vertex_shader_fbo.glsl 离屏渲染用到的 顶点着色器
attribute vec4 v_Position;
attribute vec2 f_Position;
varying vec2 ft_Position;
void main() {
ft_Position = f_Position;
gl_Position = v_Position;
}
fragment_shader_fbo.glsl 离屏渲染用到的 片元着色器
precision mediump float;
varying vec2 ft_Position;
uniform sampler2D sTexture;
void main() {
gl_FragColor=texture2D(sTexture, ft_Position);
}
- 模仿Android提供的GLSurfaceView 实现一个我们自己的SurfaceView
首先:自定义一个EGLHelper管理EGL环境 【BaseEglHelper.java】
其次:自定义抽象类BaseSurfaceView 继承SurfaceView 实现 SurfaceHolder.Callback 【BaseSurfaceView它能把EGLContext共享给其他Surface,但是Android_GLSurfaceView不能共享EGLContext】
最后:CameraRender 实现BaseSurfaceView.GLRender 来搞出自己的渲染类【CameraRender.java】
由于代码相对较多,此处不贴出代码,文末将给出代码地址 - 提供一个用于离屏渲染的Render 【CameraFboRender.java】
- 定义相机类,用来搞开始渲染,停止渲染,释放资源,切换摄像头等
三. 实战使用自定义的BaseSurfaceView 实现预览Camera图像
public class CameraView extends BaseSurfaceView {
private AnCamera camera; //自己的摄像头类
private CameraRender cameraRender; //渲染类
private int cameraId = Camera.CameraInfo.CAMERA_FACING_BACK;//后置摄像头
public CameraView(Context context) {
this(context, null);
}
public CameraView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CameraView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
camera = new AnCamera(context);
cameraRender = new CameraRender(context);
setRender(cameraRender);
switchPreAngle(context);
cameraRender.setOnSurfaceTextureListener(new CameraRender.onSurfaceTextureListener() {
@Override
public void onCreateSurfaceTexture(SurfaceTexture surfaceTexture) {
camera.initCamera(surfaceTexture, cameraId);
}
});
}
//停止预览
public void onDestroy() {
if (camera != null) {
camera.setStopCamera();
}
}
//切换屏幕预览角度
public void switchPreAngle(Context context) {
int angle = 0;
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (manager != null) {
angle = manager.getDefaultDisplay().getRotation();
}
cameraRender.resetMatrix();
switch (angle) {
case Surface.ROTATION_0:
Log.d("CameraView", "0");
if (cameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
cameraRender.setAngle(90, 0, 0, 1);
cameraRender.setAngle(180, 1, 0, 0);
} else {
cameraRender.setAngle(90f, 0f, 0f, 1f);
}
break;
case Surface.ROTATION_90:
Log.d("CameraView", "90");
if (cameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
cameraRender.setAngle(180, 0, 0, 1);
cameraRender.setAngle(180, 0, 1, 0);
} else {
cameraRender.setAngle(90f, 0f, 0f, 1f);
}
break;
case Surface.ROTATION_180:
Log.d("CameraView", "180");
if (cameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
cameraRender.setAngle(90f, 0.0f, 0f, 1f);
cameraRender.setAngle(180f, 0.0f, 1f, 0f);
} else {
cameraRender.setAngle(-90, 0f, 0f, 1f);
}
break;
case Surface.ROTATION_270:
Log.d("CameraView", "270");
if (cameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
cameraRender.setAngle(180f, 0.0f, 1f, 0f);
} else {
cameraRender.setAngle(0f, 0f, 0f, 1f);
}
break;
}
}
}
布局文件 main_layout.xml
主页面 MainActivity.java 【记得别忘加权限】
public class MainActivity extends AppCompatActivity {
/*权限请求Code*/
private final static int PERMISSION_REQUEST_CODE = 1234;
/*我们需要使用的权限*/
private String[] permissions = {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO
};
private CameraView cameraView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*SDK>6.0 权限申请*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
checkSelfPermission(permissions[0]) != PackageManager.PERMISSION_GRANTED &&
checkSelfPermission(permissions[1]) != PackageManager.PERMISSION_GRANTED &&
checkSelfPermission(permissions[2]) != PackageManager.PERMISSION_GRANTED &&
checkSelfPermission(permissions[3]) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(permissions, PERMISSION_REQUEST_CODE);
}
cameraView = findViewById(R.id.camera_view);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
cameraView.switchPreAngle(MainActivity.this);
}
@Override
protected void onDestroy() {
super.onDestroy();
cameraView.onDestroy();
}
//权限反馈
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
/*PackageManager.PERMISSION_GRANTED 权限被许可*/
/*PackageManager.PERMISSION_DENIED 没有权限;拒绝访问*/
if (grantResults.length > 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {
showWaringDialog("无法读取内存卡!");
} else if (grantResults.length > 0 && grantResults[1] != PackageManager.PERMISSION_GRANTED) {
showWaringDialog("无法读取内存卡!");
} else if (grantResults.length > 0 && grantResults[2] != PackageManager.PERMISSION_GRANTED) {
showWaringDialog("无法使用相机!");
} else if (grantResults.length > 0 && grantResults[3] != PackageManager.PERMISSION_GRANTED) {
showWaringDialog("无法录制音频!");
}
break;
}
}
private void showWaringDialog(String msg) {
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("警告!")
.setMessage(msg)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 一般情况下如果用户不授权的话,功能是无法运行的,我们暂时做退出处理
finish();
}
}).setPositiveButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 一般情况下如果用户不授权的话,功能是无法运行的,我们暂时做退出处理
finish();
}
}).show();
}
}
代码地址【位于antony_live module中】:https://github.com/YuLingRui/AntonyLLTV.git