It has been a little time since this question was asked, but I came across this in search of something else and, as it turned out, got a solution. You can achieve this without OpenCV or any other third-party library using only the ColorMatrixColorFilter , accessible from API level 1.
Here are the matrices you can use:
//matrix that changes picture into gray scale public static ColorMatrix createGreyMatrix() { ColorMatrix matrix = new ColorMatrix(new float[] { 0.2989f, 0.5870f, 0.1140f, 0, 0, 0.2989f, 0.5870f, 0.1140f, 0, 0, 0.2989f, 0.5870f, 0.1140f, 0, 0, 0, 0, 0, 1, 0 }); return matrix; } // matrix that changes gray scale picture into black and white at given threshold. // It works this way: // The matrix after multiplying returns negative values for colors darker than threshold // and values bigger than 255 for the ones higher. // Because the final result is always trimed to bounds (0..255) it will result in bitmap made of black and white pixels only public static ColorMatrix createThresholdMatrix(int threshold) { ColorMatrix matrix = new ColorMatrix(new float[] { 85.f, 85.f, 85.f, 0.f, -255.f * threshold, 85.f, 85.f, 85.f, 0.f, -255.f * threshold, 85.f, 85.f, 85.f, 0.f, -255.f * threshold, 0f, 0f, 0f, 1f, 0f }); return matrix; }
And here is how to use them:
BitmapFactory.Options options = new BitmapFactory.Options(); options.inScaled = false; //load source bitmap and prepare destination bitmap Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.thePicture, options); Bitmap result = Bitmap.createBitmap(pic.getWidth(), pic.getHeight(), Bitmap.Config.ARGB_8888); Canvas c = new Canvas(result); //first convert bitmap to grey scale: bitmapPaint.setColorFilter(new ColorMatrixColorFilter(createGreyMatrix())); c.drawBitmap(pic, 0, 0, bitmapPaint); //then convert the resulting bitmap to black and white using threshold matrix bitmapPaint.setColorFilter(new ColorMatrixColorFilter(createThresholdMatrix(128))); c.drawBitmap(result, 0, 0, bitmapPaint); //voilΓ ! You can now draw the result bitmap anywhere You want: bitmapPaint.setColorFilter(null); otherCanvas.drawBitmap(result, null, new Rect(x, y, x + size, y + size), bitmapPaint);
Hope this helps someone.