SurfaceTexture源码分析

本文详细分析了SurfaceTexture的源码,包括其作为OpenGL ES纹理捕获图像流的功能,重点介绍了updateTexImage()和getTransformMatrix()两个关键方法的作用。SurfaceTexture用于处理图像流的二次处理,如Camera滤镜和特效。它使用GL_TEXTURE_EXTERNAL_OES纹理类型,并提供了OnFrameAvailableListener接口用于监听新帧数据。
摘要由CSDN通过智能技术生成
概述

SurfaceTexture,是Android3.0以后引入的,它对图像流的处理并不直接显示,而是转换为GL外部纹理,因此可用于图像流数据的二次处理,比如Camera滤镜、特效等。

分析源码
SurfaceTexture

java.lang.Object
↳ android.graphics.SurfaceTexture

public class SurfaceTexture {
   

 }
API 注释
  • Captures frames from an image stream as an OpenGL ES texture. 从图像流中捕获帧数据用作OpenGL ES的纹理。The image stream may come from either camera preview or video decode 其中这个图像流数据也可以是相机预览数据也可以是视频解码数据。
  • A SurfaceTexture may be used in place of a SurfaceHolder when specifying the output destination of a Camera or MediaPlayer object. 当输出目标指定为Camera或者MediaPlayer对象时候,可以使用SurfaceTexture来代替SurfaceHolder
  • Doing so will cause all the frames from the image stream to be sent to the SurfaceTexture object rather than to the device’s display. 如果这样做的话,image stream将把所有帧对象都传给了SurfaceTexture对象,而不是显示在设备上。
两个重要的方法
SurfaceTexture 是 Android 中提供的一个类,用于将实时相机预览数据渲染到一个 OpenGL ES 纹理上,以便进行后续的图像处理。实现相机预览的一般流程如下: 1. 打开相机,设置相机参数; 2. 创建一个 SurfaceTexture 对象,并将其设置为相机预览回调中的 Surface; 3. 在 SurfaceTexture 的 onFrameAvailable 回调中,获取最新的相机预览帧数据,并将其渲染到一个 OpenGL ES 纹理上; 4. 在 OpenGL ES 纹理上进行后续的图像处理。 以下是一个简单的示例代码: ```java // 打开相机 Camera camera = Camera.open(); // 设置相机参数 Camera.Parameters parameters = camera.getParameters(); parameters.setPreviewSize(width, height); camera.setParameters(parameters); // 创建一个 SurfaceTexture 对象 SurfaceTexture surfaceTexture = new SurfaceTexture(0); surfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() { @Override public void onFrameAvailable(SurfaceTexture surfaceTexture) { // 获取最新的相机预览帧数据,并将其渲染到一个 OpenGL ES 纹理上 surfaceTexture.updateTexImage(); // 在 OpenGL ES 纹理上进行后续的图像处理 // ... } }); // 将 SurfaceTexture 设置为相机预览回调中的 Surface camera.setPreviewTexture(surfaceTexture); // 开始相机预览 camera.startPreview(); ``` 需要注意的是,在渲染相机预览数据之前,需要先创建一个 OpenGL ES 纹理,可以使用 GLES20 类中的 glGenTextures 方法来生成一个纹理 ID。在渲染时,可以使用 SurfaceTexture 的 getTransformMatrix 方法获取纹理坐标变换矩阵,以便正确地将相机预览数据渲染到纹理上。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值