4
4
import java .util .ArrayList ;
5
5
import java .util .List ;
6
6
7
- import android .content .pm .PackageManager ;
8
7
import android .graphics .SurfaceTexture ;
9
8
import android .hardware .Camera ;
10
9
import android .hardware .Camera .Size ;
@@ -88,22 +87,6 @@ public void run() {
88
87
mCameraHandler = new CameraHandler (Capture .this );
89
88
}
90
89
});
91
-
92
- glView .queueEvent (new Runnable () {
93
- @ Override
94
- public void run () {
95
- mFullScreen = new FullFrameRect (
96
- new Texture2dProgram (Texture2dProgram .ProgramType .TEXTURE_EXT ));
97
- mTextureId = mFullScreen .createTextureObject ();
98
-
99
- mSurfaceTexture = new SurfaceTexture (mTextureId );
100
- mSurfaceTexture .setOnFrameAvailableListener (Capture .this );
101
- // mCameraHandler.sendMessage(mCameraHandler.obtainMessage(CameraHandler.MSG_START_CAMERA, null));
102
- startCameraImpl (0 );
103
- System .out .println ("sent starting message to UI thread" );
104
- prepareFrameBuffers ();
105
- }
106
- });
107
90
}
108
91
109
92
public void setCamera (String camera ) {
@@ -113,56 +96,82 @@ public void setCamera(String camera) {
113
96
selectedCamera = camerasList .indexOf (camera );
114
97
}
115
98
log ("Selected camera = " + selectedCamera );
116
- startCameraImpl (selectedCamera );
99
+ mCameraHandler .sendMessage (mCameraHandler .obtainMessage (
100
+ CameraHandler .MSG_START_CAMERA , new Integer (selectedCamera )));
101
+ mCameraHandler .sendMessage (mCameraHandler .obtainMessage (
102
+ CameraHandler .MSG_SET_SURFACE_TEXTURE , mSurfaceTexture ));
117
103
}
118
-
119
- public void startCameraImpl (int cameraId ) {
120
- try {
121
- mCamera = Camera .open (cameraId );
122
- mCamera .setDisplayOrientation (90 );
123
- startPreview (applet .getSurfaceHolder ());
124
- } catch (Exception e ) {
125
- log ("Couldn't open the Camera" );
126
- e .printStackTrace ();
127
- }
104
+
105
+ public void start () {
106
+ mCameraHandler .sendMessage (mCameraHandler .obtainMessage (
107
+ CameraHandler .MSG_START_PREVIEW ));
128
108
}
129
-
109
+
130
110
public void pause () {
131
111
log ("pause called" );
132
112
if (mCamera != null ) {
133
113
mCamera .release ();
134
114
}
135
- /*
136
- if (mFullScreen != null) {
137
- mFullScreen.release(false); // assume the GLSurfaceView EGL context is about
138
- mFullScreen = null; // to be destroyed
139
- }*/
115
+
116
+ glView .queueEvent (new Runnable () {
117
+ @ Override
118
+ public void run () {
119
+ if (mSurfaceTexture != null ) {
120
+ mSurfaceTexture .release ();
121
+ mSurfaceTexture = null ;
122
+ }
123
+ if (mFullScreen != null ) {
124
+ mFullScreen .release (false ); // assume the GLSurfaceView EGL context is about
125
+ mFullScreen = null ; // to be destroyed
126
+ }
127
+ }
128
+ });
140
129
}
141
-
130
+
142
131
public void resume () {
143
132
log ("resume called" );
144
- if (selectedCamera != -1 ) {
145
- startCameraImpl (selectedCamera );
146
- }
133
+ glView .queueEvent (new Runnable () {
134
+ @ Override
135
+ public void run () {
136
+ mFullScreen = new FullFrameRect (
137
+ new Texture2dProgram (Texture2dProgram .ProgramType .TEXTURE_EXT ));
138
+ mTextureId = mFullScreen .createTextureObject ();
139
+ mSurfaceTexture = new SurfaceTexture (mTextureId );
140
+ mSurfaceTexture .setOnFrameAvailableListener (Capture .this );
141
+ prepareFrameBuffers ();
142
+
143
+ //If camera is not null, the activity was started already and we're coming back from a pause.
144
+ if (mCamera != null ) {
145
+ log ("Starting Camera in resume" );
146
+ mCameraHandler .sendMessage (mCameraHandler .obtainMessage (
147
+ CameraHandler .MSG_START_CAMERA , new Integer (selectedCamera )));
148
+ mCameraHandler .sendMessage (mCameraHandler .obtainMessage (
149
+ CameraHandler .MSG_SET_SURFACE_TEXTURE , mSurfaceTexture ));
150
+ mCameraHandler .sendMessage (mCameraHandler .obtainMessage (
151
+ CameraHandler .MSG_START_PREVIEW ));
152
+ }
153
+ }
154
+ });
147
155
}
148
156
149
157
@ Override
150
158
public void surfaceCreated (SurfaceHolder holder ) {
151
- startPreview ( holder );
159
+ log ( "surfaceCreated" );
152
160
}
153
161
154
162
@ Override
155
163
public void surfaceChanged (SurfaceHolder holder , int format , int width ,
156
164
int height ) {
157
- startPreview ( holder );
165
+ log ( "surfaceChanged" );
158
166
}
159
167
160
168
@ Override
161
169
public void surfaceDestroyed (SurfaceHolder holder ) {
162
- // TODO: Release Camera resources
170
+ log ( "surface destroyed" );
163
171
}
164
172
165
173
public static String [] list () {
174
+ //The following check has to be commented to make list() method static
166
175
// if (applet.getPackageManager().hasSystemFeature(
167
176
// PackageManager.FEATURE_CAMERA)) {
168
177
int nOfCameras = Camera .getNumberOfCameras ();
@@ -183,38 +192,11 @@ public static String[] list() {
183
192
// return null;
184
193
}
185
194
186
- private void startPreview (SurfaceHolder mHolder ) {
187
-
188
- if (mHolder .getSurface () == null ) {
189
- // preview surface does not exist
190
- return ;
191
- }
192
-
193
- // stop preview before making changes
194
- try {
195
- mCamera .stopPreview ();
196
- } catch (Exception e ) {
197
- // ignore: tried to stop a non-existent preview
198
- }
199
-
200
- // set preview size and make any resize, rotate or
201
- // reformatting changes here
202
-
203
- // start preview with new settings
204
- try {
205
- mCamera .setPreviewTexture (mSurfaceTexture );
206
- mCamera .startPreview ();
207
- log ("Started the preview" );
208
- } catch (Exception e ) {
209
- Log .d ("PROCESSING" ,
210
- "Error starting camera preview: " + e .getMessage ());
211
- e .printStackTrace ();
212
- }
213
- }
214
-
215
195
static class CameraHandler extends Handler {
216
196
public static final int MSG_SET_SURFACE_TEXTURE = 0 ;
217
197
public static final int MSG_START_CAMERA = 1 ;
198
+ public static final int MSG_STOP_CAMERA = 2 ;
199
+ public static final int MSG_START_PREVIEW = 3 ;
218
200
219
201
// Weak reference to the Activity; only access this from the UI thread.
220
202
private CameraHandlerCallback callback ;
@@ -238,7 +220,13 @@ public void handleMessage(Message inputMessage) {
238
220
callback .handleSetSurfaceTexture ((SurfaceTexture ) inputMessage .obj );
239
221
break ;
240
222
case MSG_START_CAMERA :
241
- callback .startCamera ();
223
+ callback .startCamera ((Integer ) inputMessage .obj );
224
+ break ;
225
+ case MSG_START_PREVIEW :
226
+ callback .startPreview ();
227
+ break ;
228
+ case MSG_STOP_CAMERA :
229
+ callback .stopCamera ();
242
230
break ;
243
231
default :
244
232
throw new RuntimeException ("unknown msg " + what );
@@ -272,10 +260,54 @@ public static void printCompatibleResolutionsList(Capture capture) {
272
260
public void handleSetSurfaceTexture (SurfaceTexture st ) {}
273
261
274
262
@ Override
275
- public void startCamera () {
263
+ public void startCamera (Integer cameraId ) {
276
264
System .out .println ("Start Camera Impl" );
277
- startCameraImpl (0 );
278
- };
265
+ if (cameraId == null ) {
266
+ cameraId = 0 ;
267
+ }
268
+ try {
269
+ mCamera = Camera .open (cameraId );
270
+ mCamera .setDisplayOrientation (90 );
271
+ } catch (Exception e ) {
272
+ log ("Couldn't open the Camera" );
273
+ e .printStackTrace ();
274
+ }
275
+ }
276
+
277
+ @ Override
278
+ public void stopCamera () {
279
+ if (mCamera != null ) {
280
+ mCamera .release ();
281
+ mCamera = null ;
282
+ }
283
+ }
284
+
285
+ @ Override
286
+ public void startPreview () {
287
+
288
+ if (applet .getSurfaceHolder ().getSurface () == null ) {
289
+ // preview surface does not exist
290
+ return ;
291
+ }
292
+
293
+ // stop preview before making changes
294
+ try {
295
+ mCamera .stopPreview ();
296
+ } catch (Exception e ) {
297
+ // ignore: tried to stop a non-existent preview
298
+ }
299
+
300
+ // start preview with new settings
301
+ try {
302
+ mCamera .setPreviewTexture (mSurfaceTexture );
303
+ mCamera .startPreview ();
304
+ log ("Started the preview" );
305
+ } catch (Exception e ) {
306
+ Log .d ("PROCESSING" ,
307
+ "Error starting camera preview: " + e .getMessage ());
308
+ e .printStackTrace ();
309
+ }
310
+ }
279
311
280
312
@ Override
281
313
public void onFrameAvailable (final SurfaceTexture surfaceTexture ) {
@@ -299,7 +331,7 @@ public void run() {
299
331
pixelBuffer.get(Capture.this.pixels);
300
332
updatePixels(); */
301
333
302
- //Fall back to default frame buffer
334
+ //Fall back to default frame buffer. Not sure if needed.
303
335
GLES20 .glBindFramebuffer (GLES20 .GL_FRAMEBUFFER , 0 );
304
336
}
305
337
});
@@ -366,7 +398,7 @@ public void prepareFrameBuffers() {
366
398
public void getImage (boolean loadPixels ) {
367
399
368
400
if (destpg == null || destpg .width != width || destpg .height != height ) {
369
- destpg = (PGraphicsOpenGL ) parent .createGraphics (width , height , PConstants .P2D );
401
+ destpg = (PGraphicsOpenGL ) applet .createGraphics (width , height , PConstants .P2D );
370
402
destpg .pgl .setGlThread (Thread .currentThread ());
371
403
}
372
404
0 commit comments