gpt4 book ai didi

Android org.webrtc.VideoRenderer.I420Frame 数组到 PreviewCallback.onPreviewFrame byte[]

转载 作者:太空狗 更新时间:2023-10-29 12:44:26 27 4
gpt4 key购买 nike

我一直希望一些代码会出现在 Internet 上,但一无所获 ;)

WebRTC 传入的 I420Frame 对象似乎有 3 个 yuvPlanes 数组

典型的 Android 相机应用获取 PreviewCallback.onPreviewFrame byte[] 作为单个字节数组。

有人可以帮助我如何将 I420Frames yuvPlanes 转换为单个 byte[] 数组,如 PreviewCallback.onPreviewFrame byte[] YCbCr_420_SP (NV21)?

作为引用,VideoStreamsView.java 有这段代码可以渲染到 OpenGL - 但我只是希望它像相机预览一样 ;) 来自:https://code.google.com/p/libjingle/source/browse/trunk/talk/examples/android/src/org/appspot/apprtc/VideoStreamsView.java?r=286

// Upload the YUV planes from |frame| to |textures|.
private void texImage2D(I420Frame frame, int[] textures) {
for (int i = 0; i < 3; ++i) {
ByteBuffer plane = frame.yuvPlanes[i];
GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[i]);
int w = i == 0 ? frame.width : frame.width / 2;
int h = i == 0 ? frame.height : frame.height / 2;
abortUnless(w == frame.yuvStrides[i], frame.yuvStrides[i] + "!=" + w);
GLES20.glTexImage2D(
GLES20.GL_TEXTURE_2D, 0, GLES20.GL_LUMINANCE, w, h, 0,
GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE, plane);
}
checkNoGLES2Error();
}

谢谢。

最佳答案

好的,给你:

// Copy the bytes out of |src| and into |dst|, ignoring and overwriting
// positon & limit in both buffers.
//** copied from org/webrtc/VideoRenderer.java **//
private static void copyPlane(ByteBuffer src, ByteBuffer dst) {
src.position(0).limit(src.capacity());
dst.put(src);
dst.position(0).limit(dst.capacity());
}

public static android.graphics.YuvImage ConvertTo(org.webrtc.VideoRenderer.I420Frame src, int imageFormat) {
switch (imageFormat) {
default:
return null;

case android.graphics.ImageFormat.YV12: {
byte[] bytes = new byte[src.yuvStrides[0]*src.height +
src.yuvStrides[1]*src.height/2 +
src.yuvStrides[2]*src.height/2];
ByteBuffer tmp = ByteBuffer.wrap(bytes, 0, src.yuvStrides[0]*src.height);
copyPlane(src.yuvPlanes[0], tmp);
tmp = ByteBuffer.wrap(bytes, src.yuvStrides[0]*src.height, src.yuvStrides[2]*src.height/2);
copyPlane(src.yuvPlanes[2], tmp);
tmp = ByteBuffer.wrap(bytes, src.yuvStrides[0]*src.height+src.yuvStrides[2]*src.height/2, src.yuvStrides[1]*src.height/2);
copyPlane(src.yuvPlanes[1], tmp);
int[] strides = src.yuvStrides.clone();
return new YuvImage(bytes, imageFormat, src.width, src.height, strides);
}

case android.graphics.ImageFormat.NV21: {
if (src.yuvStrides[0] != src.width)
return convertLineByLine(src);
if (src.yuvStrides[1] != src.width/2)
return convertLineByLine(src);
if (src.yuvStrides[2] != src.width/2)
return convertLineByLine(src);

byte[] bytes = new byte[src.yuvStrides[0]*src.height +
src.yuvStrides[1]*src.height/2 +
src.yuvStrides[2]*src.height/2];
ByteBuffer tmp = ByteBuffer.wrap(bytes, 0, src.width*src.height);
copyPlane(src.yuvPlanes[0], tmp);

byte[] tmparray = new byte[src.width/2*src.height/2];
tmp = ByteBuffer.wrap(tmparray, 0, src.width/2*src.height/2);

copyPlane(src.yuvPlanes[2], tmp);
for (int row=0; row<src.height/2; row++) {
for (int col=0; col<src.width/2; col++) {
bytes[src.width*src.height + row*src.width + col*2] = tmparray[row*src.width/2 + col];
}
}
copyPlane(src.yuvPlanes[1], tmp);
for (int row=0; row<src.height/2; row++) {
for (int col=0; col<src.width/2; col++) {
bytes[src.width*src.height + row*src.width + col*2+1] = tmparray[row*src.width/2 + col];
}
}
return new YuvImage(bytes, imageFormat, src.width, src.height, null);
}
}
}

public static android.graphics.YuvImage convertLineByLine(org.webrtc.VideoRenderer.I420Frame src) {
byte[] bytes = new byte[src.width*src.height*3/2];
int i=0;
for (int row=0; row<src.height; row++) {
for (int col=0; col<src.width; col++) {
bytes[i++] = src.yuvPlanes[0][col+row*src.yuvStrides[0]];
}
}
for (int row=0; row<src.height/2; row++) {
for (int col=0; col<src.width/2; col++) {
bytes[i++] = src.yuvPlanes[2][col+row*src.yuvStrides[2]];
bytes[i++] = src.yuvPlanes[1][col+row*src.yuvStrides[1]];
}
}
return new YuvImage(bytes, android.graphics.ImageFormat.NV21, src.width, src.height, null);

}
}

这会将 I420Frame 转换为 android.graphics.ImageFormat.NV21 的 Android YuvImage ,你可以compressToJpeg() . ImageFormat.YV12 支持似乎在 SDK 中受到限制。注意 Y 和 V 必须打乱。

为简洁起见,跳过了大多数错误检查。

关于Android org.webrtc.VideoRenderer.I420Frame 数组到 PreviewCallback.onPreviewFrame byte[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20803423/

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