android图片的灰度化、线性灰度、二值化处理

在网上看视频教程,根据里面的理解,写了一个简单的方法。

	/**
	 * 对图片进行灰度化处理
	 * @param 原始图片
	 * @return 灰度化图片
	 */
	public static Bitmap getGrayBitmap(Bitmap bm){
		Bitmap bitmap = null;
		 //获取图片的宽和高
		int width = bm.getWidth();
		int height = bm.getHeight();
		//创建灰度图片
		bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
		//创建画布
		Canvas canvas = new Canvas(bitmap);
		//创建画笔
		Paint paint = new Paint();
		//创建颜色矩阵
		ColorMatrix matrix = new ColorMatrix();
		//设置颜色矩阵的饱和度:0代表灰色,1表示原图
		matrix.setSaturation(0);
		//颜色过滤器
		ColorMatrixColorFilter cmcf = new ColorMatrixColorFilter(matrix);
		//设置画笔颜色过滤器  
		paint.setColorFilter(cmcf);
		//画图
		canvas.drawBitmap(bm, 0,0, paint);
		return bitmap;
	}
/**
     * 线性灰度变化
     * @param image        原图片
     * @return             线性灰度化的图片
     */
    public Bitmap getLineGrey(Bitmap image)
    {
        //得到图像的宽度和长度
        int width = image.getWidth();
        int height = image.getHeight();
        //创建线性拉升灰度图像
        Bitmap linegray = null;
        linegray = image.copy(Bitmap.Config.ARGB_8888, true);
        //依次循环对图像的像素进行处理
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                //得到每点的像素值
                int col = image.getPixel(i, j);
                int alpha = col & 0xFF000000;
                int red = (col & 0x00FF0000) >> 16;
                int green = (col & 0x0000FF00) >> 8;
                int blue = (col & 0x000000FF);
                // 增加了图像的亮度
                red = (int) (1.1 * red + 30);
                green = (int) (1.1 * green + 30);
                blue = (int) (1.1 * blue + 30);
                //对图像像素越界进行处理
                if (red >= 255)
                {
                    red = 255;
                }

                if (green >= 255) {
                    green = 255;
                }

                if (blue >= 255) {
                    blue = 255;
                }
                // 新的ARGB
                int newColor = alpha | (red << 16) | (green << 8) | blue;
                //设置新图像的RGB值
                linegray.setPixel(i, j, newColor);
            }
        }
        return linegray;
    }
/**
     * 对图片进行二值化处理
     * @param graymap         原图
     * @return                二值化处理后的图
     */
    public Bitmap getGray2Binary(Bitmap graymap) {
        //得到图形的宽度和长度
        int width = graymap.getWidth();
        int height = graymap.getHeight();
        //创建二值化图像
        Bitmap binarymap = null;
        binarymap = graymap.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;
    }
发布了305 篇原创文章 · 获赞 261 · 访问量 184万+

猜你喜欢

转载自blog.csdn.net/chenguang79/article/details/100580407