Skip to content

Instantly share code, notes, and snippets.

@moondroid
Created March 12, 2015 09:15
Show Gist options
  • Save moondroid/c824ae418e83669583ff to your computer and use it in GitHub Desktop.
Save moondroid/c824ae418e83669583ff to your computer and use it in GitHub Desktop.
Transform Bitmap to grayscale
public static Bitmap toGrayScale(Bitmap bmpOriginal) {
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
bmpOriginal.recycle();
return bmpGrayscale;
}
@rayworks
Copy link

rayworks commented Aug 9, 2018

@arifbd
It seems the original bitmap is quite large. Please check the width and height before you call the method Bitmap.createBitmap(...).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment