Android 自定义byteArray数据到WebRTC videoTrack
我需要使用android将特定裁剪(面部)视频发送到视频频道。我可以操纵WebRTC的类来裁剪面部。现在我正在将其设置为ImageView。Android 自定义byteArray数据到WebRTC videoTrack,android,android-camera,webrtc,android-vision,apprtcdemo,Android,Android Camera,Webrtc,Android Vision,Apprtcdemo,我需要使用android将特定裁剪(面部)视频发送到视频频道。我可以操纵WebRTC的类来裁剪面部。现在我正在将其设置为ImageView。 Camera1Session.java的listenForBytebufferFrames() private void listenForBytebufferFrames() { this.camera.setPreviewCallbackWithBuffer(new PreviewCallback() { public void
Camera1Session.java的listenForBytebufferFrames()
private void listenForBytebufferFrames() {
this.camera.setPreviewCallbackWithBuffer(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera callbackCamera) {
Camera1Session.this.checkIsOnCameraThread();
if(callbackCamera != Camera1Session.this.camera) {
Logging.e("Camera1Session", "Callback from a different camera. This should never happen.");
} else if(Camera1Session.this.state != Camera1Session.SessionState.RUNNING) {
Logging.d("Camera1Session", "Bytebuffer frame captured but camera is no longer running.");
} else {
mFrameProcessor.setNextFrame(data, callbackCamera);
long captureTimeNs = TimeUnit.MILLISECONDS.toNanos(SystemClock.elapsedRealtime());
if(!Camera1Session.this.firstFrameReported) {
int startTimeMs = (int)TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - Camera1Session.this.constructionTimeNs);
Camera1Session.camera1StartTimeMsHistogram.addSample(startTimeMs);
Camera1Session.this.firstFrameReported = true;
}
ByteBuffer byteBuffer1 = ByteBuffer.wrap(data);
Frame outputFrame = new Frame.Builder()
.setImageData(byteBuffer1,
Camera1Session.this.captureFormat.width,
Camera1Session.this.captureFormat.height,
ImageFormat.NV21)
.setTimestampMillis(mFrameProcessor.mPendingTimeMillis)
.setId(mFrameProcessor.mPendingFrameId)
.setRotation(3)
.build();
int w = outputFrame.getMetadata().getWidth();
int h = outputFrame.getMetadata().getHeight();
SparseArray<Face> detectedFaces = mDetector.detect(outputFrame);
if (detectedFaces.size() > 0) {
Face face = detectedFaces.valueAt(0);
ByteBuffer byteBufferRaw = outputFrame.getGrayscaleImageData();
byte[] byteBuffer = byteBufferRaw.array();
YuvImage yuvimage = new YuvImage(byteBuffer, ImageFormat.NV21, w, h, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//My crop logic to get face co-ordinates
yuvimage.compressToJpeg(new Rect(left, top, right, bottom), 80, baos);
final byte[] jpegArray = baos.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(jpegArray, 0, jpegArray.length);
Activity currentActivity = getActivity();
if (currentActivity instanceof CallActivity) {
((CallActivity) currentActivity).setBitmapToImageView(bitmap); //face on ImageView is set just fine
}
Camera1Session.this.events.onByteBufferFrameCaptured(Camera1Session.this, data, Camera1Session.this.captureFormat.width, Camera1Session.this.captureFormat.height, Camera1Session.this.getFrameOrientation(), captureTimeNs);
Camera1Session.this.camera.addCallbackBuffer(data);
} else {
Camera1Session.this.events.onByteBufferFrameCaptured(Camera1Session.this, data, Camera1Session.this.captureFormat.width, Camera1Session.this.captureFormat.height, Camera1Session.this.getFrameOrientation(), captureTimeNs);
Camera1Session.this.camera.addCallbackBuffer(data);
}
}
}
});
}
这样设置它们会导致以下错误:
../../webrtc/sdk/android/src/jni/androidvideotracksource.cc line 82
Check failed: length >= width * height + 2 * uv_width * ((height + 1) / 2) (2630 vs. 460800)
我认为这是因为,androidvideotracksource
没有得到它所期望的byteArray
的长度,因为帧现在被裁剪了。
有人能告诉我如何实现它吗?这是操作数据并输入到视频轨道的正确方式/位置吗
编辑:byteArray数据的位图
与byteArrayjpegArray
不同,在图像视图
上不提供相机预览。可能是因为它们的打包方式不同?尤其是WebRTC,视频流通常假定视频具有固定的维度。如果要裁剪检测到的人脸,您可以选择使用黑色像素(WebRTC不使用透明度)填充裁剪后的图像,并在接收器侧裁剪视频,或者,如果您无法控制接收器,则裁剪后的区域以填充预期的宽*高
帧(还应保持预期的纵横比)
请注意,用于裁剪原始图像的JPEG压缩/解压缩效率很低。可以在中找到其他一些选项。我们是否可以使用WebRTC的数据通道来交换自定义数据,即裁剪的人脸“图像”在您的情况下,使用任何第三方库(如OpenGL等)在接收端进行相应的计算?我建议的原因是,从通道接收的WebRTC视频源是实时流,而不是字节数组。WebRTC视频的固有架构并不意味着从另一方面裁剪视频。如果我们想要裁剪或增强视频,我们可以必须使用任何ar库来完成此工作
我们始终可以利用WebRTC的数据通道来交换自定义数据。不建议使用视频通道来交换自定义数据,因为它是实时流,而不是bytearray。如果有任何问题,请还原。好的,这肯定是原始字节[]数据的打包方式和字节[]的方式的问题jpegArray
已打包。更改打包方式并按照AlexCohn的建议对其进行缩放对我很有效。我在打包方式上从on StackOverflow获得了帮助。以下是它的代码:
private byte[] getNV21(int left, int top, int inputWidth, int inputHeight, Bitmap scaled) {
int [] argb = new int[inputWidth * inputHeight];
scaled.getPixels(argb, 0, inputWidth, left, top, inputWidth, inputHeight);
byte [] yuv = new byte[inputWidth*inputHeight*3/2];
encodeYUV420SP(yuv, argb, inputWidth, inputHeight);
scaled.recycle();
return yuv;
}
private void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
final int frameSize = width * height;
int yIndex = 0;
int uvIndex = frameSize;
int a, R, G, B, Y, U, V;
int index = 0;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
a = (argb[index] & 0xff000000) >> 24; // a is not used obviously
R = (argb[index] & 0xff0000) >> 16;
G = (argb[index] & 0xff00) >> 8;
B = (argb[index] & 0xff) >> 0;
// well known RGB to YUV algorithm
Y = ( ( 66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
U = ( ( -38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
V = ( ( 112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
// NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
// meaning for every 4 Y pixels there are 1 V and 1 U. Note the sampling is every other
// pixel AND every other scanline.
yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
if (j % 2 == 0 && index % 2 == 0) {
yuv420sp[uvIndex++] = (byte)((V<0) ? 0 : ((V > 255) ? 255 : V));
yuv420sp[uvIndex++] = (byte)((U<0) ? 0 : ((U > 255) ? 255 : U));
}
index ++;
}
}
}`
在此之前,我必须缩放位图,这非常简单:
int width = bitmapToScale.getWidth();
int height = bitmapToScale.getHeight();
Matrix matrix = new Matrix();
matrix.postScale(newWidth / width, newHeight / height);
Bitmap scaledBitmap = Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), bitmapToScale.getHeight(), matrix, true);
Re:byteArray数据的位图无法在ImageView上为我提供摄像头预览-如何从NV21数据创建位图?yuvimage。压缩到JPEG(新的Rect(左、上、右、下)、80、BAS);
对byteArray执行此操作。我从解码byteArray
获得位图,((CallActivity)currentActivity)。setBitmapToImageView(位图)
未按预期工作,但((CallActivity)currentActivity)。setBitmapToImageView(jpegArray)
工作?从字节[]数据创建位图并将其设置为imageView不起作用,而是从字节[]创建位图jpegArray
确实有效。无论如何,我已经发布了我的答案和修复程序。此外,我还按照您指出的尺寸进行了缩放。但是我无法使I420Frame
工作。只是检查了这个。通过jpeg需要5-10毫秒,并且scale()
+getNV21()
需要50-70毫秒。这些都不会发生在UI线程上。我只在setBitmapToImageView(位图)中返回UI线程
数据通道
是否足以支持大型byteArray
的连续流?否。必须以叠加或任何形式进行增强对象识别。为此,一方可以使用webrtc数据通道与另一方交换图像以及不同的其他相关坐标,即裁剪细节w.r.t.图像。在接收方,可以进行上下文计算以显示裁剪后的脸的覆盖,可以使用openGL显示实时提要。缩放或填充数据时的带宽消耗将与视频通话时的带宽消耗相同,否?此外,如果填充低光视频,接收时的脸裁剪将非常繁琐(在我的情况下,这也会使计算量加倍)。缩放会破坏人脸本身的纵横比。无论如何,我可以在I420Frame
中附加人脸纵横比?由于视频压缩,恒定填充(黑色不是必需的)的带宽开销最小。缩放(放大)基于同样的原因,位图的效果不会增加太多带宽。我不明白“中断纵横比”是什么意思。是的,您可以在I420Frame内工作。不清楚如何处理getNV21()
。编辑了我的答案以反映这一点。谢谢。
Camera1Session.this.events.onByteBufferFrameCaptured(
Camera1Session.this,
data,
w,
h,
Camera1Session.this.getFrameOrientation(),
captureTimeNs);
Camera1Session.this.camera.addCallbackBuffer(data);
int width = bitmapToScale.getWidth();
int height = bitmapToScale.getHeight();
Matrix matrix = new Matrix();
matrix.postScale(newWidth / width, newHeight / height);
Bitmap scaledBitmap = Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), bitmapToScale.getHeight(), matrix, true);