Skip to content

Commit e0c391a

Browse files
committed
Prefectly comply with activity lifecycle events
Signed-off-by: Umair Khan <[email protected]>
1 parent d81f388 commit e0c391a

File tree

2 files changed

+111
-77
lines changed

2 files changed

+111
-77
lines changed

src/in/omerjerk/processing/video/android/CameraHandlerCallback.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44

55
public interface CameraHandlerCallback {
66
public void handleSetSurfaceTexture(SurfaceTexture texture);
7-
public void startCamera();
7+
public void startCamera(Integer cameraId);
8+
public void startPreview();
9+
public void stopCamera();
810
}

src/in/omerjerk/processing/video/android/Capture.java

Lines changed: 108 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.util.ArrayList;
55
import java.util.List;
66

7-
import android.content.pm.PackageManager;
87
import android.graphics.SurfaceTexture;
98
import android.hardware.Camera;
109
import android.hardware.Camera.Size;
@@ -88,22 +87,6 @@ public void run() {
8887
mCameraHandler = new CameraHandler(Capture.this);
8988
}
9089
});
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-
});
10790
}
10891

10992
public void setCamera(String camera) {
@@ -113,56 +96,82 @@ public void setCamera(String camera) {
11396
selectedCamera = camerasList.indexOf(camera);
11497
}
11598
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));
117103
}
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));
128108
}
129-
109+
130110
public void pause() {
131111
log("pause called");
132112
if (mCamera != null) {
133113
mCamera.release();
134114
}
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+
});
140129
}
141-
130+
142131
public void resume() {
143132
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+
});
147155
}
148156

149157
@Override
150158
public void surfaceCreated(SurfaceHolder holder) {
151-
startPreview(holder);
159+
log("surfaceCreated");
152160
}
153161

154162
@Override
155163
public void surfaceChanged(SurfaceHolder holder, int format, int width,
156164
int height) {
157-
startPreview(holder);
165+
log("surfaceChanged");
158166
}
159167

160168
@Override
161169
public void surfaceDestroyed(SurfaceHolder holder) {
162-
// TODO: Release Camera resources
170+
log("surface destroyed");
163171
}
164172

165173
public static String[] list() {
174+
//The following check has to be commented to make list() method static
166175
// if (applet.getPackageManager().hasSystemFeature(
167176
// PackageManager.FEATURE_CAMERA)) {
168177
int nOfCameras = Camera.getNumberOfCameras();
@@ -183,38 +192,11 @@ public static String[] list() {
183192
// return null;
184193
}
185194

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-
215195
static class CameraHandler extends Handler {
216196
public static final int MSG_SET_SURFACE_TEXTURE = 0;
217197
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;
218200

219201
// Weak reference to the Activity; only access this from the UI thread.
220202
private CameraHandlerCallback callback;
@@ -238,7 +220,13 @@ public void handleMessage(Message inputMessage) {
238220
callback.handleSetSurfaceTexture((SurfaceTexture) inputMessage.obj);
239221
break;
240222
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();
242230
break;
243231
default:
244232
throw new RuntimeException("unknown msg " + what);
@@ -272,10 +260,54 @@ public static void printCompatibleResolutionsList(Capture capture) {
272260
public void handleSetSurfaceTexture(SurfaceTexture st) {}
273261

274262
@Override
275-
public void startCamera() {
263+
public void startCamera(Integer cameraId) {
276264
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+
}
279311

280312
@Override
281313
public void onFrameAvailable(final SurfaceTexture surfaceTexture) {
@@ -299,7 +331,7 @@ public void run() {
299331
pixelBuffer.get(Capture.this.pixels);
300332
updatePixels(); */
301333

302-
//Fall back to default frame buffer
334+
//Fall back to default frame buffer. Not sure if needed.
303335
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
304336
}
305337
});
@@ -366,7 +398,7 @@ public void prepareFrameBuffers() {
366398
public void getImage(boolean loadPixels) {
367399

368400
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);
370402
destpg.pgl.setGlThread(Thread.currentThread());
371403
}
372404

0 commit comments

Comments
 (0)