Search in sources :

Example 1 with MagicBeautyFilter

use of com.seu.magicfilter.filter.advanced.MagicBeautyFilter in project MagicCamera by wuhaoyu1990.

the class MagicCameraView method drawPhoto.

private Bitmap drawPhoto(Bitmap bitmap, boolean isRotated) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int[] mFrameBuffers = new int[1];
    int[] mFrameBufferTextures = new int[1];
    if (beautyFilter == null)
        beautyFilter = new MagicBeautyFilter();
    beautyFilter.init();
    beautyFilter.onDisplaySizeChanged(width, height);
    beautyFilter.onInputSizeChanged(width, height);
    if (filter != null) {
        filter.onInputSizeChanged(width, height);
        filter.onDisplaySizeChanged(width, 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]);
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, mFrameBufferTextures[0], 0);
    GLES20.glViewport(0, 0, width, height);
    int textureId = OpenGlUtils.loadTexture(bitmap, OpenGlUtils.NO_TEXTURE, true);
    FloatBuffer gLCubeBuffer = ByteBuffer.allocateDirect(TextureRotationUtil.CUBE.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
    FloatBuffer gLTextureBuffer = ByteBuffer.allocateDirect(TextureRotationUtil.TEXTURE_NO_ROTATION.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
    gLCubeBuffer.put(TextureRotationUtil.CUBE).position(0);
    if (isRotated)
        gLTextureBuffer.put(TextureRotationUtil.getRotation(Rotation.NORMAL, false, false)).position(0);
    else
        gLTextureBuffer.put(TextureRotationUtil.getRotation(Rotation.NORMAL, false, true)).position(0);
    if (filter == null) {
        beautyFilter.onDrawFrame(textureId, gLCubeBuffer, gLTextureBuffer);
    } else {
        beautyFilter.onDrawFrame(textureId);
        filter.onDrawFrame(mFrameBufferTextures[0], gLCubeBuffer, gLTextureBuffer);
    }
    IntBuffer ib = IntBuffer.allocate(width * height);
    GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, ib);
    Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    result.copyPixelsFromBuffer(ib);
    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);
    beautyFilter.destroy();
    beautyFilter = null;
    if (filter != null) {
        filter.onDisplaySizeChanged(surfaceWidth, surfaceHeight);
        filter.onInputSizeChanged(imageWidth, imageHeight);
    }
    return result;
}
Also used : MagicBeautyFilter(com.seu.magicfilter.filter.advanced.MagicBeautyFilter) Bitmap(android.graphics.Bitmap) IntBuffer(java.nio.IntBuffer) FloatBuffer(java.nio.FloatBuffer)

Aggregations

Bitmap (android.graphics.Bitmap)1 MagicBeautyFilter (com.seu.magicfilter.filter.advanced.MagicBeautyFilter)1 FloatBuffer (java.nio.FloatBuffer)1 IntBuffer (java.nio.IntBuffer)1