How to save an image after applying an effect (filter) to it ?

  • Replies:0
Rizwan Ahmed
  • Forum posts: 1

Sep 9, 2015, 6:25:44 PM via Website

Hello,

I have successfully applied some filters to an image and after that I need to save it ? Can any one please guide me in it ? I tried it by taking the screenshot of the rendered view but I know that there is some other way out. The take screen shot function takes the screen shot but it takes the entire screen. I need a way which will only save the image after applying effects to it.

Here is the code

package com.example.android.mediafx;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.media.effect.Effect;
import android.media.effect.EffectContext;
import android.media.effect.EffectFactory;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;
import android.widget.Button;

import java.io.BufferedOutputStream;
import java.nio.ByteOrder;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.Random;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class HelloEffects extends Activity implements GLSurfaceView.Renderer {

private GLSurfaceView mEffectView;
private int[] mTextures = new int[2];
private EffectContext mEffectContext;
private Effect mEffect;
private TextureRenderer mTexRenderer = new TextureRenderer();
private int mImageWidth;
private int mImageHeight;
private boolean mInitialized = false;
int mCurrentEffect;
private volatile boolean saveFrame;
private int rsum;
private int bsum;
private int gsum;
private int pixel;
private int original_rsum;
private int original_bsum;
private int original_gsum;
private int original_pixel;
Button button;

public void setCurrentEffect(int effect) {
    mCurrentEffect = effect;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);
    /**
     * Initialise the renderer and tell it to only render when
     * Explicit requested with the RENDERMODE_WHEN_DIRTY option
     */
    mEffectView = (GLSurfaceView) findViewById(R.id.effectsview);
    mEffectView.setEGLContextClientVersion(2);
    mEffectView.setRenderer(this);
    mEffectView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    mCurrentEffect = R.id.autofix;
    openCamera();
}

private void openCamera() {


    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 0);

        }
    });
}

private void loadTextures() {
    // Generate textures
    GLES20.glGenTextures(2, mTextures, 0);

    // Load input bitmap
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.puppy1);
    mImageWidth = bitmap.getWidth();
    mImageHeight = bitmap.getHeight();
    mTexRenderer.updateTextureSize(mImageWidth, mImageHeight);
    //mTexRendere enga declared



    // Upload to texture
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[0]);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

    // Set texture parameters
    GLToolbox.initTexParams();
}


private void initEffect() {
    EffectFactory effectFactory = mEffectContext.getFactory();
    if (mEffect != null) {
        mEffect.release();
    }
    /**
     * Initialize the correct effect based on the selected menu/action item
     */

    mEffect = effectFactory.createEffect(
            EffectFactory.EFFECT_AUTOFIX);
    mEffect.setParameter("scale", 0.5f);


}

private void applyEffect() {
    mEffect.apply(mTextures[0], mImageWidth, mImageHeight, mTextures[1]);
}

private void renderResult() {
    if (mCurrentEffect == R.id.autofix) {
        saveFrame = true;
        // if no effect is chosen, just render the original bitmap
        mTexRenderer.renderTexture(mTextures[1]);
    } else {
      //  saveFrame = true;
        // render the result of applyEffect()
        mTexRenderer.renderTexture(mTextures[0]);
    }
}

@Override
public void onDrawFrame(GL10 gl) {
    if (!mInitialized) {
        //Only need to do this once
        mEffectContext = EffectContext.createWithCurrentGlContext();
        mTexRenderer.init();
        loadTextures();
        mInitialized = true;
    }
    if (mCurrentEffect != R.id.none) {
        //if an effect is chosen initialize it and apply it to the texture
        initEffect();



        applyEffect();
    }
    renderResult();
    if (saveFrame) {
        saveBitmap(takeScreenshot(gl));

    }
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    if (mTexRenderer != null) {
        mTexRenderer.updateViewSize(width, height);
    }
}

private void saveBitmap(Bitmap bitmap) {
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/images");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-" + n + ".jpg";
    File file = new File(myDir, fname);
    if (file.exists()) file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
        Log.i("TAG", "Image SAVED==========" + file.getAbsolutePath());
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public Bitmap takeScreenshot(GL10 mGL) {

    final int mWidth = mEffectView.getWidth() ;
    final int mHeight =mEffectView.getHeight();
    final int startx=mHeight - mImageHeight;
    IntBuffer ib = IntBuffer.allocate(mWidth * mHeight);
    IntBuffer ibt = IntBuffer.allocate(mWidth * mHeight);
    mGL.glReadPixels(0,startx, mWidth, mHeight, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);


    // Convert upside down mirror-reversed image to right-side up normal
    // image.
    for (int i = 0; i < mHeight; i++) {
        for (int j = 0; j < mWidth; j++) {
            ibt.put((mHeight - i - 1) * mWidth + j, ib.get(i * mWidth + j));
        }
    }

    Bitmap mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
    mBitmap.copyPixelsFromBuffer(ibt);



    return mBitmap;
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}

}

Reply