Learn how to remove colors from images by using android, return grayscale images, and use the setSaturation method in ColorMatrix (34 ),

Source: Internet
Author: User

Learn how to remove colors from images by using android, return grayscale images, and use the setSaturation method in ColorMatrix (34 ),

Source image:


:



To achieve the above results is actually very simple, directly go to the Code:

public class MainActivity extends Activity {private Button btn_start;private ImageView img;private Bitmap bitmap;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.image_layout);initView();}private void initView() {img = (ImageView) findViewById(R.id.iv_image);btn_start = (Button) findViewById(R.id.btn_start);bitmap = ((BitmapDrawable) img.getDrawable()).getBitmap();btn_start.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Bitmap bm = ImageTools.toGrayscale(bitmap);img.setImageBitmap(bm);}});}}

Let's take a look at the methods in the ImageTools tool class:

/***** // **** Remove the color of the image, return the grayscale image ** @ param bmp original *. The imported image * @ return the de-colored image */public static Bitmap toGrayscale (Bitmap bmp) {if (bmp! = Null) {int width, height; Paint paint = new Paint (); height = bmp. getHeight (); width = bmp. getWidth (); Bitmap bm = Bitmap. createBitmap (width, height, Bitmap. config. RGB_565); Canvas c = new Canvas (bm); ColorMatrix cm = new ColorMatrix (); cm. setSaturation (0); ColorMatrixColorFilter f = new ColorMatrixColorFilter (cm); paint. setColorFilter (f); c. drawBitmap (bmp, 0, 0, paint); return bm;} else {return bmp ;}}

The setSaturation method in the ColorMatrix class is used to remove colors. If you want to view the official website API, this method has the following description:


  • Set the matrix to affect the saturation of colors. A value of 0 maps the color to gray-scale. 1 is identity.
It means to set the saturation of the color matrix. 0 is gray and 1 is the source image. Can this method only set 0 or 1? Actually not. Let's take a look.

How to Implement setSaturation source code:

 public void setSaturation(float sat) {        reset();        float[] m = mArray;                final float invSat = 1 - sat;        final float R = 0.213f * invSat;        final float G = 0.715f * invSat;        final float B = 0.072f * invSat;        m[0] = R + sat; m[1] = G;       m[2] = B;        m[5] = R;       m[6] = G + sat; m[7] = B;        m[10] = R;      m[11] = G;      m[12] = B + sat;    }

For how to set the image color, see 《

Learning about how to use ColorMatrix to process various special effects of images for android (32)

Next, let's look at the code above. The first step is to call the reset method. Let's look at the reset method:


/**     * Set this colormatrix to identity:     * [ 1 0 0 0 0   - red vector     *   0 1 0 0 0   - green vector     *   0 0 1 0 0   - blue vector     *   0 0 0 1 0 ] - alpha vector     */    public void reset() {        final float[] a = mArray;                for (int i = 19; i > 0; --i) {            a[i] = 0;        }        a[0] = a[6] = a[12] = a[18] = 1;    }

In fact, it is to initialize the values in the color matrix.

Next, we will find that removing the color of the image is to change the value in the color matrix.


Set the value in setSaturation to 3:


public static Bitmap toGrayscale(Bitmap bmp) {if (bmp != null) {int width, height;Paint paint = new Paint();height = bmp.getHeight();width = bmp.getWidth();Bitmap bm = Bitmap.createBitmap(width, height,Bitmap.Config.RGB_565);Canvas c = new Canvas(bm);ColorMatrix cm = new ColorMatrix();cm.setSaturation(3);ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);paint.setColorFilter(f);c.drawBitmap(bmp, 0, 0, paint);return bm;}else{return bmp;}}

The effect is as follows:




By changing the color matrix value, you can achieve some nice effects.


Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/45244049 sentiment control _

Related Article

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.