Tuesday 9 July 2013

Before Sepia-toning Effect


After Sepia-toning Effect

more red

more blue

more red,blue

more red,green
Create new Android Project

Project Name: PlayingwithBitmaps
Build Target: Android 2.3.3   //or greater than that
Application Name: PlayingwithBitmaps
Package Name: com.hamad.playingwithbitmaps
Create Activity: Main
Min SDK: 10 // or greater than that


  1. create main layout:
  • One image view to display the image.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:id="@+id/rlMain" >

    <ImageView
        android:id="@+id/imViewAndroid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/beautiful" />

</RelativeLayout>

 2. code of main activity:

package com.shaikhhamadali.blogspot.playingwithbitmaps;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.widget.ImageView;

public class Main extends Activity {
 ImageView imViewAndroid;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  imViewAndroid = (ImageView) findViewById(R.id.imViewAndroid);
  imViewAndroid.setImageBitmap(applySepiaToningEffect(BitmapFactory.decodeResource(getResources(), R.drawable.android_droid),50,2.2, 0, 2.2));
 }

 public Bitmap applySepiaToningEffect(Bitmap src, int depth, double red, double green, double blue) {
  // source image size
  int width = src.getWidth();
  int height = src.getHeight();
  // create output bitmap
  Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
  // constant grayscale
  final double GS_RED = 0.3;
  final double GS_GREEN = 0.59;
  final double GS_BLUE = 0.11;
  // color information
  int A, R, G, B;
  int pixel;

  // scan through all pixels of image
  for(int x = 0; x < width; ++x) {
   for(int y = 0; y < height; ++y) {
    // get pixel color
    pixel = src.getPixel(x, y);
    // get color on each channel
    A = Color.alpha(pixel);
    R = Color.red(pixel);
    G = Color.green(pixel);
    B = Color.blue(pixel);
    // apply grayscale sample
    B = G = R = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);

    // apply intensity level for sepia-toning on each channel
    R += (depth * red);
    if(R > 255) { R = 255; }

    G += (depth * green);
    if(G > 255) { G = 255; }

    B += (depth * blue);
    if(B > 255) { B = 255; }

    // set new pixel color to output image
    bmOut.setPixel(x, y, Color.argb(A, R, G, B));
   }
  }

  // return final image
  return bmOut;
 }

}


Sepia-toning effect is used very commonly in photography.It is the process of changing the intensity on every pixel color of a gray-scale image, or so-called black-and-white.

  3. note that:

  • with the help of this,applySepiaToningEffect() method you can set the sepia-toning effect of image on click,on action_down etc

 4. conclusion:

  • some deep information about color sepia-toning Effect.
  • Know how to set sepia-toning Effect on image bitmap from drawables.
  5. About the post:

  •  The code seems to explain itself due to comments, and is very easy to understand.
  •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
  •  Hope you enjoy it!

6. Source Code:
        you can download the source code here

Cheers,
Hamad Ali Shaikh