Skip to content

Commit 436da50

Browse files
committed
Use custom texture and use glReadPixels to fill the pixels array
Signed-off-by: Umair Khan <[email protected]>
1 parent befe3a0 commit 436da50

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed

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

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ public static void log(String log) {
5050
private int mTextureId;
5151
private final float[] mSTMatrix = new float[16];
5252

53-
private Texture appletTexture;
53+
private Texture customTexture;
54+
private PGraphicsOpenGL pg;
55+
private IntBuffer pixelBuffer;
5456

5557
private CameraHandler mCameraHandler;
5658

@@ -62,19 +64,24 @@ public Capture(PApplet context) {
6264
}
6365

6466
public Capture(final PApplet applet, int width, int height) {
67+
super();
6568
this.applet = applet;
6669
if (width == -1 || height == -1) {
6770
//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;
7373
}
74+
init(width, height, ARGB);
75+
pixelBuffer = IntBuffer.allocate(width * height);
76+
pixelBuffer.position(0);
77+
7478
applet.registerMethod("pause", this);
7579
applet.registerMethod("resume", this);
7680
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);
7885
applet.runOnUiThread(new Runnable() {
7986
@Override
8087
public void run() {
@@ -91,7 +98,8 @@ public void run() {
9198

9299
mSurfaceTexture = new SurfaceTexture(mTextureId);
93100
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);
95103
System.out.println("sent starting message to UI thread");
96104
prepareFrameBuffers();
97105
}
@@ -243,11 +251,20 @@ public Camera getCamera() {
243251

244252
public static void printCompatibleResolutionsList(Capture capture) {
245253
Camera camera = capture.getCamera();
254+
boolean selfOpen = false;
255+
if (camera == null) {
256+
camera = Camera.open(0);
257+
selfOpen = true;
258+
}
259+
246260
List<Camera.Size> sizes = camera.getParameters()
247261
.getSupportedPreviewSizes();
248262
for (Size size : sizes) {
249263
System.out.println(size.width + "x" + size.height);
250264
}
265+
if (selfOpen) {
266+
camera.release();
267+
}
251268
}
252269

253270
@Override
@@ -266,14 +283,21 @@ public void onFrameAvailable(final SurfaceTexture surfaceTexture) {
266283
public void run() {
267284
System.out.println("onFrameAvailable");
268285
surfaceTexture.updateTexImage();
286+
269287
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBuffers.get(0));
288+
GLES20.glViewport(0, 0, width, height);
270289
surfaceTexture.getTransformMatrix(mSTMatrix);
271290
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();
273297
}
274298
});
275299
}
276-
300+
277301
public void prepareFrameBuffers() {
278302

279303
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
@@ -292,21 +316,24 @@ public void prepareFrameBuffers() {
292316
GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, renderBuffers.get(0));
293317
GlUtil.checkGlError("glBindRenderbuffer");
294318
//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);
296320
GlUtil.checkGlError("glRenderbufferStorage");
297321

298322
//Attach render buffer to frame buffer
299323
GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT,
300324
GLES20.GL_RENDERBUFFER, renderBuffers.get(0));
301325
GlUtil.checkGlError("glFramebufferRenderbuffer");
302326

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);
305333
GlUtil.checkGlError("glTexImage2D");
306-
307-
System.out.println("applet's texture name = " + appletTexture.glName);
334+
308335
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);
310337
GlUtil.checkGlError("glFramebufferTexture2D");
311338

312339
/*

0 commit comments

Comments
 (0)