1
1
package in .omerjerk .processing .video .android ;
2
2
3
- import in .omerjerk .processing .video .android .helpers .FullFrameRect ;
4
- import in .omerjerk .processing .video .android .helpers .GlUtil ;
5
- import in .omerjerk .processing .video .android .helpers .Texture2dProgram ;
6
-
7
3
import java .io .IOException ;
8
4
import java .nio .IntBuffer ;
9
5
import java .util .ArrayList ;
10
6
import java .util .List ;
11
7
12
- import android .app .Activity ;
13
8
import android .graphics .SurfaceTexture ;
14
9
import android .hardware .Camera ;
15
10
import android .hardware .Camera .Size ;
18
13
import android .os .Handler ;
19
14
import android .os .Message ;
20
15
import android .util .Log ;
21
- import processing .core .PConstants ;
22
16
import processing .core .PApplet ;
23
- import processing .opengl .PGL ;
24
17
import processing .opengl .PGraphicsOpenGL ;
25
- import processing .opengl .Texture ;
26
18
27
19
@ SuppressWarnings ("deprecation" )
28
- public class Capture extends VideoBase implements CameraHandlerCallback ,
29
- SurfaceTexture .OnFrameAvailableListener {
30
-
31
- private static final boolean DEBUG = true ;
32
-
33
- private Activity activity ;
34
-
35
- public static void log (String log ) {
36
- if (DEBUG )
37
- System .out .println (log );
38
- }
20
+ public class Capture extends VideoBase implements CameraHandlerCallback {
39
21
40
22
private Camera mCamera ;
41
23
@@ -46,33 +28,14 @@ public static void log(String log) {
46
28
47
29
private int selectedCamera = -1 ;
48
30
49
- private GLSurfaceView glView ;
50
- private SurfaceTexture mSurfaceTexture ;
51
- private FullFrameRect mFullScreen ;
52
- private int mTextureId ;
53
- private final float [] mSTMatrix = new float [16 ];
54
-
55
- // private Texture customTexture;
56
- private PGraphicsOpenGL pg ;
57
-
58
- private PGraphicsOpenGL destpg ;
59
-
60
31
private CameraHandler mCameraHandler ;
61
-
62
- IntBuffer frameBuffers = IntBuffer .allocate (1 );
63
- IntBuffer renderBuffers = IntBuffer .allocate (1 );
64
- IntBuffer customTexture = IntBuffer .allocate (1 );
65
-
66
- private IntBuffer pixelBuffer ;
67
-
68
- private boolean isAvailable = false ;
69
32
70
33
public Capture (PApplet parent ) {
71
34
this (parent , -1 , -1 );
72
35
}
73
36
74
37
public Capture (final PApplet parent , int width , int height ) {
75
- super ();
38
+ super (parent );
76
39
this .parent = parent ;
77
40
if (width == -1 || height == -1 ) {
78
41
//TODO: Temp hack. Needs to be handled intelligently.
@@ -81,8 +44,6 @@ public Capture(final PApplet parent, int width, int height) {
81
44
}
82
45
init (width , height , ARGB );
83
46
84
- parent .registerMethod ("pause" , this );
85
- parent .registerMethod ("resume" , this );
86
47
glView = (GLSurfaceView ) parent .getSurfaceView ();
87
48
pg = (PGraphicsOpenGL )parent .g ;
88
49
// customTexture = new Texture(pg, width, height);
@@ -122,10 +83,6 @@ public void start() {
122
83
CameraHandler .MSG_START_PREVIEW ));
123
84
}
124
85
125
- public boolean available () {
126
- return isAvailable ;
127
- }
128
-
129
86
@ Override
130
87
public void loadPixels () {
131
88
super .loadPixels ();
@@ -142,7 +99,8 @@ public void loadPixels() {
142
99
pixelBuffer .get (Capture .this .pixels );
143
100
}
144
101
145
- public void pause () {
102
+ @ Override
103
+ public void onPause () {
146
104
log ("pause called" );
147
105
isAvailable = false ;
148
106
if (mCamera != null ) {
@@ -164,7 +122,8 @@ public void run() {
164
122
});
165
123
}
166
124
167
- public void resume () {
125
+ @ Override
126
+ public void onResume () {
168
127
log ("resume called" );
169
128
glView .queueEvent (new Runnable () {
170
129
@ Override
@@ -320,124 +279,4 @@ public void startPreview() {
320
279
e .printStackTrace ();
321
280
}
322
281
}
323
-
324
- @ Override
325
- public void onFrameAvailable (final SurfaceTexture surfaceTexture ) {
326
- glView .queueEvent (new Runnable () {
327
- @ Override
328
- public void run () {
329
- log ("onFrameAvailable" );
330
- isAvailable = true ;
331
- surfaceTexture .updateTexImage ();
332
-
333
- GLES20 .glBindFramebuffer (GLES20 .GL_FRAMEBUFFER , frameBuffers .get (0 ));
334
- GLES20 .glViewport (0 , 0 , width , height );
335
- surfaceTexture .getTransformMatrix (mSTMatrix );
336
- mFullScreen .drawFrame (mTextureId , mSTMatrix );
337
-
338
- getImage (false );
339
-
340
- /*
341
- pixelBuffer.position(0);
342
- GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixelBuffer);
343
- pixelBuffer.position(0);
344
- pixelBuffer.get(Capture.this.pixels);
345
- updatePixels(); */
346
-
347
- //Fall back to default frame buffer. Not sure if needed.
348
- GLES20 .glBindFramebuffer (GLES20 .GL_FRAMEBUFFER , 0 );
349
- }
350
- });
351
- }
352
-
353
- private void createSurfaceTexture () {
354
- mFullScreen = new FullFrameRect (
355
- new Texture2dProgram (Texture2dProgram .ProgramType .TEXTURE_EXT ));
356
- mTextureId = mFullScreen .createTextureObject ();
357
- mSurfaceTexture = new SurfaceTexture (mTextureId );
358
- mSurfaceTexture .setOnFrameAvailableListener (Capture .this );
359
- }
360
-
361
- private void prepareFrameBuffers () {
362
-
363
- GLES20 .glActiveTexture (GLES20 .GL_TEXTURE0 );
364
-
365
- //Generate frame buffer
366
- GLES20 .glGenFramebuffers (1 , frameBuffers );
367
- GlUtil .checkGlError ("glGenFramebuffers" );
368
- //Bind frame buffer
369
- GLES20 .glBindFramebuffer (GLES20 .GL_FRAMEBUFFER , frameBuffers .get (0 ));
370
- GlUtil .checkGlError ("glBindFramebuffer" );
371
-
372
- //Generate render buffers
373
- GLES20 .glGenRenderbuffers (1 , renderBuffers );
374
- GlUtil .checkGlError ("glGenRenderbuffers" );
375
- //Bind render buffers
376
- GLES20 .glBindRenderbuffer (GLES20 .GL_RENDERBUFFER , renderBuffers .get (0 ));
377
- GlUtil .checkGlError ("glBindRenderbuffer" );
378
- //Allocate memory to render buffers
379
- GLES20 .glRenderbufferStorage (GLES20 .GL_RENDERBUFFER , GLES20 .GL_DEPTH_COMPONENT16 , width , height );
380
- GlUtil .checkGlError ("glRenderbufferStorage" );
381
-
382
- //Attach render buffer to frame buffer
383
- GLES20 .glFramebufferRenderbuffer (GLES20 .GL_FRAMEBUFFER , GLES20 .GL_DEPTH_ATTACHMENT ,
384
- GLES20 .GL_RENDERBUFFER , renderBuffers .get (0 ));
385
- GlUtil .checkGlError ("glFramebufferRenderbuffer" );
386
-
387
- GLES20 .glGenTextures (1 , customTexture );
388
- GlUtil .checkGlError ("glGenTextures" );
389
-
390
- GLES20 .glBindTexture (GLES20 .GL_TEXTURE_2D , customTexture .get (0 ));
391
- GlUtil .checkGlError ("glBindTexture" );
392
-
393
- GLES20 .glTexParameteri (GLES20 .GL_TEXTURE_2D , GLES20 .GL_TEXTURE_MIN_FILTER , GLES20 .GL_LINEAR );
394
- GLES20 .glTexParameteri (GLES20 .GL_TEXTURE_2D , GLES20 .GL_TEXTURE_MAG_FILTER , GLES20 .GL_LINEAR );
395
- GLES20 .glTexParameterf (GLES20 .GL_TEXTURE_2D , GLES20 .GL_TEXTURE_WRAP_S , GLES20 .GL_CLAMP_TO_EDGE );
396
- GLES20 .glTexParameterf (GLES20 .GL_TEXTURE_2D , GLES20 .GL_TEXTURE_WRAP_T , GLES20 .GL_CLAMP_TO_EDGE );
397
- GLES20 .glTexImage2D (GLES20 .GL_TEXTURE_2D , 0 , GLES20 .GL_RGBA , width , height , 0 , GLES20 .GL_RGBA , GLES20 .GL_UNSIGNED_BYTE , null );
398
- GlUtil .checkGlError ("glTexImage2D" );
399
-
400
- GLES20 .glFramebufferTexture2D (GLES20 .GL_FRAMEBUFFER , GLES20 .GL_COLOR_ATTACHMENT0 ,
401
- GLES20 .GL_TEXTURE_2D , customTexture .get (0 ), 0 );
402
- GlUtil .checkGlError ("glFramebufferTexture2D" );
403
-
404
- /*
405
- //No sure if this is required in opengl es 2. Ignoring as of now.
406
- IntBuffer drawBuffers = IntBuffer.allocate(1);
407
- drawBuffers.put(0, GLES20.GL_COLOR_ATTACHMENT0);
408
- GLES30.glDrawBuffers(1, drawBuffers);
409
- */
410
-
411
- // See if GLES is happy with all this.
412
- int status = GLES20 .glCheckFramebufferStatus (GLES20 .GL_FRAMEBUFFER );
413
- if (status != GLES20 .GL_FRAMEBUFFER_COMPLETE ) {
414
- throw new RuntimeException ("Framebuffer not complete, status=" + status );
415
- }
416
- }
417
-
418
- //The following method has been copied from Syphon library for processing
419
- public void getImage (boolean loadPixels ) {
420
-
421
- if (destpg == null || destpg .width != width || destpg .height != height ) {
422
- destpg = (PGraphicsOpenGL ) parent .createGraphics (width , height , PConstants .P2D );
423
- destpg .pgl .setGlThread (Thread .currentThread ());
424
- }
425
-
426
- destpg .beginDraw ();
427
- destpg .background (0 , 0 );
428
- PGL pgl = destpg .beginPGL ();
429
- pgl .drawTexture (PGL .TEXTURE_2D , customTexture .get (0 ), width , height ,
430
- 0 , 0 , width , height );
431
- destpg .endPGL ();
432
- destpg .endDraw ();
433
-
434
- // Uses the PGraphics texture as the cache object for the image
435
- Texture tex = destpg .getTexture ();
436
- pg .setCache (this , tex );
437
- if (loadPixels ) {
438
- super .loadPixels ();
439
- tex .get (this .pixels );
440
- this .setLoaded (false );
441
- }
442
- }
443
282
}
0 commit comments