convert bitmap to grey scale - Android Graphics

Android examples for Graphics:Bitmap Color

Description

convert bitmap to grey scale

Demo Code


//package com.java2s;

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 .j  av  a 2 s .co  m*/
     * 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