如何在TextureView Camera流预览上绘制对象并使用对象记录流? [英] How to draw objects on TextureView Camera stream preview and record the stream with objects?

查看:485
本文介绍了如何在TextureView Camera流预览上绘制对象并使用对象记录流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关正在处理的应用程序的帮助.该应用程序必须具有自定义的Camera界面才能录制带音频的视频,并且必须在TextureView画布上实时添加一些对象.旧相机API已弃用,因此我必须使用Camera2 API在TextureView上呈现实时预览.我的目标是在TextureView画布上绘制一些对象,可能是一些text/jpg/gif,而相机流在后台渲染,并能够使用叠加的画布内容和相机提要来录制视频.

I need help with an application I am working on. The application has to have a custom Camera interface to record a video with audio and have to add some objects in realtime on the TextureView canvas. Old Camera API is deprecated, so I have to use Camera2 API to render the live preview on TextureView. My goal is to draw some objects on top of the TextureView Canvas, could be some text/jpg/gif while the camera stream renders in the background and being able to record the video with my overlay canvas content and camera feed.

问题是我可以在透明的叠加视图中绘制自定义内容,但这只是出于用户查看的目的.我已经尝试研究了几天,但无法找到正确的方法来解决我的目的.

Problem is I can draw custom content in an transparent overlay view but that is is just for user's viewing purposes. I have tried researching this for a few days but I am not able to get the right approach to solve my purpose.

我在调用openCamera()方法后尝试了以下代码,但随后我只看到绘制了一个矩形,但没有看到相机预览:

I tried the following code after calling the openCamera() method, but then I just see a rectangle drawn but not the camera preview:

Canvas canvas = mTextureView.lockCanvas();
Paint myPaint = new Paint();
myPaint.setColor(Color.WHITE);
myPaint.setStrokeWidth(10);
canvas.drawRect(100, 100, 300, 300, myPaint);
mTextureView.unlockCanvasAndPost(canvas);

我还尝试了一个自定义的TextureView类,并重写了vonDrawForeground(Canvas canvas)方法,但是它不起作用.

I also tried a custom TextureView class and override thevonDrawForeground(Canvas canvas) method but it doesn't work.

TextureView类中的onDraw()方法是最终的,因此,除了流式传输相机供稿外,我目前无法执行任何操作.

The onDraw() method in TextureView class is final and thus, I am not able to do anything at this point except for just streaming the camera feed.

/**
 * Subclasses of TextureView cannot do their own rendering
 * with the {@link Canvas} object.
 *
 * @param canvas The Canvas to which the View is rendered.
 */
@Override
protected final void onDraw(Canvas canvas) {
}

简而言之,我希望用户能够通过我的相机应用在这里和那里的一些道具来录制视频.

In short, I want user to be able to record video through my camera app with some props here and there.

推荐答案

最有效的选择是将摄像头素材直接传送到GPU中,在其上绘制,然后从那里渲染到显示器和视频直接编码器.

Likely the most performant option is to pipe the camera feed straight into the GPU, draw on top of it there, and from there render to the display and a video encoder directly.

这是许多视频聊天应用程序所做的,例如,对于任何效果.

This is what many video chat apps do, for example, for any effects.

您可以使用SurfaceTexture将camera2连接到EGL,然后可以将预览呈现到四边形上,然后将添加的内容放在顶部.

You can use a SurfaceTexture to connect camera2 to EGL, and then you can render the preview onto a quad, and then your additions on top.

然后,您可以渲染到屏幕缓冲区(例如GLSurfaceView),以及从MediaRecorder/MediaCodec Surface渲染到单独的EGLImage.

Then you can render to a screen buffer (GLSurfaceView for example), and to a separate EGLImage from a MediaRecorder/MediaCodec Surface.

其中涉及很多代码,并且为EGL设置设置了很多脚手架,因此很难指出任何简单的示例.

There's a lot of code involved there, and a lot of scaffolding for EGL setup, so it's hard to point to any simple examples.

这篇关于如何在TextureView Camera流预览上绘制对象并使用对象记录流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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