Skip to content

Commit e943e64

Browse files
committed
Use own SurfaceTexture object (later copy data to Processing's base texture)
Signed-off-by: Umair Khan <[email protected]>
1 parent 862caa9 commit e943e64

File tree

2 files changed

+48
-29
lines changed

2 files changed

+48
-29
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
public interface CameraHandlerCallback {
66
public void handleSetSurfaceTexture(SurfaceTexture texture);
7+
public void startCamera();
78
}

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

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
import android.view.SurfaceHolder;
1515
import processing.core.PConstants;
1616
import processing.core.PApplet;
17+
import processing.core.PImage;
1718
import processing.opengl.PGraphicsOpenGL;
1819

1920
@SuppressWarnings("deprecation")
20-
public class Capture extends PGraphicsOpenGL implements PConstants,
21+
public class Capture extends PImage implements PConstants,
2122
SurfaceHolder.Callback, CameraHandlerCallback, SurfaceTexture.OnFrameAvailableListener {
2223

2324
private static final boolean DEBUG = true;
@@ -30,25 +31,26 @@ public static void log(String log) {
3031
private PApplet applet;
3132

3233
private Camera mCamera;
33-
private Camera.Parameters parameters;
34-
35-
private int previewWidth, previewHeight;
3634

3735
private static ArrayList<String> camerasList = new ArrayList<String>();
3836

3937
private static final String KEY_FRONT_CAMERA = "front-camera-%d";
4038
private static final String KEY_BACK_CAMERA = "back-camera-%d";
4139

4240
private int selectedCamera = -1;
43-
41+
42+
private GLSurfaceView glView;
4443
private SurfaceTexture mSurfaceTexture;
44+
private FullFrameRect mFullScreen;
45+
private int mTextureId;
46+
47+
private CameraHandler mCameraHandler;
4548

4649
public Capture(PApplet context) {
4750
this(context, -1, -1);
4851
}
4952

5053
public Capture(final PApplet applet, int width, int height) {
51-
super();
5254
this.applet = applet;
5355
if (width == -1 || height == -1) {
5456
//TODO: Temp hack. Needs to be handled intelligently.
@@ -60,15 +62,27 @@ public Capture(final PApplet applet, int width, int height) {
6062
}
6163
applet.registerMethod("pause", this);
6264
applet.registerMethod("resume", this);
63-
setParent(applet);
64-
setPrimary(false);
65-
initOffscreen();
66-
if (texture != null) {
67-
mSurfaceTexture = new SurfaceTexture(texture.glName);
68-
mSurfaceTexture.setOnFrameAvailableListener(this);
69-
} else {
70-
log("null texture");
71-
}
65+
glView = (GLSurfaceView) applet.getSurfaceView();
66+
applet.runOnUiThread(new Runnable() {
67+
@Override
68+
public void run() {
69+
mCameraHandler = new CameraHandler(Capture.this);
70+
}
71+
});
72+
73+
glView.queueEvent(new Runnable() {
74+
@Override
75+
public void run() {
76+
mFullScreen = new FullFrameRect(
77+
new Texture2dProgram(Texture2dProgram.ProgramType.TEXTURE_EXT));
78+
mTextureId = mFullScreen.createTextureObject();
79+
80+
mSurfaceTexture = new SurfaceTexture(mTextureId);
81+
mSurfaceTexture.setOnFrameAvailableListener(Capture.this);
82+
mCameraHandler.sendMessage(mCameraHandler.obtainMessage(CameraHandler.MSG_START_CAMERA, null));
83+
System.out.println("sent starting message to UI thread");
84+
}
85+
});
7286
}
7387

7488
public void setCamera(String camera) {
@@ -96,6 +110,10 @@ public void pause() {
96110
log("pause called");
97111
if (mCamera != null) {
98112
mCamera.release();
113+
}
114+
if (mFullScreen != null) {
115+
mFullScreen.release(false); // assume the GLSurfaceView EGL context is about
116+
mFullScreen = null; // to be destroyed
99117
}
100118
}
101119

@@ -144,9 +162,6 @@ public String[] list() {
144162
}
145163

146164
private void startPreview(SurfaceHolder mHolder) {
147-
// If the preview can change or rotate, take care of those events
148-
// here.
149-
// Make sure to stop the preview before resizing or reformatting it.
150165

151166
if (mHolder.getSurface() == null) {
152167
// preview surface does not exist
@@ -177,6 +192,7 @@ private void startPreview(SurfaceHolder mHolder) {
177192

178193
static class CameraHandler extends Handler {
179194
public static final int MSG_SET_SURFACE_TEXTURE = 0;
195+
public static final int MSG_START_CAMERA = 1;
180196

181197
// Weak reference to the Activity; only access this from the UI thread.
182198
private CameraHandlerCallback callback;
@@ -199,6 +215,9 @@ public void handleMessage(Message inputMessage) {
199215
case MSG_SET_SURFACE_TEXTURE:
200216
callback.handleSetSurfaceTexture((SurfaceTexture) inputMessage.obj);
201217
break;
218+
case MSG_START_CAMERA:
219+
callback.startCamera();
220+
break;
202221
default:
203222
throw new RuntimeException("unknown msg " + what);
204223
}
@@ -219,24 +238,23 @@ public static void printCompatibleResolutionsList(Capture capture) {
219238
}
220239

221240
@Override
222-
public void handleSetSurfaceTexture(SurfaceTexture st) {
223-
// TODO Auto-generated method stub
224-
}
241+
public void handleSetSurfaceTexture(SurfaceTexture st) {}
242+
243+
@Override
244+
public void startCamera() {
245+
System.out.println("Start Camera Impl");
246+
startCameraImpl(0);
247+
};
225248

226249
@Override
227250
public void onFrameAvailable(final SurfaceTexture surfaceTexture) {
228-
// TODO Auto-generated method stub
229-
System.out.println("OnFrameAvailable");
230-
((GLSurfaceView) applet.getSurfaceView()).queueEvent(new Runnable() {
231-
251+
glView.queueEvent(new Runnable() {
232252
@Override
233253
public void run() {
234-
// TODO Auto-generated method stub
254+
System.out.println("onFrameAvailable");
235255
surfaceTexture.updateTexImage();
236-
256+
//TODO: Copy this texture to applet's texture
237257
}
238258
});
239-
240259
}
241-
242260
}

0 commit comments

Comments
 (0)