【发布时间】:2011-09-15 21:47:01
【问题描述】:
我正在尝试将 ARGB byte 数组解码为 Bitmap 以将其显示为 ImageView。
我尝试使用BitmapFactory 的decodeByteArray() 和decodeStream(),但两种方式都将null 作为Bitmap。
但是当我使用createBitmap() 和setPixels() 创建位图时,它可以完美运行。这是工作代码。
data 是大小为imageWidth*imageHeight*4的字节数组
int[] pixels=new int[imageWidth*imageHeight];
int i=0,j=0;
while (i<imageWidth*imageHeight*4) {
pixels[j]= bytesToInt(data[i], data[i+1], data[i+2],data[i+3]);
i += 4;
j++;
}
Bitmap bitmap=Bitmap. createBitmap( imageWidth,imageHeight,Bitmap.Config .ARGB_8888)
bmpf.setPixels(pixels, 0, imageWidth , 0, 0, imageWidth, imageHeight);
//---------------------- definition of bytesToInt()
int bytesToInt(byte b1,byte b2,byte b3,byte b4)
{
return (((b1& 0xff)<<24)+((b2 & 0xff)<<16)+((b3&0xff)<<8)+(b4&0xff));
}
但我需要使用decodeByteArray() 或decodeStream() 来实现这一点,因为对于后一种方式,我需要从byte 数组中为setPixels() 创建整数数组,这是低效的。
我想要实现的是一个视频播放器,因此它需要每秒显示大约 15 帧/图像。
希望有人可以帮助我。 提前致谢
【问题讨论】:
-
感谢 Peter Knego 的快速回复。但对我来说,视频渲染代码是完整的并且可以工作,问题是由于这个 setPixels 函数,渲染视频帧图像的速度很慢。诠释 i=0,j=0; while (i
-
是的,byte[] 是 ARGB 格式,作为 BitmapFactory.decodeStream 的输入给出