码迷,mamicode.com
首页 > 移动开发 > 详细

Android 开发中图片灰阶(黑白)显示

时间:2015-12-03 02:25:59      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:android 黑白图片

   

     阅读本文之前关于将RGB颜色值转换为灰阶值的计算方法可参见:

     三种算法转换彩色灰阶 http://aiilive.blog.51cto.com/1925756/1718960


     灰阶显示图片的典型应用就是用户头像,如用户在线头像显示彩色(原图),不在线显示灰色(黑白图)。总结一点就是更加一张原始图片来通过颜色的过滤处理计算得到不同显示效果的图片。这方法的API主要位于:android.


       使用上文中提到的“三种算法转换彩色灰阶”一文中提到的灰阶计算方法产生的黑白图片显示效果如下图:


   技术分享

   技术分享

   

     说明:通过Use Matrix是使用Android的ColorMatrix和ColorFilter实现,其中设置ColorMatrix的setSaturation(float sat)饱和度设置为0时颜色过滤之后显示灰阶,android.graphics.ColorMatrix的内部实现和具体RGB颜色权重值近似等于图中BT709中的权重。


   代码示例(依赖此文中附加的灰阶计算方法封装类)

   

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grayscale);
        ButterKnife.bind(this);

        BitmapDrawable bd = (BitmapDrawable) Original_ImageView.getDrawable();
        Bitmap bitmap = bd.getBitmap();
        Log.d(TAG, " w=" + bitmap.getWidth() + ", h=" + bitmap.getHeight() + ", c=" + bitmap.getConfig().toString());

        //0 BT709
        Bitmap matrix = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(matrix);
        Paint paint = new Paint();
        ColorMatrix colorMatrix = new ColorMatrix();
        //传入一个大于1的数字将增加饱和度,而传入一个0~1之间的数字会减少饱和度。0值将产生一幅灰度图像
        //Android ColorMatrix 默认的灰阶计算采用下面的BT709标准
        colorMatrix.setSaturation(0f);
        ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
        paint.setColorFilter(colorMatrixColorFilter);
        canvas.drawBitmap(bitmap, 0f, 0f, paint);
        Matrix_ImageView.setImageBitmap(matrix);

        //原始图片
        Bitmap sunflower = XUtils.BitmapUtil.decodeMutableBitmapFromResourceId(this, R.drawable.sunflower);

        //1
        Bitmap lightness = grayScale(sunflower, XUtils.GrayScaleUtil.GrayScale.Lightness);
        Lightness_ImageView.setImageBitmap(lightness);

        //2
        Bitmap average = grayScale(sunflower, XUtils.GrayScaleUtil.GrayScale.Average);
        Average_ImageView.setImageBitmap(average);

        //3
        Bitmap luminosity = grayScale(sunflower, XUtils.GrayScaleUtil.GrayScale.Luminosity);
        Luminosity_ImageView.setImageBitmap(luminosity);

        //4
        Bitmap bt709 = grayScale(sunflower, XUtils.GrayScaleUtil.GrayScale.BT709);
        BT709_ImageView.setImageBitmap(bt709);

        //5
        Bitmap rmy = grayScale(sunflower, XUtils.GrayScaleUtil.GrayScale.RMY);
        RMY_ImageView.setImageBitmap(rmy);

        //6
        Bitmap y = grayScale(sunflower, XUtils.GrayScaleUtil.GrayScale.Y);
        Y_ImageView.setImageBitmap(y);

    }

    public Bitmap grayScale(final Bitmap bitmap, XUtils.GrayScaleUtil.GrayScale grayScale) {
        if (null == bitmap || null == grayScale) {
            return null;
        }
        Bitmap rs = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(rs);
        Paint paint = new Paint();
        for (int x = 0, w = bitmap.getWidth(); x < w; x++) {
            for (int y = 0, h = bitmap.getHeight(); y < h; y++) {
                int c = bitmap.getPixel(x, y);
                int a = Color.alpha(c);
                int r = Color.red(c);
                int g = Color.red(c);
                int b = Color.blue(c);
                int gc = grayScale.grayScale(r, g, b);
                paint.setColor(Color.argb(a, gc, gc, gc));
                canvas.drawPoint(x, y, paint);
            }
        }
        return rs;
    }

  


   关于ColorMatrix的介绍参见Android document : 

   本地:${SKD}/docs/reference/android/graphics/ColorMatrix.html 

   在线:http://developer.android.com/reference/android/graphics/ColorMatrix.html

   参考信息:

   Android学习笔记之图像颜色处理(ColorMatrix)

   Understanding the Use of ColorMatrix and ColorMatrixColorFilter to Modify a Drawable‘s Hue

  ColorMatrix操作视觉ColorMartrixTest 

本文出自 “野马红尘” 博客,请务必保留此出处http://aiilive.blog.51cto.com/1925756/1719008

Android 开发中图片灰阶(黑白)显示

标签:android 黑白图片

原文地址:http://aiilive.blog.51cto.com/1925756/1719008

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!