Skip to content

Instantly share code, notes, and snippets.

@jamieomatthews
Last active December 15, 2015 06:28
Show Gist options
  • Save jamieomatthews/5216018 to your computer and use it in GitHub Desktop.
Save jamieomatthews/5216018 to your computer and use it in GitHub Desktop.
@javierpuntonet implementation for getting an image bitmap from the filtered camera stream
//First, create a new class to be used as a callback. I have renamed this from javier's original source, to be in english
/////////GPUImageCallback.java
package jp.co.cyberagent.android.gpuimage;
import android.graphics.Bitmap;
public interface GPUImageCallback {
void callback(Bitmap image);
}
//then, in GPUImageRenderer.java, he made some changes to onDraw to create a bitmap in onDraw. Here are the new/changed methods
/////////GPUImageRenderer.java
protected Bitmap image;
 public Bitmap createBitmapFromGLSurface(int x, int y, int w, int h, GL10 gl) throws OutOfMemoryError {
int bitmapBuffer[] = new int[w * h];
int bitmapSource[] = new int[w * h];
IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer);
intBuffer.position(0);
try {
gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, intBuffer);
int offset1, offset2;
for (int i = 0; i < h; i++) {
offset1 = i * w;
offset2 = (h - i - 1) * w;
for (int j = 0; j < w; j++) {
int texturePixel = bitmapBuffer[offset1 + j];
int blue = (texturePixel >> 16) & 0xff;
int red = (texturePixel << 16) & 0x00ff0000;
int pixel = (texturePixel & 0xff00ff00) | red | blue;
bitmapSource[offset2 + j] = pixel;
}
}
} catch (GLException e) {
return null;
}
return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888);
}
@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();
}
}
mFilter.onDraw(mGLTextureId, mGLCubeBuffer, mGLTextureBuffer);
if (mSurfaceTexture != null) {
mSurfaceTexture.updateTexImage();
}
try
{
if (image==null)
image = createBitmapFromGLSurface((mOutputWidth-mImageWidth)/2,(mOutputHeight-mImageHeight)/2,mImageWidth,mImageHeight,gl);
if (image!=null)
if (callback!=null)
callback.callback(image);
}
catch (Exception ex)
{
image = null;
}
}
public void setScaleType(GPUImage.ScaleType scaleType) {
mScaleType = scaleType;
}
/////////////GPUImage.java
private ScaleType mScaleType = ScaleType.CENTER_INSIDE;
public Bitmap getImage()
{
return mRenderer.image;
}
public GPUImage setCallback(GPUImageCallback listener)
{
mRenderer.setCallbackGPUImageListener(listener);
return this;
}
public enum ScaleType { CENTER_INSIDE, CENTER_CROP }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment