android:
id="@+id/monitor"
android:
layout_width="400dp"
android:
layout_height="320dp"/>
android:
layout_width="match_parent"
android:
layout_height="wrap_content"
android:
gravity="center_horizontal"
android:
orientation="horizontal">
android:
id="@+id/tv_state"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="连接状态:
"/>
android:
layout_width="match_parent"
android:
layout_height="200dp"
android:
layout_gravity="center"
android:
gravity="bottom">
android:
id="@+id/center"
android:
layout_width="10dp"
android:
layout_height="10dp"
android:
layout_centerInParent="true"
android:
layout_margin="20dp"
android:
background="#ff0000"/>
android:
id="@+id/ib_left"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_centerVertical="true"
android:
layout_toLeftOf="@id/center"
android:
background="@drawable/left"
android:
contentDescription="@null"/>
android:
id="@+id/ib_right"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_centerVertical="true"
android:
layout_toRightOf="@id/center"
android:
background="@drawable/right"
android:
contentDescription="@null"/>
android:
id="@+id/ib_bottom"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_below="@id/center"
android:
layout_centerHorizontal="true"
android:
background="@drawable/bottom"
android:
contentDescription="@null"/>
android:
id="@+id/ib_top"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_above="@id/center"
android:
layout_centerHorizontal="true"
android:
background="@drawable/top"
android:
contentDescription="@null"/>
添加类库如下图所示,考虑到类库的保密性在该文档中就不再给出下载地址。
核心代码总共有两个Activity,分别是MainActivity和CameraPlayActivity
MainActivity代码如下:
packagecom.example.smartcamera;
importandroid.support.v7.app.ActionBarActivity;
importandroid.text.TextUtils;
importandroid.view.View;
importandroid.widget.EditText;
importandroid.widget.Toast;
importandroid.content.Intent;
importandroid.os.Bundle;
publicclassMainActivityextendsActionBarActivity{
publicstaticfinalStringNAME="name";
publicstaticfinalStringPSW="psw";
publicstaticfinalStringUID="uid";
privateEditTextet_name;
privateEditTextet_pwd;
privateEditTextet_uid;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
/**
*初始化子控件
*/
privatevoidinitView(){
et_name=(EditText)findViewById(R.id.et_devname);
et_pwd=(EditText)findViewById(R.id.et_psw);
et_uid=(EditText)findViewById(R.id.et_uid);
}
/**
*点击Button开始链接
*
*@paramview
*/
publicvoidconnect(Viewview){
Stringname=et_name.getText().toString();
Stringpwd=et_pwd.getText().toString();
Stringuid=et_uid.getText().toString();
if(TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)||TextUtils.isEmpty(uid)){
Toast.makeText(this,"数据不能为空。
",Toast.LENGTH_SHORT).show();
return;
}
Intentintent=newIntent(this,CameraPlayActivity.class);
intent.putExtra(NAME,name);
intent.putExtra(PSW,pwd);
intent.putExtra(UID,uid);
//跳转到播放页面
startActivity(intent);
}
}
CameraPlayActivity代码如下:
packagecom.example.smartcamera;
importcom.tutk.IOTC.AVIOCTRLDEFs;
importcom.tutk.IOTC.AVIOCTRLDEFs.SMsgAVIoctrlPtzCmd;
importcom.tutk.IOTC.Camera;
importcom.tutk.IOTC.IRegisterIOTCListener;
importcom.tutk.IOTC.Monitor;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.graphics.Bitmap;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.ImageButton;
importandroid.widget.TextView;
publicclassCameraPlayActivityextendsActivityimplementsOnClickListener,IRegisterIOTCListener{
privateMonitormonitor;
privateImageButtonib_left;
privateImageButtonib_right;
privateImageButtonib_top;
privateImageButtonib_bottom;
privateTextViewtv_state;
privateStringmName;
privateStringmPsw;
privateStringmUID;
privateCameramCamera;
Handlerhandler=newHandler(){
@Override
publicvoidhandleMessage(android.os.Messagemsg){
switch(msg.what){
//针对不同的连接状态做不同的处理
caseCamera.CONNECTION_STATE_CONNECT_FAILED:
tv_state.setText("连接失败");
break;
//如果是已经连接成功
caseCamera.CONNECTION_STATE_CONNECTED:
tv_state.setText("已连接");
//调用play()方法,完成Camera和Monitor的绑定,最终将画面显示出来
play();
break;
caseCamera.CONNECTION_STATE_CONNECTING:
tv_state.setText("连接中...");
break;
caseCamera.CONNECTION_STATE_DISCONNECTED:
tv_state.setText("未连接");
break;
caseCamera.CONNECTION_STATE_TIMEOUT:
tv_state.setText("连接超时");
break;
caseCamera.CONNECTION_STATE_UNKNOWN_DEVICE:
tv_state.setText("未知设备");
break;
caseCamera.CONNECTION_STATE_UNSUPPORTED:
tv_state.setText("不支持的设备");
break;
caseCamera.CONNECTION_STATE_WRONG_PASSWORD:
tv_state.setText("密码不正确");
break;
caseCamera.CONNECTION_STATE_NONE:
tv_state.setText("未知错误");
break;
default:
tv_state.setText("未知"+msg.what);
break;
}
};
};
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
//初始化控件
initView();
//初始化数据
initData();
//开始连接网络
connectNet();
}
/**
*显示画面的核心方法
*/
protectedvoidplay(){
/**
*如果条件满足则
*1将界面monitor和Camera进行绑定
*2开始播放Camera画面
*
*/
if(mCamera!
=null&&mCamera.isChannelConnected(Camera.DEFAULT_AV_CHANNEL)){
monitor.attachCamera(mCamera,Camera.DEFAULT_AV_CHANNEL);
mCamera.startShow(Camera.DEFAULT_AV_CHANNEL,true);
}
}
privatevoidconnectNet(){
//初始化摄像头内部加载c语言库
Camera.init();
//新创建一个Camera实例不是Android中的Camera而是com.tutk.IOTC.Camera.Camera
mCamera=newCamera();
/**
*给Camera注册IOTC监听
*IOTCInternetofThingsCloud物联网云平台
*该平台是台湾TUTK公司推出,有偿使用,因此SDK不开源
*/
mCamera.registerIOTCListener(this);
/**
*连接到IOTC云平台
*连接传入的是UID
*每个硬件设备出厂时都有唯一的UID编号,并且出厂时已经将该编号注册到IOTC平台服务器
*硬件在连接服务器的时候需要带着自己的身边标识(就是UID)
*/
mCamera.connect(mUID);
/**
*仅仅让硬件连接网络还是不够了,作为终端用户我们想看到云平台上的摄像头画面那么还需要用户
*通过用户名和密码登陆进行身份验证
*
*@parameterCamera.DEFAULT_AV_CHANNEL
*我们可以把一个摄像头硬件理解为一个电视机,那么电视机可以有多个频道,这里我们使用默认频道即可
*@parametermName用户名
*@parametermPsw密码
*
*当开始连接的时候,由于之前注册了Camera监听,因此接收到的数据会以回调的形式传到形参中
*,因此这个时候我们就去IRegisterIOTCListener的回调方法中等数据就行了
*/
mCamera.start(Camera.DEFAULT_AV_CHANNEL,mName,mPsw);
}
/**
*从上一个Activity中获取用户数据
*/
privatevoidinitData(){
Intentintent=getIntent();
mName=intent.getStringExtra(MainActivity.NAME);
mPsw=intent.getStringExtra(MainActivity.PSW);
mUID=intent.getStringExtra(MainActivity.UID);
}
/**
*当前类已经实现了OnClickListener接口因此绑定点击事件只需要传递this即可
*/
privatevoidinitView(){
monitor=(Monitor)findViewById(R.id.monitor);
ib_left=(ImageButton)findViewById(R.id.ib_left);
ib_right=(ImageButton)findViewById(R.id.ib_right);
ib_top=(ImageButton)findViewById(R.id.ib_top)