Convert Bitmap To Gray Scale In Android
Posted by blogmeister on
June 15, 2012
The best code in Android that worked for me in converting a colored Bitmap to gray scale is this one.
public static Bitmap convertToGrayScale(Bitmap src){ ColorMatrix bwMatrix = new ColorMatrix(); bwMatrix.setSaturation(0); final ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(bwMatrix); Bitmap rBitmap = src.copy(Bitmap.Config.ARGB_8888, true); Paint paint = new Paint(); paint.setColorFilter(colorFilter); Canvas myCanvas = new Canvas(rBitmap); myCanvas.drawBitmap(rBitmap, 0, 0, paint); src.recycle(); src = null; return rBitmap; }
Notice that before the object is returned, I added two lines of code src.recycle() and src = null? This is to ensure that the non-used Bitmap object will be recycled to help avoid OutOfMemory exception errors.
Now, this is a necessity because unless you restrict your app to only run in smart phones with large RAM, you have to take this into account in order for your app to run seamlessly on smart phones that do not have have large amounts of RAM.
Donations appreciated. Every little $ helps. Or click Google +1.









