您的位置:首页 > 其它

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

2015-08-02 21:57 531 查看
这里需要需要炽鸟的SDK开发包(chird文件夹、armeabi文件夹)

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

然后刷新工程



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

activity_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/VideoFrameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--  自定义视频显示控件   -->
<chird.StreamView
android:id="@+id/Video_StreamView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></chird.StreamView>
</FrameLayout>


然后编写主程序
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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androiddemo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

</manifest>


然后连接到模块的WIFI 即可显示出模块摄像头所捕捉到的实时画面
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: