在上一章MediaPipe框架解析(三):详解“Hello World! in C++ example”中我们分析了c++的helloworld源码,从而了解了mediapipe框架的基本运行方式。本章将继续分析安卓端的helloworld,相对c++ helloworld,安卓项目设计到的内容更多了,分析起来会更复杂一点。我会先从其产物分析,再到源码分析等步骤来逐步加深了解。
helloworld产物分析
上一章我们生成了helloworld.apk在bazal-bin/mediapipe/examples/android/src/java/com/google/mediapipe/apps/basic下,在将helloworld.apk重命名为helloworld.zip后解压到当前路径下,会得到如下结构

我比较关心的有如下文件,但目前还不清楚具体用途:
- assets/mobile_gpu.binarypb
- lib/arm64-v8a/libmediapipe_jni.so
- lib/arm64-v8a/libopencv_java4.so
后续应该会从帧数据的流向并结合这几个关键文件的用途来做源码分析。
源码分析
相机打开
先根据AndroidManifest.xml找到入口MainActivity,在MainActivity的onResume中可以看到应该是使用了CameraXPreviewHelper来打开和预览相机,相机数据通过SurfaceTexture输出。SurfaceTexture经过ExternalTextureConverter的renderNext方法后就将OES纹理转为了Texture2D的纹理,并将该代表相机帧的AppTextureFrame返回到了com.google.mediapipe.components.FrameProcessor.onNewFrame方法中,并在onNewFrame方法中对相机帧做了边缘处理等操作。

适配层
从“相机打开”中的分析我们可以知道相机数据帧在FrameProcessor.onNewFrame中被MediaGraph框架所捕获。通过java中的FrameProcessor/Graph/AndroidPacketCreator等类实现了将相机帧适配为MediaPipe框架支持的帧数据,再将框架输出的数据又转换为了能直接上屏的帧。
FrameProcessor
FrameProcessor是一个比较重要的核心管理类,这个类中有几个关键角色:Graph/InputStream/OutputStream/Packet/Consumer。
FrameProcessor的构造方法如下:
/**
* Constructor for video input/output.
*
* @param context an Android {@link Context}.
* @param parentNativeContext a native handle to a GL context. The GL context(s) used by the
* calculators in the graph will join the parent context's sharegroup, so that textures
* generated by the calculators are availabl

最低0.47元/天 解锁文章
2976

被折叠的 条评论
为什么被折叠?



