Android Image Effects Tutorial - Android Programming Tutorial

0

Android allows you to manipulate images by adding different kinds of effects on the images. You can easily apply image processing techniques to add certain kinds of effects on images. The effects could be brightness,darkness, grayscale conversion e.t.c.
Android provides Bitmap class to handle images. This can be found under android.graphics.bitmap. There are many ways through which you can instantiate bitmap. We are creating a bitmap of image from the imageView.
private Bitmap bmp;
private ImageView img;
img = (ImageView)findViewById(R.id.imageView1);
BitmapDrawable  abmp = (BitmapDrawable)img.getDrawable();
Now we will create bitmap by calling getBitmap() function of BitmapDrawable class. Its syntax is given below:
bmp = abmp.getBitmap();
An image is nothing but a two dimensional matrix. Same way you will handle a bitmap. An image consist of pixels. So you will get pixels from this bitmap and apply processing to it. Its syntax is as follows:
for(int i=0; i
The getWidth() and getHeight() functions returns the height and width of the matrix. The getPixel() method returns the pixel at the specified index. Once you got the pixel, you can easily manipulate it according to your needs.
Apart from these methods, there are other methods that helps us manipulate images more better.
Sr.NoMethod & description
1copy(Bitmap.Config config, boolean isMutable)
This method copy this bitmap's pixels into the new bitmap
2createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config)
Returns a mutable bitmap with the specified width and height
3createBitmap(int width, int height, Bitmap.Config config)
Returns a mutable bitmap with the specified width and height
4createBitmap(Bitmap src)
Returns an immutable bitmap from the source bitmap
5extractAlpha()
Returns a new bitmap that captures the alpha values of the original
6getConfig()
This mehtod eturn that config, otherwise return null
7getDensity()
Returns the density for this bitmap
8getRowBytes()
Return the number of bytes between rows in the bitmap's pixels
9setPixel(int x, int y, int color)
Write the specified Color into the bitmap (assuming it is mutable) at the x,y coordinate
10setDensity(int density)
This method specifies the density for this bitmap

Example

The below example demonstrates some of the image effects on the bitmap. It crates a basic application that allows you to convert the picture into grayscale and much more.
To experiment with this example , you need to run this on an actual device.
StepsDescription
1You will use Eclipse IDE to create an Android application and name it as ImageEffects under a package com.example.imageeffects. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.
2Modify src/MainActivity.java file to add necessary code.
3Modify the res/layout/activity_main to add respective XML components
4Modify the res/values/string.xml to add necessary string components
5Run the application and choose a running android device and install the application on it and verify the results
Following is the content of the modifed main activity filesrc/com.example.imageeffects/MainActivity.java.
package com.example.imageeffects;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {

   private ImageView img;
   private Bitmap bmp;
   private Bitmap operation;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   img = (ImageView)findViewById(R.id.imageView1);
   BitmapDrawable  abmp = (BitmapDrawable)img.getDrawable();
   bmp = abmp.getBitmap();

   }

   public void gray(View view){
      operation= Bitmap.createBitmap(bmp.getWidth(),
      bmp.getHeight(),bmp.getConfig());

      double red = 0.33;
      double green = 0.59;
      double blue = 0.11;

   for(int i=0; i
Following is the modified content of the xml res/layout/activity_main.xml.


   
Following is the content of the res/values/string.xml.



   ImageEffects
   Settings
   Hello world!
   Gray
   bright
   dark


Following is the content of AndroidManifest.xml file.



   

   
      
         
            

            
         
      
   


Let's try to run our Image Effects application we just modified. I assume you had created your AVD while doing environment setup. To run the app from Eclipse, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Eclipse installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window:
Now if you will look at your device screen , you will see the an image of android along with three buttons.
Now just select the gray button that will convert your image into grayscale and will update the UI. It is shown below:
Now tap on the bright button, that will add some value to each pixel of the image and thus makes an illusion of brightness. It is shown below:
Now tap on the dark button, that will subtract some value to each pixel of the image and thus makes an illusion of dark. It is shown below:

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Good readers always drop comments!!

Good readers always drop comments!!

Post a Comment (0)

buttons=(Accept !) days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top