ffmpeg综合应用示例(三)——安卓手机摄像头编码
本文的示例将实现:读取安卓手机摄像头数据并使用H.264编码格式实时编码保存为flv文件。示例包含了
1、编译适用于安卓平台的ffmpeg库
2、在java中通过JNI使用ffmpeg
3、读取安卓摄像头数据并在后台线程中使用ffmpeg进行编码的基本流程
具有较强的综合性。
编译适用于安卓平台的ffmpeg库
平时我们编译ffmpeg类库都是在x86平台下,而安卓手机属于arm平台,所以要先通过交叉编译的方法在x86平台下编译出可以在arm平台下使用的ffmpeg类库。Google就为我们提供了一套可以再Linux下完成该任务的交叉编译工具链——NDK,下载时可以看到google同时提供了ndk的windows版和linux版,前者可以在windows下通过cygwin(可以理解为一个在windows下模拟linux的工具)使用。我这里使用的是Android-ndk-r10e,在cygwin下使用。ndk的下载安装基本是傻瓜式的,但要注意linux系统对x86和x64是分的很清楚的,不像windows那么随性,此外还需要提醒的是,在下载安装cygwin时,只需要选择binutils , gcc , gcc-mingw , gdb , make这几个组件。安装完成后,在cygwin安装目录下点击cygwin.bat打开命令窗口,输入make -version验证是否安装成功。
做好以上的准备工作之后就可以正式开始ffmpeg源码的编译了。
首先需要修改configure文件,确保类库版本号不会出现在.so后缀名的后面,否则安卓平台无法识别这样的类库
找到下面几句
- SLIBNAME_WITH_MAJOR='$(SLIBNAME).$(LIBMAJOR)'
- LIB_INSTALL_EXTRA_CMD='$$(RANLIB)"$(LIBDIR)/$(LIBNAME)"'
- SLIB_INSTALL_NAME='$(SLIBNAME_WITH_VERSION)'
- SLIB_INSTALL_LINKS='$(SLIBNAME_WITH_MAJOR)$(SLIBNAME)'
- SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)'
- LIB_INSTALL_EXTRA_CMD='$$(RANLIB)"$(LIBDIR)/$(LIBNAME)"'
- SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'
- SLIB_INSTALL_LINKS='$(SLIBNAME)'
- make clean
- export NDK=xxxx/android-ndk-r10e
- export PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.8/prebuilt
- export PLATFORM=$NDK/platforms/android-8/arch-arm
- export PREFIX=../android_ffmpeglib
- ./configure --target-os=linux --prefix=$PREFIX \
- --enable-cross-compile \
- --enable-runtime-cpudetect \
- --disable-asm \
- --arch=arm \
- --cc=$PREBUILT/windows/bin/arm-linux-androideabi-gcc \
- --cross-prefix=$PREBUILT/windows/bin/arm-linux-androideabi- \
- --disable-stripping \
- --nm=$PREBUILT/windows/bin/arm-linux-androideabi-nm \
- --sysroot=$PLATFORM \
- --enable-gpl --enable-shared --disable-static --enable-small \
- --disable-ffprobe --disable-ffplay --disable-ffmpeg --disable-ffserver --disable-debug \
- --extra-cflags="-fPIC -DANDROID -D__thumb__ -mthumb -Wfatal-errors -Wno-deprecated -mfloat-abi=softfp -marm -march=armv7-a"
- make
- make install
在java中通过JNI使用ffmpeg
JNI即java本地接口,java native interface,它是一个协议,
该协议用来沟通Java代码和外部的本地C/C++代码, 通过该协议 Java代码可以调用外部的本地代码, 外部的C/C++
代码可以调用Java代码。简单来说,就是将一个C语言的方法映射到Java的某个方法上;
JNI中的一些概念 :
-- native : Java语言中修饰本地方法的修饰符, 被该修饰符修饰的方法没有方法体;
-- Native方法 : 在Java语言中被native关键字修饰的方法是Native方法;
-- JNI层 : Java声明Native方法的部分;
-- JNI函数 : JNIEnv提供的函数, 这些函数在jni.h中进行定义;
-- JNI方法 : Native方法对应的JNI层实现的 C/C++方法, 即在jni目录中实现的那些C语言代码;
1、创建常规Android项目
2、声明Native方法,形如public native String stringFromJNI();
3、创建c文件
在工程根目录下创建 jni 目录, 然后创建一个c语言源文件, 在文件中引入 include <jni.h>。c语言方法声明形如
- jstring
- Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
- jobject thiz )
-- JNIEnv参数 : 代表的是Java环境, 通过这个环境可以调用Java里面的方法;
-- jobject参数 : 调用C语言方法的对象, thiz对象表示当前的对象, 即调用JNI方法所在的类;
4、编写Android.mk文件
就好比常用的makefile,形如
- LOCAL_PATH := $(call my-dir)
- include $(CLEAR_VARS)
- LOCAL_MODULE := hello-jni
- LOCAL_SRC_FILES := hello-jni.c
- include $(BUILD_SHARED_LIBRARY)
-- include $(CLEAR_VARS) : 编译工具函数, 通过该函数可以进行一些初始化操作;
-- LOCAL_MODULE : 编译后的 .so 后缀文件叫什么名字;
-- LOCAL_SRC_FILES: 指定编译的源文件名称;
-- include $(BUILD_SHARED_LIBRARY) : 告诉编译器需要生成动态库;
5、NDK编译生成动态库
利用上面写好的.mk文件,进入 cygdrive 找到windows目录下对应的文件, 编译完成之后, 会自动生成so文件并放在libs目录下, 之后就可以在Java中调用C语言方法了;
6、java中加载动态库
在Java类中的静态代码块中使用System.LoadLibrary()方法加载编译好的 .so 动态库,形如
- static {
- System.loadLibrary("hello-jni");
- }
需要说明的是,调试JNI程序非常麻烦,无法进入c文件中进行单步调试,只能通过输出一些信息然后利用logcat进行调试,所以也可以先在vs里面把c文件的内容跑一遍简单验证一下。
第一步:我们声明如下四个Native方法
- //JNI
- //初始化,读取待编码数据的宽和高
- public native int initial(int width,int height);
- //读取yuv数据进行编码
- public native int encode(byte[] yuvimage);
- //清空缓存的帧
- public native int flush();
- //清理
- public native int close();
第二步:对应的c文件内容如下,基本就是一个将yuv数据编码为H.264的flv文件的流程,唯一需要注意的就是安卓摄像头拍摄数据的像素格式为NV21,需要转换为YUV420P才能进行编码
- /**
- * 基于FFmpeg安卓摄像头编码
- * FFmpeg Android Camera Encoder
- *
- * 张晖 Hui Zhang
- * zhanghuicuc@gmail.com
- * 中国传媒大学/数字电视技术
- * Communication University of China / Digital TV Technology
- *
- *
- */
- #include <stdio.h>
- #include <time.h>
- #include "libavcodec/avcodec.h"
- #include "libavformat/avformat.h"
- #include "libswscale/swscale.h"
- #include "libavutil/log.h"
- #ifdef ANDROID
- #include <jni.h>
- #include <android/log.h>
- #define LOGE(format, ...) __android_log_print(ANDROID_LOG_ERROR, "(>_<)", format, ##__VA_ARGS__)
- #define LOGI(format, ...) __android_log_print(ANDROID_LOG_INFO, "(=_=)", format, ##__VA_ARGS__)
- #else
- #define LOGE(format, ...) printf("(>_<) " format "\n", ##__VA_ARGS__)
- #define LOGI(format, ...) printf("(^_^) " format "\n", ##__VA_ARGS__)
- #endif
- AVFormatContext *ofmt_ctx;
- AVStream* video_st;
- AVCodecContext* pCodecCtx;
- AVCodec* pCodec;
- AVPacket enc_pkt;
- AVFrame *pFrameYUV;
- int framecnt = 0;
- int yuv_width;
- int yuv_height;
- int y_length;
- int uv_length;
- int64_t start_time;
- //Output FFmpeg's av_log()
- void custom_log(void *ptr, int level, const char* fmt, va_list vl){
- FILE *fp=fopen("/storage/emulated/0/av_log.txt","a+");
- if(fp){
- vfprintf(fp,fmt,vl);
- fflush(fp);
- fclose(fp);
- }
- }
- JNIEXPORT jint JNICALL Java_com_zhanghui_test_MainActivity_initial
- (JNIEnv *env, jobject obj,jint width,jint height)
- {
- const char* out_path = "/sdcard/zhanghui/testffmpeg.flv";
- yuv_width=width;
- yuv_height=height;
- y_length=width*height;
- uv_length=width*height/4;
- //FFmpeg av_log() callback
- av_log_set_callback(custom_log);
- av_register_all();
- //output initialize
- avformat_alloc_output_context2(&ofmt_ctx, NULL, "flv", out_path);
- //output encoder initialize
- pCodec = avcodec_find_encoder(AV_CODEC_ID_H264);
- if (!pCodec){
- LOGE("Can not find encoder!\n");
- return -1;
- }
- pCodecCtx = avcodec_alloc_context3(pCodec);
- pCodecCtx->pix_fmt = PIX_FMT_YUV420P;
- pCodecCtx->width = width;
- pCodecCtx->height = height;
- pCodecCtx->time_base.num = 1;
- pCodecCtx->time_base.den = 30;
- pCodecCtx->bit_rate = 800000;
- pCodecCtx->gop_size = 300;
- /* Some formats want stream headers to be separate. */
- if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
- pCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
- //H264 codec param
- //pCodecCtx->me_range = 16;
- //pCodecCtx->max_qdiff = 4;
- //pCodecCtx->qcompress = 0.6;
- pCodecCtx->qmin = 10;
- pCodecCtx->qmax = 51;
- //Optional Param
- pCodecCtx->max_b_frames = 3;
- // Set H264 preset and tune
- AVDictionary *param = 0;
- av_dict_set(¶m, "preset", "ultrafast", 0);
- av_dict_set(¶m, "tune", "zerolatency", 0);
- if (avcodec_open2(pCodecCtx, pCodec, ¶m) < 0){
- LOGE("Failed to open encoder!\n");
- return -1;
- }
- //Add a new stream to output,should be called by the user before avformat_write_header() for muxing
- video_st = avformat_new_stream(ofmt_ctx, pCodec);
- if (video_st == NULL){
- return -1;
- }
- video_st->time_base.num = 1;
- video_st->time_base.den = 30;
- video_st->codec = pCodecCtx;
- //Open output URL,set before avformat_write_header() for muxing
- if (avio_open(&ofmt_ctx->pb, out_path, AVIO_FLAG_READ_WRITE) < 0){
- LOGE("Failed to open output file!\n");
- return -1;
- }
- //Write File Header
- avformat_write_header(ofmt_ctx, NULL);
- start_time = av_gettime();
- return 0;
- }
- JNIEXPORT jint JNICALL Java_com_zhanghui_test_MainActivity_encode
- (JNIEnv *env, jobject obj, jbyteArray yuv)
- {
- int ret;
- int enc_got_frame=0;
- int i=0;
- pFrameYUV = avcodec_alloc_frame();
- uint8_t *out_buffer = (uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
- avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
- //安卓摄像头数据为NV21格式,此处将其转换为YUV420P格式
- jbyte* in= (jbyte*)(*env)->GetByteArrayElements(env,yuv,0);
- memcpy(pFrameYUV->data[0],in,y_length);
- for(i=0;i<uv_length;i++)
- {
- *(pFrameYUV->data[2]+i)=*(in+y_length+i*2);
- *(pFrameYUV->data[1]+i)=*(in+y_length+i*2+1);
- }
- pFrameYUV->format = AV_PIX_FMT_YUV420P;
- pFrameYUV->width = yuv_width;
- pFrameYUV->height = yuv_height;
- enc_pkt.data = NULL;
- enc_pkt.size = 0;
- av_init_packet(&enc_pkt);
- ret = avcodec_encode_video2(pCodecCtx, &enc_pkt, pFrameYUV, &enc_got_frame);
- av_frame_free(&pFrameYUV);
- if (enc_got_frame == 1){
- LOGI("Succeed to encode frame: %5d\tsize:%5d\n", framecnt, enc_pkt.size);
- framecnt++;
- enc_pkt.stream_index = video_st->index;
- //Write PTS
- AVRational time_base = ofmt_ctx->streams[0]->time_base;//{ 1, 1000 };
- AVRational r_framerate1 = {60, 2 };//{ 50, 2 };
- AVRational time_base_q = { 1, AV_TIME_BASE };
- //Duration between 2 frames (us)
- int64_t calc_duration = (double)(AV_TIME_BASE)*(1 / av_q2d(r_framerate1)); //内部时间戳
- //Parameters
- //enc_pkt.pts = (double)(framecnt*calc_duration)*(double)(av_q2d(time_base_q)) / (double)(av_q2d(time_base));
- enc_pkt.pts = av_rescale_q(framecnt*calc_duration, time_base_q, time_base);
- enc_pkt.dts = enc_pkt.pts;
- enc_pkt.duration = av_rescale_q(calc_duration, time_base_q, time_base); //(double)(calc_duration)*(double)(av_q2d(time_base_q)) / (double)(av_q2d(time_base));
- enc_pkt.pos = -1;
- //Delay
- int64_t pts_time = av_rescale_q(enc_pkt.dts, time_base, time_base_q);
- int64_t now_time = av_gettime() - start_time;
- if (pts_time > now_time)
- av_usleep(pts_time - now_time);
- ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
- av_free_packet(&enc_pkt);
- }
- return 0;
- }
- JNIEXPORT jint JNICALL Java_com_zhanghui_test_MainActivity_flush
- (JNIEnv *env, jobject obj)
- {
- int ret;
- int got_frame;
- AVPacket enc_pkt;
- if (!(ofmt_ctx->streams[0]->codec->codec->capabilities &
- CODEC_CAP_DELAY))
- return 0;
- while (1) {
- enc_pkt.data = NULL;
- enc_pkt.size = 0;
- av_init_packet(&enc_pkt);
- ret = avcodec_encode_video2(ofmt_ctx->streams[0]->codec, &enc_pkt,
- NULL, &got_frame);
- if (ret < 0)
- break;
- if (!got_frame){
- ret = 0;
- break;
- }
- LOGI("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n", enc_pkt.size);
- //Write PTS
- AVRational time_base = ofmt_ctx->streams[0]->time_base;//{ 1, 1000 };
- AVRational r_framerate1 = { 60, 2 };
- AVRational time_base_q = { 1, AV_TIME_BASE };
- //Duration between 2 frames (us)
- int64_t calc_duration = (double)(AV_TIME_BASE)*(1 / av_q2d(r_framerate1)); //内部时间戳
- //Parameters
- enc_pkt.pts = av_rescale_q(framecnt*calc_duration, time_base_q, time_base);
- enc_pkt.dts = enc_pkt.pts;
- enc_pkt.duration = av_rescale_q(calc_duration, time_base_q, time_base);
- //转换PTS/DTS(Convert PTS/DTS)
- enc_pkt.pos = -1;
- framecnt++;
- ofmt_ctx->duration = enc_pkt.duration * framecnt;
- /* mux encoded frame */
- ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
- if (ret < 0)
- break;
- }
- //Write file trailer
- av_write_trailer(ofmt_ctx);
- return 0;
- }
- JNIEXPORT jint JNICALL Java_com_zhanghui_test_MainActivity_close
- (JNIEnv *env, jobject obj)
- {
- if (video_st)
- avcodec_close(video_st->codec);
- avio_close(ofmt_ctx->pb);
- avformat_free_context(ofmt_ctx);
- return 0;
- }
- LOCAL_PATH := $(call my-dir)
- # FFmpeg library
- include $(CLEAR_VARS)
- LOCAL_MODULE := avcodec
- LOCAL_SRC_FILES := libavcodec-56.so
- include $(PREBUILT_SHARED_LIBRARY)
- include $(CLEAR_VARS)
- LOCAL_MODULE := avdevice
- LOCAL_SRC_FILES := libavdevice-56.so
- include $(PREBUILT_SHARED_LIBRARY)
- include $(CLEAR_VARS)
- LOCAL_MODULE := avfilter
- LOCAL_SRC_FILES := libavfilter-5.so
- include $(PREBUILT_SHARED_LIBRARY)
- include $(CLEAR_VARS)
- LOCAL_MODULE := avformat
- LOCAL_SRC_FILES := libavformat-56.so
- include $(PREBUILT_SHARED_LIBRARY)
- include $(CLEAR_VARS)
- LOCAL_MODULE := avutil
- LOCAL_SRC_FILES := libavutil-54.so
- include $(PREBUILT_SHARED_LIBRARY)
- include $(CLEAR_VARS)
- LOCAL_MODULE := postproc
- LOCAL_SRC_FILES := libpostproc-53.so
- include $(PREBUILT_SHARED_LIBRARY)
- include $(CLEAR_VARS)
- LOCAL_MODULE := swresample
- LOCAL_SRC_FILES := libswresample-1.so
- include $(PREBUILT_SHARED_LIBRARY)
- include $(CLEAR_VARS)
- LOCAL_MODULE := swscale
- LOCAL_SRC_FILES := libswscale-3.so
- include $(PREBUILT_SHARED_LIBRARY)
- # Program
- include $(CLEAR_VARS)
- LOCAL_MODULE := encode
- LOCAL_SRC_FILES :=encode.c
- LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
- LOCAL_LDLIBS := -llog -lz
- LOCAL_SHARED_LIBRARIES := avcodec avdevice avfilter avformat avutil postproc swresample swscale
- include $(BUILD_SHARED_LIBRARY)
第四步:使用cygwin切换到当前项目的jni目录下,输入ndk-build进行编译即可,如下
此时就可以在当前项目的libs目录下看到ffmpeg所有的类库以及我们自己的类库,我这里是libencode.so,如下
第五步:在java中加载动态库
- static{
- System.loadLibrary("avutil-54");
- System.loadLibrary("swresample-1");
- System.loadLibrary("avcodec-56");
- System.loadLibrary("avformat-56");
- System.loadLibrary("swscale-3");
- System.loadLibrary("postproc-53");
- System.loadLibrary("avfilter-5");
- System.loadLibrary("avdevice-56");
- System.loadLibrary("encode");
- }
至此,就可以在java中调用我们声明的四个方法了。
java部分获取摄像头数据并编码的流程
简单来说,就是利用Camera的PreviewCallback获得摄像头数据,在通过AsyncTask开启后台线程将摄像头数据进行编码。这里为了避免编码过程占用过多的硬件资源,将编码视频宽和高设置为了640和480,。
这里有两个坑,一是onPreviewFrame方法的arg0参数就是YUV数据了,不要再转换为YuvImage对象了,否则会在头部加上一些多余的信息,导致编码错误,二是用于清空缓存帧的flush方法不能在oncreate主线程中调用,否则会导致程序闪退,目前还不清楚原因,所以这里连同close()方法一起放到了onPause方法中进行调用,在安卓开发方面我还是新手,希望有了解这个问题怎么解决的朋友多多指教。
- package com.zhanghui.test;
- import java.io.IOException;
- import android.annotation.TargetApi;
- import android.app.Activity;
- import android.content.pm.PackageManager;
- import android.hardware.Camera;
- import android.os.AsyncTask;
- import android.os.Build;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- @SuppressWarnings("deprecation")
- public class MainActivity extends Activity {
- private static final String TAG= "MainActivity";
- private Button mTakeButton;
- private Camera mCamera;
- private SurfaceView mSurfaceView;
- private SurfaceHolder mSurfaceHolder;
- private boolean isRecording = false;
- private class StreamTask extends AsyncTask<Void, Void, Void>{
- private byte[] mData;
- //构造函数
- StreamTask(byte[] data){
- this.mData = data;
- }
- @Override
- protected Void doInBackground(Void... params) {
- // TODO Auto-generated method stub
- if(mData!=null){
- Log.i(TAG, "fps: " + mCamera.getParameters().getPreviewFrameRate());
- encode(mData);
- }
- return null;
- }
- }
- private StreamTask mStreamTask;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- final Camera.PreviewCallback mPreviewCallbacx=new Camera.PreviewCallback() {
- @Override
- public void onPreviewFrame(byte[] arg0, Camera arg1) {
- // TODO Auto-generated method stub
- if(null != mStreamTask){
- switch(mStreamTask.getStatus()){
- case RUNNING:
- return;
- case PENDING:
- mStreamTask.cancel(false);
- break;
- }
- }
- mStreamTask = new StreamTask(arg0);
- mStreamTask.execute((Void)null);
- }
- };
- mTakeButton=(Button)findViewById(R.id.take_button);
- PackageManager pm=this.getPackageManager();
- boolean hasCamera=pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) ||
- pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT) ||
- Build.VERSION.SDK_INT<Build.VERSION_CODES.GINGERBREAD;
- if(!hasCamera)
- mTakeButton.setEnabled(false);
- mTakeButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- if(mCamera!=null)
- {
- if (isRecording) {
- mTakeButton.setText("Start");
- mCamera.setPreviewCallback(null);
- Toast.makeText(MainActivity.this, "encode done", Toast.LENGTH_SHORT).show();
- isRecording = false;
- }else {
- mTakeButton.setText("Stop");
- initial(mCamera.getParameters().getPreviewSize().width,mCamera.getParameters().getPreviewSize().height);
- mCamera.setPreviewCallback(mPreviewCallbacx);
- isRecording = true;
- }
- }
- }
- });
- mSurfaceView=(SurfaceView)findViewById(R.id.surfaceView1);
- SurfaceHolder holder=mSurfaceView.getHolder();
- holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- holder.addCallback(new SurfaceHolder.Callback() {
- @Override
- public void surfaceDestroyed(SurfaceHolder arg0) {
- // TODO Auto-generated method stub
- if(mCamera!=null)
- {
- mCamera.stopPreview();
- mSurfaceView = null;
- mSurfaceHolder = null;
- }
- }
- @Override
- public void surfaceCreated(SurfaceHolder arg0) {
- // TODO Auto-generated method stub
- try{
- if(mCamera!=null){
- mCamera.setPreviewDisplay(arg0);
- mSurfaceHolder=arg0;
- }
- }catch(IOException exception){
- Log.e(TAG, "Error setting up preview display", exception);
- }
- }
- @Override
- public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
- // TODO Auto-generated method stub
- if(mCamera==null) return;
- Camera.Parameters parameters=mCamera.getParameters();
- parameters.setPreviewSize(640,480);
- parameters.setPictureSize(640,480);
- mCamera.setParameters(parameters);
- try{
- mCamera.startPreview();
- mSurfaceHolder=arg0;
- }catch(Exception e){
- Log.e(TAG, "could not start preview", e);
- mCamera.release();
- mCamera=null;
- }
- }
- });
- }
- @TargetApi(9)
- @Override
- protected void onResume(){
- super.onResume();
- if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.GINGERBREAD){
- mCamera=Camera.open(0);
- }else
- {
- mCamera=Camera.open();
- }
- }
- @Override
- protected void onPause(){
- super.onPause();
- flush();
- close();
- if(mCamera!=null){
- mCamera.release();
- mCamera=null;
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- //JNI
- public native int initial(int width,int height);
- public native int encode(byte[] yuvimage);
- public native int flush();
- public native int close();
- static{
- System.loadLibrary("avutil-54");
- System.loadLibrary("swresample-1");
- System.loadLibrary("avcodec-56");
- System.loadLibrary("avformat-56");
- System.loadLibrary("swscale-3");
- System.loadLibrary("postproc-53");
- System.loadLibrary("avfilter-5");
- System.loadLibrary("avdevice-56");
- System.loadLibrary("encode");
- }
- }
至此,就完成了全部功能的开发,程序截图如下
转自: http://blog.csdn.net/nonmarking/article/details/48601317
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
支付宝扫一扫捐赠
支付宝扫一扫捐赠
微信公众号: 共鸣圈
欢迎讨论,邮件: 924948$qq.com 请把$改成@
QQ群:263132197
QQ: 924948