summaryrefslogtreecommitdiff
path: root/roombacomm-client/src/com/hackingroomba/roombacomm/watchVideo.java
blob: 978459adccf522d8274e537f07e66c897ea0d59a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * roombacomm.WatchVideo -- test out the video subsystem without robot motion
 *
 *  Copyright (c) 2009 Paul Bouchier, bouchier@at@classicnet.net
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General
 *  Public License along with this library; if not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 *  Boston, MA  02111-1307  USA
 *
 */
package com.hackingroomba.roombacomm;

import jargs.gnu.CmdLineParser;
import java.io.*;


/**	
	    Run it with something like: <pre>
	    java roombacomm.WatchVideo --videoServer 192.168.0.150 --videoPortNum 5005 -x 160 -y 120 --debug<br>
     Usage:
       roombacomm.WatchVideo --videoServer <IP> --videoPortNum <port> 
         -x <image X size> -y <image Y size> [options]<br>
     where 
     [options] can be one or more of:
      -debug       -- turn on debug output
      -hwhandshake -- use hardware-handshaking, for Windows Bluetooth
      -nohwhandshake -- don't use hardware-handshaking
	   </pre>
*/	 
	public class watchVideo {
	    
		private int height = 120;
		private int width = 160;
		String usage = 
	        "Usage: \n"+
	        "  roombacomm.Roborama --videoServer <IP> --videoPortNum <port> -x <image X size> -y <image Y size>[options]\n" +
	        "where [options] can be one or more of:\n"+
	        " -X | --debug       -- turn on debug output\n"+
	        " -x <width> or --width <width>: set width\n" +
	        " -y <height> or --height <height> -- set height\n" +
	        " -c | --color -- use color mode, otherwise grayscale\n" +
	        " --videoserver <IP> : set IP address of video server\n" +
	        " --videoPortNum <port> : set port number on video server\n" +
	        " -t | --threshold <threshold> : set quantization threshold\n" +
	        " -R | --RoboRealm : connect to Roborealm & send image to it\n" +
	        " -hwhandshake -- use hardware-handshaking, for Windows Bluetooth\n";

	    boolean debug = false;
		private int waittime;
		private int thresholdOverride = 0;
		private String videoServer = "";
		private int videoPortNum = 5005 ;
		private int videoRowStart = 50;
		private int videoRowEnd = 54;
    	private int pixelCnt;
		private boolean color = false;		// when true, capture images in color
		private boolean roborealm = false; 	// when true, connect & send images to RoboRealm
		FrameProcessor fp;
	    
		public watchVideo() {
			// constructor, mustn't throw exceptions. Do nothing for now
		}
		
		// main - it all starts here
		public static void main(String[] args) {
			watchVideo b = new watchVideo();
			b.parseCmd(args);
        	b.initVideo();
			b.displayVideo();
		}
		
		public void displayVideo()
		{
        	
        	while (true) {
	        	getShowFrame();
	        	if (roborealm) {
	        		Boolean rv = fp.frame2Roborealm();	
	        		if (rv == false)
	        			System.out.println("error sending image to Roborealm");
	        		//fp.getShapeData();
	        	}
        	}
        	//frameSize = fp.readFrame();
        	//System.out.println("getVideo read " + frameSize + " bytes");
        	//fp.disconnect();
		}

		/**
		 * Get a frame from the video server and display it
		 */
		public void getShowFrame() {
			pixelCnt = fp.readFrame(getWidth(), getHeight(), color?1:0);
			if (pixelCnt != (getWidth() * getHeight())) {
				System.out.println("getVideo read " + pixelCnt + " bytes - abandoning frame");
				return;
			}

			// output the frame to wherever it's wanted
			fp.displayFrame();
			return;
		}

		/**
		 * Connect to the video server, and to RoboRealm if requested
		 */
		public FrameProcessor initVideo() {
			if (getVideoServer() == null || getVideoServer().length() == 0) {
        		System.err.println(" you must supply a --videoServer value to use the command \"getVideo\"");
        		if (getVideoPortNum() <= 0) {
	        		System.err.println(" you must supply a --videoPortNum value to use the command \"getVideo\"");
        		}
        		System.exit(6);
        	}
        	if (getVideoPortNum() <= 0) {
        		System.err.println(" you must supply a --videoPortNum value to use the command \"getVideo\"");
        		System.exit(7);
        	}

        	fp = new FrameProcessor(getVideoServer(),getVideoPortNum(), 0, 0, 0, 0, getThresholdOverride());
        	//fp.testQuantization();
        	fp.createAndShowGUI();
        	fp.connect(roborealm);		// connect to the video server, and to Roborealm too if requested
        	
        	return fp;
		}
		
		public void parseCmd(String[] args){
			System.out.println("*** start of WatchVideo WparseCmd");

	        CmdLineParser parser = new CmdLineParser();
	        CmdLineParser.Option debugOption = parser.addBooleanOption('X', "debug");
//	        CmdLineParser.Option verboseOption = parser.addBooleanOption('W', "Verbose");
	        CmdLineParser.Option widthOption = parser.addIntegerOption('x', "width");
	        CmdLineParser.Option heightOption = parser.addIntegerOption('y', "height");
	        CmdLineParser.Option videoServerOption = parser.addStringOption("videoServer");
	        CmdLineParser.Option videoPortNumOption = parser.addIntegerOption("videoPortNum");
	        CmdLineParser.Option thresholdOption = parser.addIntegerOption('t', "threshold");
	        CmdLineParser.Option hwHandShakeOption = parser.addBooleanOption("nohwhandshake");
	        CmdLineParser.Option colorOption = parser.addBooleanOption('c', "color");
	        CmdLineParser.Option roborealmOption = parser.addBooleanOption('R', "roborealm");
	        try {
	            parser.parse(args);
	        }
	        catch ( CmdLineParser.OptionException e ) {
	            System.err.println(e.getMessage());
	            System.out.println("parseCmd had an error\n"+ usage );
	            System.exit(2);
	        }	        
	        
	        //	        String portname = args[0];  // e.g. "/dev/cu.KeySerial1", or "COM5" or "192.168.1.1"
	        setThresholdOverride(((Integer)parser.getOptionValue(thresholdOption, getThresholdOverride())).intValue());
	        setWidth(((Integer)parser.getOptionValue(widthOption,getWidth())).intValue());
	        setHeight(((Integer)parser.getOptionValue(heightOption,getHeight())).intValue());
	        setVideoServer(((String)parser.getOptionValue(videoServerOption)));
	        setVideoPortNum(((Integer)parser.getOptionValue(videoPortNumOption, getVideoPortNum())).intValue());
			//	        String cmd = args[1+argOffset];

	        Boolean debugBool = (Boolean)parser.getOptionValue(debugOption,new Boolean(false));
	        setDebug(debugBool.booleanValue());
	        System.out.println("debug is ("+isDebug()+")");
	        Boolean hwHandShakeBool = (Boolean)parser.getOptionValue(hwHandShakeOption, new Boolean(false));
	        setThresholdOverride((Integer)parser.getOptionValue(thresholdOption, new Integer(0)));
	        System.out.println("thresholdOverride is " + getThresholdOverride());
	        color = (Boolean)parser.getOptionValue(colorOption, new Boolean(false));
	        roborealm = (Boolean)parser.getOptionValue(roborealmOption, new Boolean(false));
	        System.out.println("color mode is " + color + ", roborealm mode is " + roborealm);
	        System.out.println("*** end of parseCmd");
	    }

		public boolean isDebug() {
			return debug;
		}
		public void setDebug(boolean debug_) {
			debug = debug_;
		}
		public int getWaittime() {
			return waittime;
		}
		public void setWaittime(int waittime_) {
			waittime = waittime_;
		}
		public int getThresholdOverride() {
			return thresholdOverride;
		}
		public void setThresholdOverride(int thresholdOverride) {
			this.thresholdOverride = thresholdOverride;
		}
		protected int getHeight() {
			return height;
		}
		protected void setHeight(int height) {
			this.height = height;
		}
		protected int getWidth() {
			return width;
		}
		protected void setWidth(int width) {
			this.width = width;
		}
		/**
		 * @return the videoServer
		 */
		protected String getVideoServer() {
			return videoServer;
		}
		/**
		 * @param videoServer the videoServer to set
		 */
		protected void setVideoServer(String videoServer) {
			this.videoServer = videoServer;
		}
		/**
		 * @return the videoPortNum
		 */
		protected int getVideoPortNum() {
			return videoPortNum;
		}
		/**
		 * @param videoPortNum the videoPortNum to set
		 */
		protected void setVideoPortNum(int videoPortNum) {
			this.videoPortNum = videoPortNum;
		}

		public boolean isColor() {
			return color;
		}

		public void setColor(boolean color) {
			this.color = color;
		}

		public boolean isRoborealm() {
			return roborealm;
		}

		public void setRoborealm(boolean roborealm) {
			this.roborealm = roborealm;
		}
		
	}