WIFI视频实时预览功能的实现


这里需要需要炽鸟的SDK开发包(chird文件夹、armeabi文件夹)


首先新建一个工程  这里我取名为 AndroidDemo  然后将chird文件夹放在src文件夹下   将armeabi文件夹放在lib文件夹下 

然后刷新工程


WIFI视频实时预览功能的实现_第1张图片


然后在布局文件中添加自定义视频的显示控件

activity_layout.xml



 		
	


然后编写主程序

Mainactivity.java

package com.example.androiddemo;

import chird.StreamView;
import chird.chd_wmp_apis;
import chird.st_VideoFrame;
import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;


	public class MainActivity extends ActionBarActivity {
		private chd_wmp_apis chd_wmp = new chd_wmp_apis();
		private long m_handle = 0;
		public StreamView m_StreamView;
		private Bitmap m_Bitmap  = null; // yuv显示给jni赋值操作
		private boolean m_VideoRunFlag = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //全屏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//屏幕亮
        setContentView(R.layout.activity_main);
        
        int width = getWindowManager().getDefaultDisplay().getWidth();
        int height= getWindowManager().getDefaultDisplay().getHeight();
        m_StreamView = (StreamView) findViewById(R.id.Video_StreamView);
        m_StreamView.SetActivity(width, height);
        m_handle = chd_wmp.CHD_WMP_ConnectDevice("192.168.100.254");
        if (m_handle < 0){
	        Toast toast=Toast.makeText(getApplicationContext(),"警告:连接失败", Toast.LENGTH_SHORT);
	        toast.show();
        }
        int ret = chd_wmp.CHD_WMP_Video_Begin(m_handle);
        if (ret < 0){
	        Toast toast=Toast.makeText(getApplicationContext(), "警告:无法打开视频流", Toast.LENGTH_SHORT);
	        toast.show();
        }
        m_VideoRunFlag = true;
        chd_demo_poll_thread thread = new chd_demo_poll_thread();
        thread.start();
   }
    
    class chd_demo_poll_thread extends Thread{
    	int ret = 0, width = 0, height = 0;
    	byte [] buffer = new byte[5*1024*1024];


    	public void run(){
	    	st_VideoFrame vframe = new st_VideoFrame();
	    	while (m_VideoRunFlag){
		    	ret = chd_wmp.CHD_WMP_Poll(m_handle, 2, 0);
		    	if (ret < 0) continue;
		    	Log.v("test", "poll ret:"+ret);
		    	
		    	if (ret == chd_wmp.CHD_POLL_TYPE_VIDEO){
			    	chd_wmp.CHD_WMP_Video_RequestVideoData(m_handle,vframe, buffer);
			    	
			    	if (vframe.width != width || vframe.height != height){
						try{
							m_Bitmap = Bitmap.createBitmap(vframe.width, vframe.height, Config.ARGB_8888);
						}catch(OutOfMemoryError e){
							Toast toast=Toast.makeText(getApplicationContext(), "内存异常。", Toast.LENGTH_SHORT);
							toast.show();
							
							continue;
						}
						width 		= vframe.width;
						height		= vframe.height;
					}
			    	
			    	if (vframe.format == vframe.CHD_FMT_MJPEG)
			    		ret= chd_wmp.CHD_WMP_Decode_MjpegToBitmap(m_handle,vframe.width,vframe.height,vframe.datalen,m_Bitmap,buffer);
			    	else if(vframe.format==vframe.CHD_FMT_YUYV)
			    		ret=chd_wmp.CHD_WMP_Decode_YuyvToBitmap(m_handle,vframe.width, vframe.height,m_Bitmap,buffer);
			    	else ret= -1;
			    	
			    	if (ret == 0)
			    		m_StreamView.ShowBitmapFrame(m_Bitmap);
		    	}
	    	}
    	}
    }
}


然后添加网络权限

AndroidManifest.xml




    

    
        
            
                

                
            
        
    
    
	
	
	
	
	
 	
	
	
    


然后连接到模块的WIFI  即可显示出模块摄像头所捕捉到的实时画面


你可能感兴趣的:(WIFI视频实时预览功能的实现)