convert bitmap to grey scale - Android android.graphics

Android examples for android.graphics:Bitmap Convert

Description

convert bitmap to grey scale

Demo Code

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;

public class Main {

  /**/*from ww w .  ja va  2  s .c om*/
   * convert bitmap to grey scale
   * 
   * @param bm
   *          bitmap we want to convert
   * @return bitmap in greyscale
   */
  public static Bitmap convertToGrayscale(Bitmap bm) {
    Bitmap result = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.RGB_565);
    Canvas c = new Canvas(result);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(bm, 0, 0, paint);
    bm.recycle();
    return result;
  }

}

Related Tutorials