流媒体程序开发之:H264解码器移植到OPhone
2009-09-22 00:00:00 来源:WEB开发网图片看不清楚?请点击这里查看原图(大图)。
图片看不清楚?请点击这里查看原图(大图)。
如果你看到了Install:**,这说明你的库已经编译好了。
FAQ 2:
如果编译遇到下面错误,怎么办?
error: redefinition of typedef 'int8_t'
需要注释掉你的代码中“typedef signed char int8_t;”,如果你的代码之前是已经移植到了Mobile/Symbian上的话,很有可能遇到这个问题。
5.4 编写库测试程序
用Eclipse创建一个OPhone工程,在入口类中输入如下代码:
/**
* @author ophone
* @email 3751624@qq.com
*/
package ophone.streaming.video.h264;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import OPhone.app.Activity;
import OPhone.graphics.BitmapFactory;
import OPhone.os.Bundle;
import OPhone.os.Handler;
import OPhone.os.Message;
import OPhone.widget.ImageView;
import OPhone.widget.TextView;
public class H264Example extends Activity {
private static final int VideoWidth = 352;
private static final int VideoHeight = 288;
private ImageView ImageLayout = null;
private TextView FPSLayout = null;
private H264decode Decode = null;
private Handler H = null;
private byte[] Buffer = null;
private int DecodeCount = 0;
private long StartTime = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageLayout = (ImageView) findViewById(R.id.ImageView);
FPSLayout = (TextView) findViewById(R.id.TextView);
Decode = new H264decode();
StartTime = System.currentTimeMillis();
new Thread(new Runnable(){
public void run() {
StartDecode();
}
}).start();
H = new Handler(){
public void handleMessage(Message msg) {
ImageLayout.invalidate();
ImageLayout.setImageBitmap(BitmapFactory.decodeByteArray(Buffer, 0, Buffer.length));
long Time = (System.currentTimeMillis()-StartTime)/1000;
if(Time > 0){
FPSLayout.setText("花费时间:" + Time + "秒 解码帧数:" + DecodeCount + " FPS:" + (DecodeCount/Time) );
}
}
};
}
private void StartDecode(){
File h264file = new File("/tmp/Demo.264");
InputStream h264stream = null;
try {
h264stream = new FileInputStream(h264file);
ByteBuffer pInBuffer = ByteBuffer.allocate(51200);//分配50k的缓存
ByteBuffer pRGBBuffer = ByteBuffer.allocate(VideoWidth*VideoHeight*3);
while (h264stream.read(pInBuffer.array(), pInBuffer.position(), pInBuffer.remaining()) >= 0) {
pInBuffer.position(0);
do{
int DecodeLength = Decode.DecodeOneFrame(pInBuffer, pRGBBuffer);
//如果解码成功,把解码出来的图片显示出来
if(DecodeLength > 0 && pRGBBuffer.position() > 0){
//转换RGB字节为BMP
BMPImage bmp = new BMPImage(pRGBBuffer.array(),VideoWidth,VideoHeight);
Buffer = bmp.getByte();
H.sendMessage(H.obtainMessage());
Thread.sleep(1);
DecodeCount ++;
}
}while(pInBuffer.remaining() > 10240);//确保缓存区里面的数据始终大于10k
//清理已解码缓冲区
int Remaining = pInBuffer.remaining();
System.arraycopy(pInBuffer.array(), pInBuffer.position(), pInBuffer.array(), 0, Remaining);
pInBuffer.position(Remaining);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try{h264stream.close();} catch(Exception e){}
}
}
protected void onDestroy() {
super.onDestroy();
Decode.Cleanup();
}
}
更多精彩
赞助商链接