MagicCamera
MagicCamera copied to clipboard
Not effect filter after save bitmap
I have applied filter -> View on the Surface is ok, but when I save the bitmap to image, not have the filter?
public void getBitmapWithFilter(final Bitmap bitmap, final File outFile, final OnBitmapReceiveListener onBitmapReceiveListener){
if (!mIsSaving) {
mIsSaving = true;
mGLSurfaceView.queueEvent(new Runnable() {
@Override
public void run() {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
if(mFilter != null) {
mFilter.onInputSizeChanged(width, height);
mFilter.onDisplaySizeChanged(width, height);
}
// init buffer for texture
int[] mFrameBuffers = new int[1];
int[] mFrameBufferTextures = new int[1];
// load texture
OpenGlUtils.loadTextureBufferFiltering(mFrameBuffers, mFrameBufferTextures, width, height);
GLES20.glViewport(0, 0, width, height);
// load texture with bitmap
int textureId = OpenGlUtils.loadTexture(bitmap, OpenGlUtils.NO_TEXTURE, false);
// draw frame to texture
if(mFilter != null){
mFilter.onDrawFrame(textureId);
} else {
imageInput.onDrawFrame(textureId);
}
// copy pixel buffer from the current texture
Bitmap result = BitmapProcessor.getBitmapFromTexture(mFrameBuffers[0], textureId, width, height);
// delete texture
OpenGlUtils.deleteTextureBufferFiltering(textureId, mFrameBuffers, mFrameBufferTextures);
// rest getBitmapWithFilter display
if(mFilter != null) {
mFilter.onDisplaySizeChanged(mSurfaceWidth, mSurfaceHeight);
mFilter.onInputSizeChanged(mImageWidth, mImageHeight);
}
if (outFile != null) {
// save bitmap to disk
BitmapProcessor.saveBitmap(result, outFile);
}
mIsSaving = false;
// reset viewport of the current texture.
GLES20.glViewport(0, 0, mSurfaceWidth, mSurfaceHeight);
// notice to activity
if (onBitmapReceiveListener != null) {
onBitmapReceiveListener.onBitmapReceive(result);
}
}
});
}
}
public static Bitmap getBitmapFromTexture(int mFrameBuffers, int mFrameBufferTextures, final int width, final int height) {
final Bitmap object[] = new Bitmap[1];
try {
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFrameBuffers);
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, mFrameBufferTextures, 0);
GLES20.glClear(0);
object[0] = createBitmap(width, height);
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
GLES20.glClear(0);
} catch (Exception e) {
e.printStackTrace();
}
return object[0];
}
public static Bitmap createBitmap(int width, int height) {
ByteBuffer ib = ByteBuffer.allocate(width * height * 4);
ib.order(ByteOrder.LITTLE_ENDIAN);
GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, ib);
ib.rewind();
Bitmap resultTemp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
resultTemp.copyPixelsFromBuffer(ib);
return resultTemp;
}
public static void loadTextureBufferFiltering(int[] mFrameBuffers, int[] mFrameBufferTextures, int width, int height) {
GLES20.glGenFramebuffers(1, mFrameBuffers, 0);
GLES20.glGenTextures(1, mFrameBufferTextures, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mFrameBufferTextures[0]);
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFrameBuffers[0]);
//Bind the texture to your FBO
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, mFrameBufferTextures[0], 0);
}
public static void deleteTextureBufferFiltering(int textureId, int[] mFrameBuffers, int[] mFrameBufferTextures) {
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
GLES20.glDeleteTextures(1, new int[]{textureId}, 0);
GLES20.glDeleteFramebuffers(mFrameBuffers.length, mFrameBuffers, 0);
GLES20.glDeleteTextures(mFrameBufferTextures.length, mFrameBufferTextures, 0);
}