@@ -50,7 +50,9 @@ public static void log(String log) {
50
50
private int mTextureId ;
51
51
private final float [] mSTMatrix = new float [16 ];
52
52
53
- private Texture appletTexture ;
53
+ private Texture customTexture ;
54
+ private PGraphicsOpenGL pg ;
55
+ private IntBuffer pixelBuffer ;
54
56
55
57
private CameraHandler mCameraHandler ;
56
58
@@ -62,19 +64,24 @@ public Capture(PApplet context) {
62
64
}
63
65
64
66
public Capture (final PApplet applet , int width , int height ) {
67
+ super ();
65
68
this .applet = applet ;
66
69
if (width == -1 || height == -1 ) {
67
70
//TODO: Temp hack. Needs to be handled intelligently.
68
- this .width = 1080 ;
69
- this .height = 1920 ;
70
- } else {
71
- this .width = width ;
72
- this .height = height ;
71
+ width = 720 ;
72
+ height = 1280 ;
73
73
}
74
+ init (width , height , ARGB );
75
+ pixelBuffer = IntBuffer .allocate (width * height );
76
+ pixelBuffer .position (0 );
77
+
74
78
applet .registerMethod ("pause" , this );
75
79
applet .registerMethod ("resume" , this );
76
80
glView = (GLSurfaceView ) applet .getSurfaceView ();
77
- appletTexture = ((PGraphicsOpenGL )applet .g ).getTexture ();
81
+ pg = (PGraphicsOpenGL )applet .g ;
82
+ customTexture = new Texture (pg , width , height );
83
+ log ("cusotm texture address = " + customTexture .glName );
84
+ pg .setCache (this , customTexture );
78
85
applet .runOnUiThread (new Runnable () {
79
86
@ Override
80
87
public void run () {
@@ -91,7 +98,8 @@ public void run() {
91
98
92
99
mSurfaceTexture = new SurfaceTexture (mTextureId );
93
100
mSurfaceTexture .setOnFrameAvailableListener (Capture .this );
94
- mCameraHandler .sendMessage (mCameraHandler .obtainMessage (CameraHandler .MSG_START_CAMERA , null ));
101
+ // mCameraHandler.sendMessage(mCameraHandler.obtainMessage(CameraHandler.MSG_START_CAMERA, null));
102
+ startCameraImpl (0 );
95
103
System .out .println ("sent starting message to UI thread" );
96
104
prepareFrameBuffers ();
97
105
}
@@ -243,11 +251,20 @@ public Camera getCamera() {
243
251
244
252
public static void printCompatibleResolutionsList (Capture capture ) {
245
253
Camera camera = capture .getCamera ();
254
+ boolean selfOpen = false ;
255
+ if (camera == null ) {
256
+ camera = Camera .open (0 );
257
+ selfOpen = true ;
258
+ }
259
+
246
260
List <Camera .Size > sizes = camera .getParameters ()
247
261
.getSupportedPreviewSizes ();
248
262
for (Size size : sizes ) {
249
263
System .out .println (size .width + "x" + size .height );
250
264
}
265
+ if (selfOpen ) {
266
+ camera .release ();
267
+ }
251
268
}
252
269
253
270
@ Override
@@ -266,14 +283,21 @@ public void onFrameAvailable(final SurfaceTexture surfaceTexture) {
266
283
public void run () {
267
284
System .out .println ("onFrameAvailable" );
268
285
surfaceTexture .updateTexImage ();
286
+
269
287
GLES20 .glBindFramebuffer (GLES20 .GL_FRAMEBUFFER , frameBuffers .get (0 ));
288
+ GLES20 .glViewport (0 , 0 , width , height );
270
289
surfaceTexture .getTransformMatrix (mSTMatrix );
271
290
mFullScreen .drawFrame (mTextureId , mSTMatrix );
272
- //TODO: Copy this texture to applet's texture
291
+
292
+ pixelBuffer .position (0 );
293
+ GLES20 .glReadPixels (0 , 0 , width , height , GLES20 .GL_RGBA , GLES20 .GL_UNSIGNED_BYTE , pixelBuffer );
294
+ pixelBuffer .position (0 );
295
+ pixelBuffer .get (Capture .this .pixels );
296
+ updatePixels ();
273
297
}
274
298
});
275
299
}
276
-
300
+
277
301
public void prepareFrameBuffers () {
278
302
279
303
GLES20 .glActiveTexture (GLES20 .GL_TEXTURE0 );
@@ -292,21 +316,24 @@ public void prepareFrameBuffers() {
292
316
GLES20 .glBindRenderbuffer (GLES20 .GL_RENDERBUFFER , renderBuffers .get (0 ));
293
317
GlUtil .checkGlError ("glBindRenderbuffer" );
294
318
//Allocate memory to render buffers
295
- GLES20 .glRenderbufferStorage (GLES20 .GL_RENDERBUFFER , GLES20 .GL_DEPTH_COMPONENT16 , 1080 , 1920 );
319
+ GLES20 .glRenderbufferStorage (GLES20 .GL_RENDERBUFFER , GLES20 .GL_DEPTH_COMPONENT16 , width , height );
296
320
GlUtil .checkGlError ("glRenderbufferStorage" );
297
321
298
322
//Attach render buffer to frame buffer
299
323
GLES20 .glFramebufferRenderbuffer (GLES20 .GL_FRAMEBUFFER , GLES20 .GL_DEPTH_ATTACHMENT ,
300
324
GLES20 .GL_RENDERBUFFER , renderBuffers .get (0 ));
301
325
GlUtil .checkGlError ("glFramebufferRenderbuffer" );
302
326
303
- GLES20 .glBindTexture (GLES20 .GL_TEXTURE_2D , appletTexture .glName );
304
- GLES20 .glTexImage2D (GLES20 .GL_TEXTURE_2D , 0 , GLES20 .GL_RGBA , 1080 , 1920 , 0 , GLES20 .GL_RGBA , GLES20 .GL_UNSIGNED_BYTE , null );
327
+ GLES20 .glBindTexture (GLES20 .GL_TEXTURE_2D , customTexture .glName );
328
+ GLES20 .glTexParameteri (GLES20 .GL_TEXTURE_2D , GLES20 .GL_TEXTURE_MIN_FILTER , GLES20 .GL_LINEAR );
329
+ GLES20 .glTexParameteri (GLES20 .GL_TEXTURE_2D , GLES20 .GL_TEXTURE_MAG_FILTER , GLES20 .GL_LINEAR );
330
+ GLES20 .glTexParameterf (GLES20 .GL_TEXTURE_2D , GLES20 .GL_TEXTURE_WRAP_S , GLES20 .GL_CLAMP_TO_EDGE );
331
+ GLES20 .glTexParameterf (GLES20 .GL_TEXTURE_2D , GLES20 .GL_TEXTURE_WRAP_T , GLES20 .GL_CLAMP_TO_EDGE );
332
+ GLES20 .glTexImage2D (GLES20 .GL_TEXTURE_2D , 0 , GLES20 .GL_RGBA , width , height , 0 , GLES20 .GL_RGBA , GLES20 .GL_UNSIGNED_BYTE , null );
305
333
GlUtil .checkGlError ("glTexImage2D" );
306
-
307
- System .out .println ("applet's texture name = " + appletTexture .glName );
334
+
308
335
GLES20 .glFramebufferTexture2D (GLES20 .GL_FRAMEBUFFER , GLES20 .GL_COLOR_ATTACHMENT0 ,
309
- GLES20 .GL_TEXTURE_2D , appletTexture .glName , 0 );
336
+ GLES20 .GL_TEXTURE_2D , customTexture .glName , 0 );
310
337
GlUtil .checkGlError ("glFramebufferTexture2D" );
311
338
312
339
/*
0 commit comments