Saturday 29 June 2013

Before Gray Scale


After Gray Scale


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(grayScaleImage(BitmapFactory.decodeResource(           getResources(), R.drawable.android_droid)));
 }
 public static Bitmap grayScaleImage(Bitmap src) {
  // constant factors
  final double GS_RED = 0.299;
  final double GS_GREEN = 0.587;
  final double GS_BLUE = 0.114;

  // create output bitmap
  Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
  // pixel information
  int A, R, G, B;
  int pixel;

  // get image size
  int width = src.getWidth();
  int height = src.getHeight();

  // scan through every single pixel
  for(int x = 0; x < width; ++x) {
   for(int y = 0; y < height; ++y) {
    // get one pixel color
    pixel = src.getPixel(x, y);
    // retrieve color of all channels
    A = Color.alpha(pixel);
    R = Color.red(pixel);
    G = Color.green(pixel);
    B = Color.blue(pixel);
    // take conversion up to one single value
    R = G = B = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
    // set new pixel color to output bitmap
    bmOut.setPixel(x, y, Color.argb(A, R, G, B));
   }
  }

  // return final image
  return bmOut;
 }

}

  3. note that:

  • Grayscale image is an image that each pixel holds a single sample, only intensity information, also known as black-and-white image.
  • You might want to refer about Greyscale Image on Wikipedia and how it is implemented in GIMP.
  • The intensity is calculated by common formula: 30% of Red + 59% of Green + 11 % of Blue.
  • with the help of this,grayScaleImage() method you can Gray Scale image on click,on action_down etc
  4. conclusion:

  • know the calculated formula of grayscale.
  • some deep information about gray scale image.
  • Know how to gray scale an 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