Download Code

Bitmap Image Effects Tutorial Android

Image Editting is a trend nowadays because people like their photos in attractive looks.In this tutorial you will learn how to edit/apply image effects in android application.You will deal with Bitmap Image and it’s concerning methods in this tutorial.

So,let me give you the steps you will perform in this tutorial.

  • Pick Image from gallery.
  • Open edit options from menu.
  • Apply effect on that image.

Let’s start,Create new android application project.Go to activity_main.xml and add ImageView,Button.

activity_main.xml

image effects,bitmap image,bitmap,gallery intent,ImageView,Button,how to pick image from gallery android tutorial, bitmapdrawable,getBitmap,getWidth,getHeight,Color,setPixel,setImageBitmap, getconfig,createBitmap,how to create bitmap in android tutorial, how to edit image in android tutorial, how to apply effect on image in android tutorial, gallery intent,intent,uri,cursor,bitmapfactory,decodefile, how to set bitmap to imageview android tutorial,

Set Menu options go to menu > main.xml.

image effects,bitmap image,bitmap,gallery intent,ImageView,Button,how to pick image from gallery android tutorial, bitmapdrawable,getBitmap,getWidth,getHeight,Color,setPixel,setImageBitmap, getconfig,createBitmap,how to create bitmap in android tutorial, how to edit image in android tutorial, how to apply effect on image in android tutorial, gallery intent,intent,uri,cursor,bitmapfactory,decodefile, how to set bitmap to imageview android tutorial,

Go to MainActivity.java and do the following steps.

  • Create UI Component variables and Refer their id’s.
  • Apply click event to open gallery using ACTION_PICK intent constant from MediaStore.
  • Apply effects on image by converting/Manipulating/Bitmap Image.

MainActivity.java

package developer.codecimageeffects;


import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends Activity implements OnClickListener{
	Button pickImageBtn;
	ImageView pickedImage;
	Bitmap bitImage;
	Bitmap bitImg;
	Bitmap operations;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initialize();
		
	}
	public void initialize(){
		pickedImage = (ImageView) findViewById(R.id.image);
		pickImageBtn = (Button) findViewById(R.id.pickImage);
		pickImageBtn.setOnClickListener(this);
		
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.pickImage:
			Intent p = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			startActivityForResult(p, 1);
			break;
		}
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
			Uri selectedImage = data.getData();
			String[] filePathColumn = { MediaStore.Images.Media.DATA };

			Cursor cursor = getContentResolver().query(selectedImage,
					filePathColumn, null, null, null);
			cursor.moveToFirst();

			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
			String picturePath = cursor.getString(columnIndex);
			cursor.close();
			pickedImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));

		}

	}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
        case R.id.Colored_Negative:
        	BitmapDrawable bd = (BitmapDrawable) pickedImage.getDrawable();    // get the image for customization
        	bitImg = bd.getBitmap();    // rendering bitmap
        	// create new bitmap
        	operations = Bitmap.createBitmap(bitImg.getWidth(),bitImg.getHeight(), bitImg.getConfig());
            // loops to get individual pixel and change it's color
            for (int i = 0; i < bitImg.getWidth(); i++) {
               for (int j = 0; j < bitImg.getHeight(); j++) {
                  int p = bitImg.getPixel(i, j);
                  int r = Color.red(p);
                  int g = Color.green(p);
                  int b = Color.blue(p);
                  int alpha = Color.alpha(p); // setting pixel alpha individual
                  //subtracting value from pixel to change color
                  r =  r - 70;    
                  g =  g - 60 ;
                  b =  b - 50;
                  //changing alpha means opacity
                  alpha =alpha -60;
                  // now setting pixel into a bitmap
                  operations.setPixel(i, j, Color.argb(alpha, r, g, b));  //applying image effects
               }
            }
            pickedImage.setImageBitmap(operations); // setting bitmap Image
        	break;
        case R.id.negative:
        	BitmapDrawable bD = (BitmapDrawable) pickedImage.getDrawable();
        	bitImg = bD.getBitmap();
        	operations = Bitmap.createBitmap(bitImg.getWidth(),bitImg.getHeight(), bitImg.getConfig());
       
            for (int i = 0; i < bitImg.getWidth(); i++) {
               for (int j = 0; j < bitImg.getHeight(); j++) {
                  int p = bitImg.getPixel(i, j);
                  int r = Color.red(p);
                  int g = Color.green(p);
                  int b = Color.blue(p);
                  int alpha = Color.alpha(p);
                  
                  r = r - 70;
                  g = g - 70;
                  b = b - 70;
                  alpha = alpha - 60;
                  operations.setPixel(i, j, Color.argb(alpha, r, g, b));  //applying image effects
               }
            }
            pickedImage.setImageBitmap(operations);
        	break;
        }
        return super.onOptionsItemSelected(item);
    }
}

Add WRITE_EXTERNAL_STROAGE permission to Android Manifest.xml.

image effects,bitmap image,bitmap,gallery intent,ImageView,Button,how to pick image from gallery android tutorial, bitmapdrawable,getBitmap,getWidth,getHeight,Color,setPixel,setImageBitmap, getconfig,createBitmap,how to create bitmap in android tutorial, how to edit image in android tutorial, how to apply effect on image in android tutorial, gallery intent,intent,uri,cursor,bitmapfactory,decodefile, how to set bitmap to imageview android tutorial,

Run your android application project.I have taken a random picture.

image effects,bitmap image,bitmap,gallery intent,ImageView,Button,how to pick image from gallery android tutorial, bitmapdrawable,getBitmap,getWidth,getHeight,Color,setPixel,setImageBitmap, getconfig,createBitmap,how to create bitmap in android tutorial, how to edit image in android tutorial, how to apply effect on image in android tutorial, gallery intent,intent,uri,cursor,bitmapfactory,decodefile, how to set bitmap to imageview android tutorial,

That’s it for this tutorial.

Thanks.