gpt4 book ai didi

java - 如何在 android 中录制视频的同时绘制视频,并保存视频和绘图?

转载 作者:太空狗 更新时间:2023-10-29 14:46:01 33 4
gpt4 key购买 nike

我正在尝试开发一款应用程序,允许我在录制视频时在其上绘图,然后将录制内容和视频保存在一个 mp4 文件中以备后用。此外,我想使用 camera2 库,尤其是我需要我的应用程序在高于 API 21 的设备上运行,而且我总是避免使用已弃用的库。

我尝试了很多方法来做到这一点,包括 FFmpeg,我在其中放置了 TextureView.getBitmap()(来自相机)的叠加层和从 Canvas 获取的位图。它起作用了,但由于它是一个慢速函数,视频无法捕捉到足够的帧(甚至不到 25 fps),而且它运行得如此之快。我也希望包含音频。

我考虑过 MediaProjection 库,但我不确定它是否可以仅在其 VirtualDisplay 中捕获包含相机和绘图的布局,因为应用程序用户可能还会在视频中添加文本,而我不会想要键盘出现。

请帮忙,已经进行了一周的研究,但我没有发现任何适合我的东西。

附言:如果在用户按下“停止录制”按钮后包含一点处理时间,我没有问题。

已编辑:

现在在 Eddy 的回答之后,我正在使用 shadercam 应用程序在相机表面上绘制,因为该应用程序会进行视频渲染,而要做的解决方法是将我的 Canvas 渲染成位图,然后再渲染成 GL 纹理,但是我无法成功完成。我需要你们的帮助,我需要完成应用程序 :S

我正在使用 shadercam 库 ( https://github.com/googlecreativelab/shadercam ),我用以下代码替换了“ExampleRenderer”文件:

public class WriteDrawRenderer extends CameraRenderer
{
private float offsetR = 1f;
private float offsetG = 1f;
private float offsetB = 1f;

private float touchX = 1000000000;
private float touchY = 1000000000;

private Bitmap textBitmap;

private int textureId;

private boolean isFirstTime = true;

//creates a new canvas that will draw into a bitmap instead of rendering into the screen
private Canvas bitmapCanvas;

/**
* By not modifying anything, our default shaders will be used in the assets folder of shadercam.
*
* Base all shaders off those, since there are some default uniforms/textures that will
* be passed every time for the camera coordinates and texture coordinates
*/
public WriteDrawRenderer(Context context, SurfaceTexture previewSurface, int width, int height)
{
super(context, previewSurface, width, height, "touchcolor.frag.glsl", "touchcolor.vert.glsl");
//other setup if need be done here


}

/**
* we override {@link #setUniformsAndAttribs()} and make sure to call the super so we can add
* our own uniforms to our shaders here. CameraRenderer handles the rest for us automatically
*/
@Override
protected void setUniformsAndAttribs()
{
super.setUniformsAndAttribs();

int offsetRLoc = GLES20.glGetUniformLocation(mCameraShaderProgram, "offsetR");
int offsetGLoc = GLES20.glGetUniformLocation(mCameraShaderProgram, "offsetG");
int offsetBLoc = GLES20.glGetUniformLocation(mCameraShaderProgram, "offsetB");

GLES20.glUniform1f(offsetRLoc, offsetR);
GLES20.glUniform1f(offsetGLoc, offsetG);
GLES20.glUniform1f(offsetBLoc, offsetB);

if (touchX < 1000000000 && touchY < 1000000000)
{
//creates a Paint object
Paint yellowPaint = new Paint();
//makes it yellow
yellowPaint.setColor(Color.YELLOW);
//sets the anti-aliasing for texts
yellowPaint.setAntiAlias(true);
yellowPaint.setTextSize(70);

if (isFirstTime)
{
textBitmap = Bitmap.createBitmap(mSurfaceWidth, mSurfaceHeight, Bitmap.Config.ARGB_8888);
bitmapCanvas = new Canvas(textBitmap);
}

bitmapCanvas.drawText("Test Text", touchX, touchY, yellowPaint);

if (isFirstTime)
{
textureId = addTexture(textBitmap, "textBitmap");
isFirstTime = false;
}
else
{
updateTexture(textureId, textBitmap);
}

touchX = 1000000000;
touchY = 1000000000;
}
}

/**
* take touch points on that textureview and turn them into multipliers for the color channels
* of our shader, simple, yet effective way to illustrate how easy it is to integrate app
* interaction into our glsl shaders
* @param rawX raw x on screen
* @param rawY raw y on screen
*/
public void setTouchPoint(float rawX, float rawY)
{
this.touchX = rawX;
this.touchY = rawY;
}
}

请大家帮忙,已经一个月了,我仍然坚持使用同一个应用程序 :( 并且对 opengl 一无所知。两周后,我正在尝试将这个项目用于我的应用程序,但没有任何渲染视频。

提前致谢!

最佳答案

这是一个应该可行的粗略大纲,但需要大量工作:

  1. 设置一个 android.media.MediaRecorder 来录制视频和音频
  2. 从 MediaRecorder 获取一个 Surface 并从中设置一个 EGLImage ( https://developer.android.com/reference/android/opengl/EGL14.html#eglCreateWindowSurface(android.opengl.EGLDisplay , android.opengl.EGLConfig, java.lang.Object, int[], int) );您将需要一个完整的 OpenGL 上下文并为此进行设置。然后您需要将该 EGLImage 设置为您的渲染目标。
  3. 在该 GL 上下文中创建 SurfaceTexture。
  4. 配置相机以将数据发送到该 SurfaceTexture
  5. 启动媒体记录器
  6. 在从相机接收到的每一帧上,将用户绘制的图形转换为 GL 纹理,并将相机纹理和用户绘制的图形合成。
  7. 最后,调用glSwapBuffers将合成帧发送到录像机

关于java - 如何在 android 中录制视频的同时绘制视频,并保存视频和绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40191950/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com