Image gray Scale (black and white) display in Android development

Source: Internet
Author: User

Before reading this article, the calculation method for converting an RGB color value to a grayscale value can be found in the following:

Three algorithms convert color gray scale http://aiilive.blog.51cto.com/1925756/1718960


The typical application of grayscale display images is the user's avatar, such as the user's online avatar display Color (original), not online display gray (black and white image). A summary is a more original picture to the color of the filter processing to obtain a different display effect of the picture. The API for this method is mainly located in: Android.


use the above mentioned in the " Three algorithms convert color gray scale "The gray scale calculation method mentioned in the article shows the effect of black and white image display as follows:


650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/76/EB/wKioL1ZezfLih9e3AAETwvaOZj4338.png " Title= "Gray_1.png" alt= "Wkiol1zezflih9e3aaetwvaozj4338.png"/>

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/76/EB/wKioL1ZezfPhx5ElAAEM6Es3tBI811.png " Title= "Gray_2.png" alt= "Wkiol1zezfphx5elaaem6es3tbi811.png"/>

Description: The use matrix is implemented using Android ColorMatrix and Colorfilter, where setting ColorMatrix's setsaturation (float Sat) saturation is set to 0 o'clock color filter to show gray levels, The internal implementation of the Android.graphics.ColorMatrix and the specific RGB color weight values are approximately equal to the weights in the BT709 in the graph.


code example (dependency this article The addition of the gray scale calculation Method encapsulation Class)


@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 (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;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 ();         //passing in a number greater than 1 will increase saturation, and passing in a number between 0~1 will reduce the saturation level. A value of 0 will produce a grayscale image         //Android ColorMatrix  the default grayscale calculation using the BT709 standard below         colormatrix.setsaturation (0f);         ColorMatrixColorFilter colorMatrixColorFilter = new  Colormatrixcolorfilter (ColorMatrix);         paint.setcolorfilter ( Colormatrixcolorfilter);         canvas.drawbitmap (bitmap, 0f,  0f, paint);        matrix_imageview.setimagebitmap (Matrix);         //Original Photo         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,&NBSP;XUTILS.GRAYSCALEUTIL.GRAYSCALE.Y);         y_imageview.setimagebitmap (Y);   &nbSp; }    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++) &NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;   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);     &nBsp;           paint.setcolor (Color.argb (A,&NBSP;GC, &NBSP;GC,&NBSP;GC));                 canvas.drawpoint (X, y, paint);             }        }         return rs;    }



For an introduction to ColorMatrix see Android Document:

Local: ${skd}/docs/reference/android/graphics/colormatrix.html

Online: http://developer.android.com/reference/android/graphics/ColorMatrix.html

Reference information:

Image Color processing for Android learning notes (ColorMatrix)

Understanding the use of ColorMatrix and Colormatrixcolorfilter to Modify a drawable ' s Hue

ColorMatrix Operation Vision : colormartrixtest

This article is from the "Red Horse Red" blog, please be sure to keep this source http://aiilive.blog.51cto.com/1925756/1719008

Image gray Scale (black and white) display in Android development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.