Created
May 23, 2015 18:39
-
-
Save moneytoo/87e3772c821cb1e86415 to your computer and use it in GitHub Desktop.
Efficiently invert bitmap on Android
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 Bitmap invert(Bitmap src) | |
{ | |
int height = src.getHeight(); | |
int width = src.getWidth(); | |
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); | |
Canvas canvas = new Canvas(bitmap); | |
Paint paint = new Paint(); | |
ColorMatrix matrixGrayscale = new ColorMatrix(); | |
matrixGrayscale.setSaturation(0); | |
ColorMatrix matrixInvert = new ColorMatrix(); | |
matrixInvert.set(new float[] | |
{ | |
-1.0f, 0.0f, 0.0f, 0.0f, 255.0f, | |
0.0f, -1.0f, 0.0f, 0.0f, 255.0f, | |
0.0f, 0.0f, -1.0f, 0.0f, 255.0f, | |
0.0f, 0.0f, 0.0f, 1.0f, 0.0f | |
}); | |
matrixInvert.preConcat(matrixGrayscale); | |
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrixInvert); | |
paint.setColorFilter(filter); | |
canvas.drawBitmap(src, 0, 0, paint); | |
return bitmap; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment