在android中怎么读取视频文件呢?读取一个.yuv类型视频文件,并把该文件保存为byte[]类型的;
我用的方法出现 Out of Memoryerror错误;
代码如下:
public static byte[] getByetsFromFile(File file) {
byte[]buffer = null;
ByteArrayOutputStream bos =null;
try{
FileInputStream fis = new FileInputStream(file);
bos=new ByteArrayOutputStream();
buffer = new byte[1024];
int length = 0;
while((length = fis.read(buffer))!= -1){
bos.write(buffer, 0, length);
}
fis.close();
bos.close();
return bos.toByteArray();//字节流转换为一个 byte数组然后采用byte[] yuv = getByetsFromFile(new File("sdcard/foreman.yuv"));
请问大家该如何修改呢?
我用的方法出现 Out of Memoryerror错误;
代码如下:
public static byte[] getByetsFromFile(File file) {
byte[]buffer = null;
ByteArrayOutputStream bos =null;
try{
FileInputStream fis = new FileInputStream(file);
bos=new ByteArrayOutputStream();
buffer = new byte[1024];
int length = 0;
while((length = fis.read(buffer))!= -1){
bos.write(buffer, 0, length);
}
fis.close();
bos.close();
return bos.toByteArray();//字节流转换为一个 byte数组然后采用byte[] yuv = getByetsFromFile(new File("sdcard/foreman.yuv"));
请问大家该如何修改呢?
解决方案 »
- 4.0以上版本如何获取添加APN权限
- Service 多次启动对系统有没有影响?
- android短信发送界面 布局的问题求大神解~~~~
- 如何在android系统上编程实现3d tag cloud(3d标签云特效)
- 怎样监控一个Activity的状态呢
- the application has stop unexpectedly
- 导入第三方包的时候,eclipse提示Conversion to Dalvik format failed with error 1该怎么解决啊
- 使用刷机软件后遗症。
- 如何监听应用程序意外终止?
- 新人,对于安卓的开发只停留在搭建安卓开发环境,求问能做一个简单的APP吗
- 看见一个实时采集视频的程序!!!
- SQLiteOpenHelper类的onCreate方法什么时候被执行
http://www.fourcc.org/yuv.php
以你这算法,就算10M能取,那100M、1G、10G呢,再好的配置也会有撑爆的一天
public static byte[] getByetsFromFile(File file){
FileInputStream is = null;
// 获取文件大小
long length = file.length();
// 创建一个数据来保存文件数据
byte[] fileData = new byte[(int)length];
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int bytesRead=0;
// 读取数据到byte数组中
while(bytesRead != fileData.length) {
try {
bytesRead += is.read(fileData, bytesRead, fileData.length - bytesRead);
if(is != null)
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return fileData;
}
没有Out of Memoryerror错误了,不过运行出来的是
{
int readCount=0; //已经从YUYV读取的次数
int frameCountPerTime=2;//每次读取的帧数,考虑YUYV的数据排列方式,每次读取2或2的倍数帧比较好处理
byte[] buffYUYV = new byte[1024];
File file = new File("/mnt/sdcard/test.YUV");
int length=0;
do
{
length= getByetsFromYUYVFile(file,buffYUYV,readCount,frameCountPerTime);//读取了2帧的YUV数据
byte[] buffRGB565= ConvertYUYVToRGB565(buffYUYV,frameCountPerTime);//转换后得到2帧的RGB565数据
for(int i=0;i<frameCountPerTime;i++)
{
// 将每帧的RGB565数据绘制到界面
}
readCount++;
}while(length==frameCountPerTime*2);
}
private byte[] ConvertYUYVToRGB565(byte[] buffYUYV,int frameCountPerTime)
{
byte[] buffRGB565=null;
// 将YUYV的数据转换为GRB565
// 返回GRB565数据
return buffRGB565;
}
private int getByetsFromYUYVFile(File file,byte[] buffer,int readCount,int frameCountPerTime) {
int length = 0;
FileInputStream fis =null;
try
{
fis = new FileInputStream(file);
length=fis.read(buffer, readCount*320*240*2*frameCountPerTime, 2*frameCountPerTime);
fis.close();
}
catch(Exception ex)
{
//to do
}
return length;
}
像NV21、YV12等稍微麻烦点,需要在方法getByetsFromYUYVFile中通过一番计算才能找到要读取的帧数据
fis = new FileInputStream(file);
length=fis.read(buffer, readCount*320*240*2*frameCountPerTime, 2*frameCountPerTime);
在JNI中进行解码效率相对高些吧。