基于DragonBoard 410c的远程家居监控平台三之客户端显示(上)
2017-03-30 13:47
507 查看
一.背景:
继实现基于DragonBoard 410c的实时视频远程无线传输后,博主今天来介绍下android手机的客户端的实现方式(原来是用浏览器打开,但是无法自己进行界面的修改).
二.代码分析
1.代码目录:
图1 代码目录
2.核心代码:
三.实测效果
图2 手机端实测效果
继实现基于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 手机端实测效果
相关文章推荐
- 基于DragonBoard 410c的远程家居监控平台一之远程图像传输(上)
- 基于DragonBoard 410c的远程家居监控平台五之客户端显示(中)
- 基于Dragonboard 410c进行开发的远程遥控机器人(三)
- 基于DragonBoard 410c的远程家居监控平台四之远程图像传输(下)
- 基于Dragonboard 410c平台的机器3D视觉——摄像机标定原理
- 基于qualcomm平台Dragonboard 410c 的LCD driver移植解析
- 基于Dragonboard 410c进行开发的远程遥控机器人(二)
- 【DragonBoard 410c 试用体验】android手机远程监控
- 基于DragonBoard 410c的遥控炮台五之远程交互(下)
- 基于DragonBoard 410c的遥控炮台三之远程交互(上)
- 基于dragonboard 410c的智能魔镜设计(3)——文本消息检索及自动显示
- 基于DragonBoard 410c平台的移动图像监测系统
- 基于DragonBoard 410c的家庭智能环保卫士——(6)kobuki位置信息传输及显示
- 基于Dragonboard 410c的指纹锁(七)
- 基于Dragonboard 410c的自动感应调色灯实现效果演示
- 基于DragonBoard 410c的环保机器人系统一之人机界面(中)
- 基于Dragonboard 410c的音乐储钱罐(二)
- 基于Dragonboard 410c的音乐储钱罐(三)
- 基于dragonboard 410c的机械手臂(四)
- 基于DragonBoard 410c的家庭智能环保卫士——(12)环境数据融合