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

相关文章

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操

Redis出现中文乱码的问题及解决

《Redis出现中文乱码的问题及解决》:本文主要介绍Redis出现中文乱码的问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 问题的产生2China编程. 问题的解决redihttp://www.chinasem.cns数据进制问题的解决中文乱码问题解决总结

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

全面解析MySQL索引长度限制问题与解决方案

《全面解析MySQL索引长度限制问题与解决方案》MySQL对索引长度设限是为了保持高效的数据检索性能,这个限制不是MySQL的缺陷,而是数据库设计中的权衡结果,下面我们就来看看如何解决这一问题吧... 目录引言:为什么会有索引键长度问题?一、问题根源深度解析mysql索引长度限制原理实际场景示例二、五大解决

Springboot如何正确使用AOP问题

《Springboot如何正确使用AOP问题》:本文主要介绍Springboot如何正确使用AOP问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录​一、AOP概念二、切点表达式​execution表达式案例三、AOP通知四、springboot中使用AOP导出

Python中Tensorflow无法调用GPU问题的解决方法

《Python中Tensorflow无法调用GPU问题的解决方法》文章详解如何解决TensorFlow在Windows无法识别GPU的问题,需降级至2.10版本,安装匹配CUDA11.2和cuDNN... 当用以下代码查看GPU数量时,gpuspython返回的是一个空列表,说明tensorflow没有找到