Skip to content

Commit

Permalink
Implement capturing of picture in same GLContext as the one displayed…
Browse files Browse the repository at this point in the history
…. Resize view for capturing picture of bigger size.

Closes #19
References #31
  • Loading branch information
Patrick Boos committed May 7, 2013
1 parent 71515ec commit 8db7386
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 20 deletions.
19 changes: 18 additions & 1 deletion library/src/jp/co/cyberagent/android/gpuimage/GPUImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import android.view.WindowManager;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.Semaphore;
Expand Down Expand Up @@ -324,6 +323,9 @@ public static void getBitmapForMultipleFilters(final Bitmap bitmap,
}

/**
* Deprecated: Please use
* {@link GPUImageView#saveToPictures(String, String, jp.co.cyberagent.android.gpuimage.GPUImageView.OnPictureSavedListener)}
*
* Save current image with applied filter to Pictures. It will be stored on
* the default Picture folder on the phone below the given folerName and
* fileName. <br />
Expand All @@ -334,12 +336,16 @@ public static void getBitmapForMultipleFilters(final Bitmap bitmap,
* @param fileName the file name
* @param listener the listener
*/
@Deprecated
public void saveToPictures(final String folderName, final String fileName,
final OnPictureSavedListener listener) {
saveToPictures(mCurrentBitmap, folderName, fileName, listener);
}

/**
* Deprecated: Please use
* {@link GPUImageView#saveToPictures(String, String, jp.co.cyberagent.android.gpuimage.GPUImageView.OnPictureSavedListener)}
*
* Apply and save the given bitmap with applied filter to Pictures. It will
* be stored on the default Picture folder on the phone below the given
* folerName and fileName. <br />
Expand All @@ -351,11 +357,21 @@ public void saveToPictures(final String folderName, final String fileName,
* @param fileName the file name
* @param listener the listener
*/
@Deprecated
public void saveToPictures(final Bitmap bitmap, final String folderName, final String fileName,
final OnPictureSavedListener listener) {
new SaveTask(bitmap, folderName, fileName, listener).execute();
}

/**
* Runs the given Runnable on the OpenGL thread.
*
* @param runnable The runnable to be run on the OpenGL thread.
*/
void runOnGLThread(Runnable runnable) {
mRenderer.runOnDrawEnd(runnable);
}

private int getOutputWidth() {
if (mRenderer != null && mRenderer.getFrameWidth() != 0) {
return mRenderer.getFrameWidth();
Expand All @@ -382,6 +398,7 @@ private int getOutputHeight() {
}
}

@Deprecated
private class SaveTask extends AsyncTask<Void, Void, Void> {

private final Bitmap mBitmap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class GPUImageRenderer implements Renderer, PreviewCallback {
private int mAddedPadding;

private final Queue<Runnable> mRunOnDraw;
private final Queue<Runnable> mRunOnDrawEnd;
private Rotation mRotation;
private boolean mFlipHorizontal;
private boolean mFlipVertical;
Expand All @@ -74,6 +75,7 @@ public class GPUImageRenderer implements Renderer, PreviewCallback {
public GPUImageRenderer(final GPUImageFilter filter) {
mFilter = filter;
mRunOnDraw = new LinkedList<Runnable>();
mRunOnDrawEnd = new LinkedList<Runnable>();

mGLCubeBuffer = ByteBuffer.allocateDirect(CUBE.length * 4)
.order(ByteOrder.nativeOrder())
Expand Down Expand Up @@ -109,17 +111,22 @@ public void onSurfaceChanged(final GL10 gl, final int width, final int height) {
@Override
public void onDrawFrame(final GL10 gl) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
synchronized (mRunOnDraw) {
while (!mRunOnDraw.isEmpty()) {
mRunOnDraw.poll().run();
}
}
runAll(mRunOnDraw);
mFilter.onDraw(mGLTextureId, mGLCubeBuffer, mGLTextureBuffer);
runAll(mRunOnDrawEnd);
if (mSurfaceTexture != null) {
mSurfaceTexture.updateTexImage();
}
}

private void runAll(Queue<Runnable> queue) {
synchronized (queue) {
while (!queue.isEmpty()) {
queue.poll().run();
}
}
}

@Override
public void onPreviewFrame(final byte[] data, final Camera camera) {
final Size previewSize = camera.getParameters().getPreviewSize();
Expand Down Expand Up @@ -319,4 +326,10 @@ protected void runOnDraw(final Runnable runnable) {
mRunOnDraw.add(runnable);
}
}

protected void runOnDrawEnd(final Runnable runnable) {
synchronized (mRunOnDrawEnd) {
mRunOnDrawEnd.add(runnable);
}
}
}
Loading

0 comments on commit 8db7386

Please sign in to comment.