您的位置:首页 > 运维架构

基于DragonBoard 410c的远程家居监控平台三之客户端显示(上)

2017-03-30 13:47 507 查看
一.背景:

继实现基于DragonBoard 410c的实时视频远程无线传输后,博主今天来介绍下android手机的客户端的实现方式(原来是用浏览器打开,但是无法自己进行界面的修改).

二.代码分析

1.代码目录:




图1 代码目录
2.核心代码:



package com.czy;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URL;
import java.util.Hashtable;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class client extends Activity  {
private Button conBtn = null;
private Button setBtn = null;
private Button qian = null;
private Button hou = null;
private Button zuo = null;
private Button you = null;
private Button ting = null;
private Button shang = null;
private Button xia = null;

private Context mContext = null;
private ImageView imView = null;
private Handler messageHandler =null;
private Boolean isStop = true;
//private String conStr = "http://192.168.0.101:8080/?action=stream";
private String conStr = "http://192.168.23.2:8080/?action=stream";
private HttpRequest http = null;
private String cmdPid = "";

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imView = (ImageView)findViewById(R.id.imageView1);
conBtn = (Button) findViewById(R.id.lianjie);
setBtn = (Button) findViewById(R.id.button1);
qian = (Button) findViewById(R.id.qian);;
hou = (Button) findViewById(R.id.hou);;
zuo = (Button) findViewById(R.id.zuo);;
you = (Button) findViewById(R.id.you);;
ting = (Button) findViewById(R.id.ting);;
shang = (Button) findViewById(R.id.shang);;
xia = (Button) findViewById(R.id.xia);;

mContext = this;
http = new HttpRequest();

qian.setOnClickListener(new OnClickListener() {public void onClick(View v) {SendCmd("w");}});
hou.setOnClickListener(new OnClickListener() {public void onClick(View v) {SendCmd("x");}});
shang.setOnClickListener(new OnClickListener() {public void onClick(View v) {SendCmd("z");}});
xia.setOnClickListener(new OnClickListener() {public void onClick(View v) {SendCmd("c");}});
zuo.setOnClickListener(new OnClickListener() {public void onClick(View v) {SendCmd("a");}});
you.setOnClickListener(new OnClickListener() {public void onClick(View v) {SendCmd("d");}});
ting.setOnClickListener(new OnClickListener() {public void onClick(View v) {SendCmd("s");}});

setBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Setting();
}
});

conBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(isStop){
isStop = false;
conBtn.setText("Clo");
new Thread() {
@SuppressWarnings("unchecked")
public void run() {
try {
URL url =new URL(conStr);

Socket server = new Socket(url.getHost(), url.getPort());
OutputStream os = server.getOutputStream();
InputStream  is = server.getInputStream();

StringBuffer request = new StringBuffer();
request.append("GET " + url.getFile() + " HTTP/1.0\r\n");
request.append("Host: " + url.getHost() + "\r\n");
request.append("\r\n");
os.write(request.toString().getBytes(), 0, request.length());

StreamSplit localStreamSplit = new StreamSplit(new DataInputStream(new BufferedInputStream(is)));
Hashtable localHashtable = localStreamSplit.readHeaders();

String str3 = (String)localHashtable.get("content-type");
int n = str3.indexOf("boundary=");
Object localObject2 = "--";
if (n != -1){
localObject2 = str3.substring(n + 9);
str3 = str3.substring(0, n);
if (!((String)localObject2).startsWith("--"))
localObject2 = "--" + (String)localObject2;
}
if (str3.startsWith("multipart/x-mixed-replace")){
localStreamSplit.skipToBoundary((String)localObject2);
}
Message message1 = Message.obtain();
message1.arg1 = 1;
messageHandler.sendMessage(message1);
do{
if (localObject2 != null){
localHashtable = localStreamSplit.readHeaders();
if (localStreamSplit.isAtStreamEnd())
break;
str3 = (String)localHashtable.get("content-type");
if (str3 == null)
throw new Exception("No part content type");
}
if (str3.startsWith("multipart/x-mixed-replace")){
n = str3.indexOf("boundary=");
localObject2 = str3.substring(n + 9);
localStreamSplit.skipToBoundary((String)localObject2);
}else{
byte[] localObject3 = localStreamSplit.readToBoundary((String)localObject2);
if (localObject3.length == 0)
break;

Message message = Message.obtain();
message.arg1 = 0;
message.obj = BitmapFactory.decodeByteArray(localObject3, 0, localObject3.length);
messageHandler.sendMessage(message);
}
try{
Thread.sleep(10L);
}catch (InterruptedException localInterruptedException){

}
}while (!isStop);
server.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("错误");
Toast.makeText(mContext, "无法连接上服务器!", Toast.LENGTH_SHORT).show();
Message message = Message.obtain();
message.arg1 = 1;
messageHandler.sendMessage(message);
}
}
}.start();
}else{
isStop = true;
conBtn.setText("Con");
}
}
});

Looper looper = Looper.myLooper();
messageHandler = new MessageHandler(looper);
}

public void SendCmd(final String cmd){
new Thread() {
public void run() {
try {
String httpStr = "http://"+(new URL(conStr)).getHost()+"/cgi/action.cgi?cmd=";
if(cmdPid == ""){
String result = http.doGet(httpStr+"p");
cmdPid = (result.substring(result.indexOf(">") + 1, result.lastIndexOf("<"))).trim();
}
if(cmdPid.equals("")){
return;
}
http.doGet(httpStr+cmd+"&pid="+cmdPid);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}

class MessageHandler extends Handler {
public MessageHandler(Looper looper) {
super(looper);
}
public void handleMessage(Message msg) {
switch (msg.arg1) {
case 0:
imView.setImageBitmap((Bitmap)msg.obj);
break;
default:
break;
}

}
}

public void Setting() {
LayoutInflater factory=LayoutInflater.from(mContext);
final View v1=factory.inflate(R.layout.setting,null);
AlertDialog.Builder dialog=new AlertDialog.Builder(mContext);
dialog.setTitle("连接地址")
4000
;
dialog.setView(v1);
EditText et = (EditText)v1.findViewById(R.id.connectionurl);
et.setText(conStr);
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
EditText qet = (EditText)v1.findViewById(R.id.connectionurl);
conStr = qet.getText().toString();
Toast.makeText(mContext, "设置成功!", Toast.LENGTH_SHORT).show();
}
});
dialog.setNegativeButton("取消",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

}
});
dialog.show();
}

}


三.实测效果





图2 手机端实测效果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: