/
MainActivity.java
342 lines (306 loc) · 11.1 KB
/
MainActivity.java
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
* Actividad de prueba que enmarca y controla tanto la vista preliminar de la cámara como la
* interfaz ICE publicada.
*
* @author Óscar Javier García Baudet
* @version 1.0, 2013-01-08
*/
package org.jderobot.androidcameraserver;
import org.jderobot.androidcameraserver.R;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLayoutChangeListener;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends Activity implements OnLayoutChangeListener {
private static final String TAG = MainActivity.class.getClass().getName();
/** Instance of ImageProvider implementation to be published */
private CameraI cameraA;
/** Instance of camera to be used in activity */
static private Camera mCamera;
/** Instance of implementation of the camera preview that we'll use */
private CameraPreview mPreview;
/** Handler for autofocus when camera doesn't support it */
private Handler autoFocusHandler;
/** Previewing state */
private boolean previewing = false;
/** Frame layout what contents surface preview */
private FrameLayout preview;
private SharedPreferences prefs;
private String adapterendpoints = " -h 0.0.0.0 -p ";
private String port = "9999";
private String protocol = "default";
private int width;
private int height;
private String dimensions;
PowerManager.WakeLock wl;
/**
* Prepare and configure activity
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* We require landscape orientation */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
/* Provide continuous autofocus if camera does not support it */
autoFocusHandler = new Handler();
prefs = PreferenceManager.getDefaultSharedPreferences(this);
// Get the value for port and protocol
port = prefs.getString("Port Number", "9999");
protocol = prefs.getString("protocol", "default");
// SSL initialization can take some time. To avoid blocking the
// calling thread, we perform the initialization in a separate thread.
new Thread(new Runnable() {
public void run() {
initializeCommunicator();
}
}).start();
}
/**
* Safe way to get the instance of camera
*/
public static Camera getCameraInstance() {
Camera camara = null;
try {
/* Open the camera */
camara = Camera.open();
} catch (Exception e) {
Log.e(TAG, "No se pudo inicializar la cámara");
}
return camara;
}
/**
* Release all camera resources
*/
private void releaseCamera() {
if (mCamera != null) {
/* Disable callbacks */
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.lock();
mCamera.release();
mCamera = null;
/* Save the state */
previewing = false;
}
}
private Runnable doAutoFocus = new Runnable() {
public void run() {
if (previewing)
mCamera.autoFocus(autoFocusCB);
}
};
PreviewCallback previewCb = new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
previewing = true;
try {
if (CameraI.idDatos == null)
return;
Camera.Parameters parameters = camera.getParameters();
Size size = parameters.getPreviewSize();
switch (parameters.getPreviewFormat()) {
case ImageFormat.NV21:
CameraI.idDatos.format = "NV21";
break;
case ImageFormat.YUY2:
CameraI.idDatos.format = "YUY2";
default:
CameraI.idDatos.format = "YCRCB";
}
CameraI.idDatos.width = size.width;
CameraI.idDatos.height = size.height;
CameraI.idImagen.pixelData = data.clone();
long milisegundos = System.currentTimeMillis();
CameraI.idImagen.timeStamp.seconds = (long) (milisegundos / 1000);
CameraI.idImagen.timeStamp.useconds = (milisegundos % 1000) * 1000;
new Thread(new Runnable() {
public void run() {
CameraI.Job actual;
while ((actual = CameraI._jobs.poll()) != null) {
actual.execute();
}
}
}).start();
/*
* CameraI.Job actual; while ((actual = CameraI._jobs.poll()) != null) { actual.execute(); }
*/
} catch (Exception ex) {
Log.e(TAG, "Prueba");
}
}
};
AutoFocusCallback autoFocusCB = new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
autoFocusHandler.postDelayed(doAutoFocus, 3500);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
Intent i = new Intent(this, Preferences.class);
startActivity(i);
break;
}
return true;
}
/**
* When the layout is about to change (size is calculated previously) use the size of parent frame
* to calculate own size to keep preview surface's aspect ratio
*/
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft,
int oldTop, int oldRight, int oldBottom) {
/* Get parent layout (relative frame) to know total application size */
RelativeLayout rootLayout = (RelativeLayout) v.getParent();
/* Avoid calling this event listener again */
v.removeOnLayoutChangeListener(this);
/* Get preview frame parameters to change it to keep aspect ratio */
RelativeLayout.LayoutParams layoutPreviewParams =
(RelativeLayout.LayoutParams) preview.getLayoutParams();
/* Calculations needed to keep camera preview aspect ratio */
double rootAspectRatio = (double) rootLayout.getWidth() / (double) rootLayout.getHeight();
double previewAspectRatio = (double) width / (double) height;
if (rootAspectRatio > previewAspectRatio) {
/* The height will be the same as parent */
layoutPreviewParams.height = rootLayout.getHeight();
/* We need to calculate width to keep aspect ratio */
layoutPreviewParams.width = (int) ((double) rootLayout.getHeight() * previewAspectRatio);
} else {
/* The width will be the same as parent */
layoutPreviewParams.width = rootLayout.getWidth();
/* We need to calculate height to keep aspect ratio */
layoutPreviewParams.height = (int) ((double) rootLayout.getWidth() * previewAspectRatio);
}
/* Tell frame to center in parent relative frame */
layoutPreviewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
preview.setLayoutParams(layoutPreviewParams);
}
public void onStop() {
super.onStop();
}
public void onPause() {
releaseCamera();
preview.removeAllViews();
mPreview = null;
Log.e(TAG, wl.isHeld() + "");
if (wl.isHeld() == true) {
wl.release();
}
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
/* Test if preferences was modified */
if (Preferences.modified) {
/* Clear modified flag */
Preferences.modified = false;
/* Finish this Activity and reload it again */
Toast.makeText(getApplicationContext(), R.string.application_reload, Toast.LENGTH_LONG).show();
this.finish();
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
/* Get an instance of the default camera */
mCamera = getCameraInstance();
if (mCamera == null) {
Toast.makeText(getApplicationContext(), R.string.error_camera, Toast.LENGTH_LONG).show();
this.finish();
}
dimensions = prefs.getString("listpref", "320 240");
width = Integer.parseInt(dimensions.substring(0, dimensions.indexOf(" ")));
height =
Integer.parseInt(dimensions.substring(dimensions.indexOf(" ") + 1, dimensions.length()));
/* We create an instance of CameraPreview to manage the camera */
mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB, width, height);
/* Find the frame that will contain the camera preview */
preview = (FrameLayout) findViewById(R.id.frameLayout);
/* Change preview size when it have been displayed */
preview.addOnLayoutChangeListener(this);
/* Add view to frame */
preview.addView(mPreview);
// Get the port and protocol
port = prefs.getString("Port Number", "9999");
protocol = prefs.getString("protocol", "default");
// Check wakelock and lockscreen
if (prefs.getBoolean("lockscreen", false) == true) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
if (prefs.getBoolean("wakelock", false) == true) {
wl.acquire();
}
// Initialize the Communicator again as ports and protocol have been
// changed
new Thread(new Runnable() {
public void run() {
initializeCommunicator();
}
}).start();
}
/* Implementation of ICE */
interface CommunicatorCallback {
void onWait();
void onCreate(Ice.Communicator communicator);
void onError(Ice.LocalException ex);
}
private Ice.Communicator _communicator;
private CommunicatorCallback _cb;
private void initializeCommunicator() {
try {
Ice.Communicator communicator;
Ice.InitializationData initData = new Ice.InitializationData();
initData.properties = Ice.Util.createProperties();
initData.properties.setProperty("Ice.Trace.Network", "3");
communicator = Ice.Util.initialize(initData);
Ice.ObjectAdapter adapter =
communicator.createObjectAdapterWithEndpoints("CameraAdapter", protocol
+ adapterendpoints + port);
cameraA = new CameraI();
adapter.add((Ice.Object) cameraA, Ice.Util.stringToIdentity("cameraA"));
adapter.activate();
synchronized (this) {
_communicator = communicator;
if (_cb != null) {
_cb.onCreate(_communicator);
}
}
} catch (Ice.LocalException ex) {
synchronized (this) {
if (_cb != null) {
_cb.onError(ex);
}
}
}
}
}