14
14
import android .view .SurfaceHolder ;
15
15
import processing .core .PConstants ;
16
16
import processing .core .PApplet ;
17
+ import processing .core .PImage ;
17
18
import processing .opengl .PGraphicsOpenGL ;
18
19
19
20
@ SuppressWarnings ("deprecation" )
20
- public class Capture extends PGraphicsOpenGL implements PConstants ,
21
+ public class Capture extends PImage implements PConstants ,
21
22
SurfaceHolder .Callback , CameraHandlerCallback , SurfaceTexture .OnFrameAvailableListener {
22
23
23
24
private static final boolean DEBUG = true ;
@@ -30,25 +31,26 @@ public static void log(String log) {
30
31
private PApplet applet ;
31
32
32
33
private Camera mCamera ;
33
- private Camera .Parameters parameters ;
34
-
35
- private int previewWidth , previewHeight ;
36
34
37
35
private static ArrayList <String > camerasList = new ArrayList <String >();
38
36
39
37
private static final String KEY_FRONT_CAMERA = "front-camera-%d" ;
40
38
private static final String KEY_BACK_CAMERA = "back-camera-%d" ;
41
39
42
40
private int selectedCamera = -1 ;
43
-
41
+
42
+ private GLSurfaceView glView ;
44
43
private SurfaceTexture mSurfaceTexture ;
44
+ private FullFrameRect mFullScreen ;
45
+ private int mTextureId ;
46
+
47
+ private CameraHandler mCameraHandler ;
45
48
46
49
public Capture (PApplet context ) {
47
50
this (context , -1 , -1 );
48
51
}
49
52
50
53
public Capture (final PApplet applet , int width , int height ) {
51
- super ();
52
54
this .applet = applet ;
53
55
if (width == -1 || height == -1 ) {
54
56
//TODO: Temp hack. Needs to be handled intelligently.
@@ -60,15 +62,27 @@ public Capture(final PApplet applet, int width, int height) {
60
62
}
61
63
applet .registerMethod ("pause" , this );
62
64
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
+ });
72
86
}
73
87
74
88
public void setCamera (String camera ) {
@@ -96,6 +110,10 @@ public void pause() {
96
110
log ("pause called" );
97
111
if (mCamera != null ) {
98
112
mCamera .release ();
113
+ }
114
+ if (mFullScreen != null ) {
115
+ mFullScreen .release (false ); // assume the GLSurfaceView EGL context is about
116
+ mFullScreen = null ; // to be destroyed
99
117
}
100
118
}
101
119
@@ -144,9 +162,6 @@ public String[] list() {
144
162
}
145
163
146
164
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.
150
165
151
166
if (mHolder .getSurface () == null ) {
152
167
// preview surface does not exist
@@ -177,6 +192,7 @@ private void startPreview(SurfaceHolder mHolder) {
177
192
178
193
static class CameraHandler extends Handler {
179
194
public static final int MSG_SET_SURFACE_TEXTURE = 0 ;
195
+ public static final int MSG_START_CAMERA = 1 ;
180
196
181
197
// Weak reference to the Activity; only access this from the UI thread.
182
198
private CameraHandlerCallback callback ;
@@ -199,6 +215,9 @@ public void handleMessage(Message inputMessage) {
199
215
case MSG_SET_SURFACE_TEXTURE :
200
216
callback .handleSetSurfaceTexture ((SurfaceTexture ) inputMessage .obj );
201
217
break ;
218
+ case MSG_START_CAMERA :
219
+ callback .startCamera ();
220
+ break ;
202
221
default :
203
222
throw new RuntimeException ("unknown msg " + what );
204
223
}
@@ -219,24 +238,23 @@ public static void printCompatibleResolutionsList(Capture capture) {
219
238
}
220
239
221
240
@ 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
+ };
225
248
226
249
@ Override
227
250
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 () {
232
252
@ Override
233
253
public void run () {
234
- // TODO Auto-generated method stub
254
+ System . out . println ( "onFrameAvailable" );
235
255
surfaceTexture .updateTexImage ();
236
-
256
+ //TODO: Copy this texture to applet's texture
237
257
}
238
258
});
239
-
240
259
}
241
-
242
260
}
0 commit comments