ffmpeg调用android相机视频流,关于ffmpeg读取视频流在安卓上显示的问题,讨教各位前辈!...

本文主要是介绍ffmpeg调用android相机视频流,关于ffmpeg读取视频流在安卓上显示的问题,讨教各位前辈!...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

参照的方式http://www.cnblogs.com/doandroid/archive/2011/11/09/2242558.html

MainActivity.java中

private static native void openFile();

private static native void drawFrame(Bitmap bitmap);

private static native void drawFrameAt(Bitmap bitmap, int secs);

private Bitmap mBitmap;

private int mSecs = 0;

static {

System.loadLibrary(“ffmpeg”);

}

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mBitmap = Bitmap.createBitmap(320, 240, Bitmap.Config.ARGB_8888);

openFile();

Button btn = (Button)findViewById(R.id.frame_adv);

btn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

drawFrame(mBitmap);

ImageView i = (ImageView)findViewById(R.id.frame);

i.setImageBitmap(mBitmap);

}

});

}

jni的native.c中

static void fill_bitmap(AndroidBitmapInfo*  info, void *pixels, AVFrame *pFrame)

{

uint8_t *frameLine;

int  yy;

for (yy = 0; yy height; yy++) {

uint8_t*  line = (uint8_t*)pixels;

frameLine = (uint8_t *)pFrame->data[0] + (yy * pFrame->linesize[0]);

int xx;

for (xx = 0; xx width; xx++) {

int out_offset = xx * 4;

int in_offset = xx * 3;

line[out_offset] = frameLine[in_offset];

line[out_offset+1] = frameLine[in_offset+1];

line[out_offset+2] = frameLine[in_offset+2];

line[out_offset+3] = 0;

}

pixels = (char*)pixels + info->stride;

}

}

void setupScaler()

{

// avpicture_alloc(&picture, PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);

static int sws_flags =  SWS_FAST_BILINEAR;

img_convert_ctx = sws_getContext(pCodecCtx->width,

pCodecCtx->height,

pCodecCtx->pix_fmt,

pCodecCtx->width,

pCodecCtx->height,

PIX_FMT_RGB24,

sws_flags, NULL, NULL, NULL);

}

void Java_cn_ct_rtmpdemo1_MainActivity_openFile(JNIEnv * env, jobject this)

{

int ret;

int err;

int i;

AVCodec *pCodec;

uint8_t *buffer;

int numBytes;

av_register_all();// 初始化 libavformat和注册全部的muxers、demuxers和protocols

avformat_network_init();

LOGE(“Registered formats”);

char errCode[512];

err = avformat_open_input(&pFormatCtx, “rtmp://XXX.64.87.XX/e/crovd1 live=1”, NULL, NULL);

LOGE(“Called open file”);

if(err!=0) {

av_strerror(err,errCode,512);

av_log_set_level(AV_LOG_DEBUG);

LOGE(“Couldn””t open file %s”,errCode);

return;

}

LOGE(“Opened file”);

if(avformat_find_stream_info(pFormatCtx,NULL)<0) {

LOGE(“Unable to get stream info”);

return;

}

videoStream = -1;

for (i=0; inb_streams; i++) {//AVMEDIA_TYPE_VIDEO

//    if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) {

if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {

videoStream = i;

LOGE(“get videoStream”);

break;

}

}

if(videoStream==-1) {

LOGE(“Unable to find video stream”);

return;

}

LOGE(“Video stream is [%d]”, videoStream);

pCodecCtx=pFormatCtx->streams[videoStream]->codec;

pCodec=avcodec_find_decoder(pCodecCtx->codec_id);

if(pCodec==NULL) {

LOGE(“Unsupported codec %d”,pCodecCtx->codec_id);

return;

}

LOGE(“avcodec_find_decoder”);

if(avcodec_open2(pCodecCtx, pCodec,NULL)<0) {

LOGE(“Unable to open codec”);

return;

}

pFrame=avcodec_alloc_frame();

pFrameRGB=avcodec_alloc_frame();

LOGE(“Video size is [%d x %d]”, pCodecCtx->width, pCodecCtx->height);

setupScaler();

numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);

buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));

avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,

pCodecCtx->width, pCodecCtx->height);

LOGE(“avpicture_fill_”);

}

void Java_cn_ct_rtmpdemo1_MainActivity_drawFrame(JNIEnv * env, jobject this, jstring bitmap)

{

AndroidBitmapInfo  info;

void*              pixels;

int                ret;

int err;

int i;

int frameFinished = 0;

AVPacket packet;

//   static struct SwsContext *img_convert_ctx;

int64_t seek_target;

if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) 

LOGE(“AndroidBitmap_getInfo() failed ! error=%d”, ret);

return;

}

LOGE(“Checked on the bitmap”);

if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) 

LOGE(“AndroidBitmap_lockPixels() failed ! error=%d”, ret);

}

LOGE(“Grabbed the pixels”);

i = 0;

while((i==0) && (av_read_frame(pFormatCtx, &packet)>=0)) {

if(packet.stream_index==videoStream) {

avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);

if(frameFinished) {

LOGE(“packet pts %llu”, packet.pts);

if(img_convert_ctx == NULL) {

LOGE(“could not initialize conversion context\n”);

return;

}

sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);

// save_frame(pFrameRGB, target_width, target_height, i);

fill_bitmap(&info, pixels, pFrameRGB);

i = 1;

}

}

av_free_packet(&packet);

}

AndroidBitmap_unlockPixels(env, bitmap);

}

点击界面上的Frame按钮,读取到的图片如下所示,像素描一样,没有色彩,讨教大家是怎么回事,是不是转换成图片的时候出了问题

465972637504821030fb94ec6563bc77.png

这篇关于ffmpeg调用android相机视频流,关于ffmpeg读取视频流在安卓上显示的问题,讨教各位前辈!...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/466092

相关文章

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

MySQL主从同步延迟问题的全面解决方案

《MySQL主从同步延迟问题的全面解决方案》MySQL主从同步延迟是分布式数据库系统中的常见问题,会导致从库读取到过期数据,影响业务一致性,下面我将深入分析延迟原因并提供多层次的解决方案,需要的朋友可... 目录一、同步延迟原因深度分析1.1 主从复制原理回顾1.2 延迟产生的关键环节二、实时监控与诊断方案

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

SQLyog中DELIMITER执行存储过程时出现前置缩进问题的解决方法

《SQLyog中DELIMITER执行存储过程时出现前置缩进问题的解决方法》在SQLyog中执行存储过程时出现的前置缩进问题,实际上反映了SQLyog对SQL语句解析的一个特殊行为,本文给大家介绍了详... 目录问题根源正确写法示例永久解决方案为什么命令行不受影响?最佳实践建议问题根源SQLyog的语句分

电脑显示mfc100u.dll丢失怎么办?系统报错mfc90u.dll丢失5种修复方案

《电脑显示mfc100u.dll丢失怎么办?系统报错mfc90u.dll丢失5种修复方案》最近有不少兄弟反映,电脑突然弹出“mfc100u.dll已加载,但找不到入口点”的错误提示,导致一些程序无法正... 在计算机使用过程中,我们经常会遇到一些错误提示,其中最常见的就是“找不到指定的模块”或“缺少某个DL

解决IDEA报错:编码GBK的不可映射字符问题

《解决IDEA报错:编码GBK的不可映射字符问题》:本文主要介绍解决IDEA报错:编码GBK的不可映射字符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录IDEA报错:编码GBK的不可映射字符终端软件问题描述原因分析解决方案方法1:将命令改为方法2:右下jav

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

MyBatis模糊查询报错:ParserException: not supported.pos 问题解决

《MyBatis模糊查询报错:ParserException:notsupported.pos问题解决》本文主要介绍了MyBatis模糊查询报错:ParserException:notsuppo... 目录问题描述问题根源错误SQL解析逻辑深层原因分析三种解决方案方案一:使用CONCAT函数(推荐)方案二:

Redis 热 key 和大 key 问题小结

《Redis热key和大key问题小结》:本文主要介绍Redis热key和大key问题小结,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、什么是 Redis 热 key?热 key(Hot Key)定义: 热 key 常见表现:热 key 的风险:二、