我正在尝试将byte[] data
转换为bitmap
。事实证明,作为初学者,这对我来说并不容易。视频使用avc
或H.264
编码。
我假设数组一次保持一帧。我注意到数组的大小在每次迭代时都会改变。当我尝试执行bmp.copyPixelsFromBuffer(sampleData)
时收到错误消息:
java.lang.RuntimeException: Buffer not large enough for pixels.
宽度和高度是之前设置的,因此我假定字节数组根据此大小返回帧,但是我不确定是否是这种情况。那么如何创建与ByteBuffer的大小相对应的位图?
做this并打电话给sampleData.rewind();
并没有帮助。
private void initListener() {
mediaManager.setMediaListener(new MediaStreamListener() {
@Override
public void getVideoStream(byte[] bytes) {
showViewData(ByteBuffer.wrap(bytes));
}
@Override
public void getAudioStream(byte[] bytes) {
}
});
}
private void showViewData(ByteBuffer sampleData) {
Bitmap bmp = Bitmap.createBitmap(640, 420, Config.ARGB_8888);
bmp.copyPixelsFromBuffer(sampleData);
imageView.setImageBitmap(bitmap);
}
我认为编码/解码有问题,对吗?有人可以指导我朝正确的方向前进吗?
编辑1
此外,我尝试了此操作,但它始终返回bitmap = null
。
public void getVideoStream(byte[] bytes) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inPreferredConfig = null;
options.inMutable = true;
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
imageView.setImageBitmap(bitmap);
}