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

相关文章

好题——hdu2522(小数问题:求1/n的第一个循环节)

好喜欢这题,第一次做小数问题,一开始真心没思路,然后参考了网上的一些资料。 知识点***********************************无限不循环小数即无理数,不能写作两整数之比*****************************(一开始没想到,小学没学好) 此题1/n肯定是一个有限循环小数,了解这些后就能做此题了。 按照除法的机制,用一个函数表示出来就可以了,代码如下

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

第10章 中断和动态时钟显示

第10章 中断和动态时钟显示 从本章开始,按照书籍的划分,第10章开始就进入保护模式(Protected Mode)部分了,感觉从这里开始难度突然就增加了。 书中介绍了为什么有中断(Interrupt)的设计,中断的几种方式:外部硬件中断、内部中断和软中断。通过中断做了一个会走的时钟和屏幕上输入字符的程序。 我自己理解中断的一些作用: 为了更好的利用处理器的性能。协同快速和慢速设备一起工作

如何在页面调用utility bar并传递参数至lwc组件

1.在app的utility item中添加lwc组件: 2.调用utility bar api的方式有两种: 方法一,通过lwc调用: import {LightningElement,api ,wire } from 'lwc';import { publish, MessageContext } from 'lightning/messageService';import Ca

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

购买磨轮平衡机时应该注意什么问题和技巧

在购买磨轮平衡机时,您应该注意以下几个关键点: 平衡精度 平衡精度是衡量平衡机性能的核心指标,直接影响到不平衡量的检测与校准的准确性,从而决定磨轮的振动和噪声水平。高精度的平衡机能显著减少振动和噪声,提高磨削加工的精度。 转速范围 宽广的转速范围意味着平衡机能够处理更多种类的磨轮,适应不同的工作条件和规格要求。 振动监测能力 振动监测能力是评估平衡机性能的重要因素。通过传感器实时监

缓存雪崩问题

缓存雪崩是缓存中大量key失效后当高并发到来时导致大量请求到数据库,瞬间耗尽数据库资源,导致数据库无法使用。 解决方案: 1、使用锁进行控制 2、对同一类型信息的key设置不同的过期时间 3、缓存预热 1. 什么是缓存雪崩 缓存雪崩是指在短时间内,大量缓存数据同时失效,导致所有请求直接涌向数据库,瞬间增加数据库的负载压力,可能导致数据库性能下降甚至崩溃。这种情况往往发生在缓存中大量 k