我有一个 ByteBuffer:
var rgbaData = JniUtil.nativeAllocateByteBuffer(frame.width * frame.height * 4)
其中框架是从实时流媒体中获取的VideoFrame。我想将HighPassSkinSmoothing滤镜应用于从上述缓冲区获取的位图,将位图转换回ByteArray,将其放入ByteBuffer中,然后释放回实时流媒体。我已经完成了以下工作:将缓冲区转换为位图,应用滤镜,然后再次将位图转换为ByteArray,并将其放入缓冲区。
应用HighPassSkinSmoothing滤镜:
val bitmap = getBitmapFromBuffer(frame.width, frame.height, rgbaData)
amniXSkinSmooth.storeBitmap(bitmap, false)
amniXSkinSmooth.initSdk()
amniXSkinSmooth.startSkinSmoothness(600F)
amniXSkinSmooth.startSkinWhiteness(2F)
//sometimes "amniXSkinSmooth.bitmapAndFree" is also null, might be due to wrong conversion process
rgbaData.put(getByteArrayFromBitmap(amniXSkinSmooth.bitmapAndFree))
amniXSkinSmooth.unInitSdk();
将 ByteBuffer 转换为 Bitmap:
private fun getBitmapFromBuffer(width: Int, height: Int, buffer: ByteBuffer): Bitmap {
buffer.rewind()
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
bitmap.copyPixelsFromBuffer(buffer)
return bitmap
}
将位图转换回字节数组:
private fun getByteArrayFromBitmap(b: Bitmap): ByteArray {
val size: Int = b.byteCount
val buffer = ByteBuffer.allocate(size)
val bytes = ByteArray(size)
b.copyPixelsToBuffer(buffer)
buffer.rewind()
buffer.get(bytes)
return bytes
}
通过这样做,我失去了帧数,并且应用过滤器后实时流媒体也会出现延迟。有没有什么解决方法,让帧数不会下降,实时流媒体也不会延迟?
如果您对转换过程或其他任何事情有任何想法或知识,您的帮助将不胜感激。谢谢!!
getByteArrayFromBitmap()
方法应该被命名为getByteBufferFromBitmap()
,因为它返回的是 ByteBuffer。不要让自己被自己的命名方案所迷惑。 - user207421