Created
March 12, 2015 09:15
-
-
Save moondroid/c824ae418e83669583ff to your computer and use it in GitHub Desktop.
Transform Bitmap to grayscale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
@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
I use your method, but i got this error on (Samsung Galaxy Grand Prime, 1024MB RAM, Android 5.0) and (Samsung Galaxy J7, 1536MB RAM, Android 6.0)
java.lang.OutOfMemoryError:
...
...
...
at packageName.toGrayscale (Conversions.java:32)
...
...
...
the code of 32 no lines are given below.
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);