gpt4 book ai didi

android - 使用 OpenGL 将位图绘制到 VideoFrame

转载 作者:行者123 更新时间:2023-12-05 00:05:23 27 4
gpt4 key购买 nike

我正在开发一个扩展 Camera2Capturer 的类,以便从相机中获取帧,对其进行修改,然后将其反馈给观察者回调。
我能够获取帧,将其转换为位图,将该位图修改为我想要的,然后使用 OpenGL 将其绘制到一个新的 VideoFrame 中,并返回 capturerObserver.onFrameCaptured(videoFrame);
问题是,我新创建的 videoFrame 被拉伸(stretch)了。我检查时位图是正确的,但绘制的视频帧在侧面拉伸(stretch)。我在不同分辨率的不同设备上尝试过,但问题在任何地方都完全相同。

这是我的 startCapture 方法的代码:

@Override
public void startCapture(int width, int height, int fps) {
super.startCapture(width, height, fps);
this.width = width;
this.height = height;

captureThread = new Thread(() -> {

final int[] textureHandle = new int[1];
GLES20.glGenTextures(1, textureHandle, 0);
Matrix matrix = new Matrix();
matrix.postScale(1f, -1f);
TextureBufferImpl buffer = new TextureBufferImpl(width, height, VideoFrame.TextureBuffer.Type.RGB, textureHandle[0], matrix, surTexture.getHandler(), yuvConverter, null);

// Bind to the texture in OpenGL
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);

try {
while (true) {
surTexture.getHandler().post(() -> {
if (needsToRedrawFrame) {
VideoFrame lastFrameReceived = capturerObs.getLastFrameReceived();

//This is the bitmap I want to draw on the video frame
Bitmap bitmapToDraw = drawingCanvasView.getmBitmap();


//At this point, bitmmapToDraw contains the drawing and the frame captured from the camera overlayed
//Now we need to convert it to fit into the onFrameCaptured callback (requires a VideoFrame).

// Set filtering
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

// Load the bitmap into the bound texture.
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmapToDraw, 0);

bitmapToDraw.recycle();

//The bitmap is drawn on the GPU at this point.

//We transfer it to the VideoFrame
VideoFrame.I420Buffer i420Buf = yuvConverter.convert(buffer);

VideoFrame videoFrame = new VideoFrame(i420Buf, 0, lastFrameReceived.getTimestampNs());

ogCapturerObserver.onFrameCaptured(videoFrame);
needsToRedrawFrame = false;
}
});

Thread.sleep(100);
}
} catch (Exception e) {
LogHelper.logError(CapturerObserverProxy.class, "RMTEST THIS > " + e.getMessage(), e);
}
});
captureThread.start();
}

下面是 bitmapToDraw 的样子:
Bitmap

这是在 SurfaceView 上绘制的 videoFrame 的样子:
VideoFrame

我到底错过了什么?我根本不熟悉OpenGL。

最佳答案

事实证明框架是正确绘制的。但是框架的分辨率与实际绘制的 Surface 不同,因此会进行拉伸(stretch)。
我不得不调整我要绘制的位图的大小(同时保持纵横比!)。如果位图与渲染的 Surface 大小相同,则不会被拉伸(stretch)。

关于android - 使用 OpenGL 将位图绘制到 VideoFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59856140/

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