Android Image Effects

Android Image Effects

Android Image Effects – You know well that how is android image effects, you have must use of image editing software or android apps to edit your photos, as I mentioned android photo editing apps in the previous tutorial. these apps are can be used to edit photos, but you don’t know how it works and how you can develop your own android editing app from scratch.

so, if you love to develop and love coding that this will be so easy for you. in this Android Image Effects tutorial I developed a simple android image effects app with the help of this tutorial you may develop your own standard android editing app. as the tutorial’s title it confirms that it is only for image effects. you can only change the image effects of your picture. so let’s start our tutorial. i will provide photo effects android source code.

android image effects

Android gives you to edit images with the addition of different sorts of results on the images. You may easily apply image finalizing ways to add certain sorts of results on images. The consequences could be brightness,darkness, grayscale transformation and so on.

What is the use of bitmap in android &

How to Use bitmap In Android

Android os provides Bitmap course to edit or handle images. This is found under google android.graphics.bitmap. There are several ways by which you can instantiate bitmap. A bitmap has been created by us 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 generate bitmap by getting in touch with getBitmap() function of BitmapDrawable class. Its syntax is listed below

bmp = abmp.getBitmap();

A pic is only a two dimensional matrix. Same manner you shall deal with a bitmap. An image contain pixels. Which means you are certain to get pixels out of this bitmap and apply handling to it. Its syntax is really as follows. We used for loop in this syntax.

for(int i=0; i<bmp.getWidth(); i++){
   for(int j=0; j<bmp.getHeight(); j++){
      int p = bmp.getPixel(i, j);
   }
}

The getWidth() and getHeight() functions profits the height and width of the matrix. The getPixel() method results the pixel at the particular index. Once you have the pixel, you can certainly manipulate it according to your needs.

From these methods apart, there are other methods that helps us change images more better.

Methods

  1. copy(Bitmap.Config config, boolean isMutable)
  2. createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config)
  3. createBitmap(int width, int height, Bitmap.Config config)
  4. createBitmap(Bitmap src)
  5. extractAlpha()
  6. getConfig()
  7. getDensity()
  8. getRowBytes()
  9. setPixel(int x, int y, int color)
  10. setDensity(int density)

Android Image Effects Example

The below android image effects example shows a few of the image results on the bitmap. It crates a simple application which allows that you convert the picture into grayscale plus much more.

To test out this android image effects example , you will need to perform this on a genuine device.

Steps     Description

  1. You use Android studio to build an Android software under a deal com.example.sappal.myapplication.
  2. Modify src/MainActivity.java data file to include necessary code.
  3. Modify the res/layout/activity_main to include respected XML components
  4. Run the application form and choose a operating google android device and set up the application onto it and confirm the results

Following is this content of the customized MainActivity.java.

package com.example.sappal.myapplication;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {
   Button b1, b2, b3;
   ImageView im;
   
   private Bitmap bmp;
   private Bitmap operation;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      b1 = (Button) findViewById(R.id.button);
      b2 = (Button) findViewById(R.id.button2);
      b3 = (Button) findViewById(R.id.button3);
      im = (ImageView) findViewById(R.id.imageView);
      
      BitmapDrawable abmp = (BitmapDrawable) im.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 < bmp.getWidth(); i++) {
         for (int j = 0; j < bmp.getHeight(); j++) {
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            
            r = (int) red * r;
            g = (int) green * g;
            b = (int) blue * b;
            operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
         }
      }
      im.setImageBitmap(operation);
   }
   
   public void bright(View view){
      operation= Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),bmp.getConfig());
      
      for(int i=0; i<bmp.getWidth(); i++){
         for(int j=0; j<bmp.getHeight(); j++){
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            int alpha = Color.alpha(p);
            
            r = 100  +  r;
            g = 100  + g;
            b = 100  + b;
            alpha = 100 + alpha;
            operation.setPixel(i, j, Color.argb(alpha, r, g, b));
         }
      }
      im.setImageBitmap(operation);
   }
   
   public void dark(View view){
      operation= Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(),bmp.getConfig());
      
      for(int i=0; i<bmp.getWidth(); i++){
         for(int j=0; j<bmp.getHeight(); j++){
            int p = bmp.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 - 50;
            g =  g - 50;
            b =  b - 50;
            alpha = alpha -50;
            operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
         }
      }
      im.setImageBitmap(operation);
   }
   
   public void gama(View view) {
      operation = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(),bmp.getConfig());
      
      for(int i=0; i<bmp.getWidth(); i++){
         for(int j=0; j<bmp.getHeight(); j++){
            int p = bmp.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 + 150;
            g =  0;
            b =  0;
            alpha = 0;
            operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
         }
      }
      im.setImageBitmap(operation);
   }
   
   public void green(View view){
      operation = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(), bmp.getConfig());
      
      for(int i=0; <bmp.getWidth(); i++){
         for(int j=0; j<bmp.getHeight(); j++){
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            int alpha = Color.alpha(p);
            
            r =  0;
            g =  g+150;
            b =  0;
            alpha = 0;
            operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
         }
      }
      im.setImageBitmap(operation);
   }
   
   public void blue(View view){
      operation = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(), bmp.getConfig());
      
      for(int i=0; i<bmp.getWidth(); i++){
         for(int j=0; j<bmp.getHeight(); j++){
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            int alpha = Color.alpha(p);
            
            r =  0;
            g =  0;
            b =  b+150;
            alpha = 0;
            operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
         }
      }
      im.setImageBitmap(operation);
   }
}

Modified Content -> xml res/layout/activity_main.xml.

Here abc indicates about logo of http://www.androdevelopment.com

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" 
   android:layout_width="match_parent"
   android:layout_height="match_parent" 
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textView"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="30dp"
      android:text="Image Effects" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Andro Development"
      android:id="@+id/textView2"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:textSize="35dp"
      android:textColor="#ff16ff01" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:layout_below="@+id/textView2"
      android:layout_centerHorizontal="true"
      android:src="@drawable/abc"/>
   
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Gray"
      android:onClick="gray"
      android:id="@+id/button"
      android:layout_alignParentBottom="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_marginBottom="97dp" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="dark"
      android:onClick="dark"
      android:id="@+id/button2"
      android:layout_alignBottom="@+id/button"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Bright"
      android:onClick="bright"
      android:id="@+id/button3"
      android:layout_alignTop="@+id/button2"
      android:layout_centerHorizontal="true" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Red"
      android:onClick="gama"
      android:id="@+id/button4"
      android:layout_below="@+id/button3"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Green"
      android:onClick="green"
      android:id="@+id/button5"
      android:layout_alignTop="@+id/button4"
      android:layout_alignLeft="@+id/button3"
      android:layout_alignStart="@+id/button3" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="blue"
      android:onClick="blue"
      android:id="@+id/button6"
      android:layout_below="@+id/button2"
      android:layout_toRightOf="@+id/textView"
      android:layout_toEndOf="@+id/textView" />
      
</RelativeLayout>

Content -> AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sappal.myapplication" >
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
   
      <activity
         android:name=".MainActivity"
         android:label="@string/app_name" >
      
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
   </application>
</manifest>

 

Let’s make an effort to run our program we just changed. I assume you’d created your AVD while doing Android Environment Setup. To perform this android image effects app from Android studio room, available one of your project’s activity files and click Run Eclipse Run icon from the toolbar. Android os studio room installs the software on your AVD and begins it in case everything is okay with your installation and application, it’ll screen pursuing Emulator screen

android image effects

If you can look at the device display now , you shall start to see the a graphic of android along with three switches/buttons.

Now just choose the Grey button that will convert your image into grayscale and can revise the UI. It really is shown below

android image effects

Now touch on the gray button, that will then add value to each pixel of the image and so makes an illusion of lighting. It really is shown below

android image effects

Touch on the dark button now, that will subtract some value to each pixel of the image and so makes an illusion of deep. It really is shown below

android image effects

Now touch on the red button, that will subtract some value to each pixel of the image and so makes an illusion of deep. It really is shown below

android image effects

Touch on the green button now, that will subtract some value to each pixel of the image and so makes an illusion of deep. It really is shown below

android image effects

Now touch on the blue button, that will subtract some value to each pixel of the image and so makes an illusion of deep. It really is shown below

android image effects