直接上代码了
/** * 图片灰度化和二值化处理 * Created by kaifa on 2016/2/14. */public class ImageFilter { /** * 图片灰度处理 * * @return */ public static Bitmap grayScale(Bitmap bitmap) { int width, height; height = bitmap.getHeight(); width = bitmap.getWidth(); Bitmap bmpGray = null; bmpGray = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(bmpGray); Paint paint = new Paint(); ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); paint.setColorFilter(f); c.drawBitmap(bitmap, 0, 0, paint); return bmpGray; } /** * 图片二值化处理 * * @param bitmap * @return */ public static Bitmap binaryzation(Bitmap bitmap) { //得到图形的宽度和长度 int width = bitmap.getWidth(); int height = bitmap.getHeight(); //创建二�?化图像 �? Bitmap binarymap = null; binarymap = bitmap.copy(Bitmap.Config.ARGB_8888, true); //依次循环,对图像的像素进行处理 �? for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { //得到当前像素的�? int col = binarymap.getPixel(i, j); //得到alpha通道的�? int alpha = col & 0xFF000000; //得到图像的像素RGB的�? int red = (col & 0x00FF0000) >> 16; int green = (col & 0x0000FF00) >> 8; int blue = (col & 0x000000FF); // 用公式X = 0.3×R+0.59×G+0.11×B计算出X代替原来的RGB int gray = (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11); //对图像进行二值化处理 if (gray <= 95) { gray = 0; } else { gray = 255; } // 新的ARGB int newColor = alpha | (gray << 16) | (gray << 8) | gray; //设置新图像的当前像素值 �? binarymap.setPixel(i, j, newColor); } } return binarymap; }}
忘了在哪里找的了,反正分享出来吧