Android study note 22: Image Color Processing (colormatrix)

Source: Internet
Author: User

In Android, you can use a color matrix to process the color of an image, such as a black or white image, a yellow image, a high contrast, or a low saturation.

1. Introduction to color matrix

Color matrix M is a 5*4 matrix, as shown in 1. In Android, the color matrix M is a one-dimensional array M = [A, B, C, D, E, F, G, H, I, J, K, L, M, n, O, P, Q, R, S, T] storage.

Figure 1 color matrix m

In an image, the rgba (red, green, blue, and transparency) value determines the color of the image. The rgba value of the image is stored in a 5*1 color component matrix C, and the color effect of the image can be controlled by the color component matrix C. The color component matrix C2 is shown.

Figure 2 color component matrix C

To change the color effect of an image, you only need to change the color matrix of the image. The color matrix allows you to easily modify the color matrix of an image. Assume that the color matrix of the modified image is C1, the formula for calculating the color matrix is 3.

Figure 3 calculation formula of color component matrix

It can be seen that the rgba value of the original image is modified through the color matrix to achieve the goal of changing the color effect of the image. In addition, according to the operation shown in 3, the first line parameter ABCDE of the color matrix m determines the red component of the image, and the second line parameter fghij determines the green component of the image, the parameter klmno in the third row determines the blue component of the image. The parameter pqrst in the fourth row determines the transparency of the image. The parameter ejot in the fifth column is the offset of the color.

Generally, you can modify the color offset of the 5th column when changing the color weight, as shown in figure 4, the color matrix M1, after calculation, we can know that the color matrix is used to increase the red and green components of the image by 100. This effect is that the image is yellow (because the red and green colors are mixed to produce yellow ).

Figure 4 color matrix M1

In addition, you can change the color component by multiplying the color value by a certain coefficient. As shown in figure 5, the color matrix m2 doubles the green component. The effect is that the image is pan-green.

Figure 5 color matrix m2

 

2. Image color processing example

After learning about the working principle of the color matrix, we can use the color matrix to process the image.

2.1 main interface Layout

In the main interface layout, you must first customize a view control to display the image processing effect. Then, 20 edittext controls are required to enter the color matrix value. A button control is also required to submit image processing requests. The page layout is shown in Figure 6.

Figure 6 main interface Layout

The top image on the main interface is the original image for color processing. In the 20 edittext controls, we set the initial values of the color matrix to {, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }. The custom view control can be defined in the XML layout file by referring to the following method.

 1  <! --  Custom View Control -->  2  <  Com. example. android_colormatrix.myview  3  Android: ID  = "@ + ID/myview"  4   Android: layout_width  = "480dp"  5   Android: layout_height  = "180dp"      >    6  </  Com. example. android_colormatrix.myview  >   

Note that the custom control must contain the complete package name. Pass the aboveCodeWe can see that the custom control is implemented in myview. Next, let's take a look at how the custom view control is implemented in myview.

2.2 Implementation of custom View Controls

To implement a custom View class, you only need to let myview inherit the View class and override the ondraw () method of the View class.

In the ondraw () method, we need to do five things:

(1) create a paint brush object for plotting.

(2) create a colormatrix color matrix object to store the color matrix.

(3) set the color matrix value of colormatrix.

(4) set the color filter of the paint brush.

(5) Use a paint brush to draw and output bitmap images.

The specific implementation code of the above five steps is as follows:

 1  /**  2   * Function: plotting function  3   * Param: canvas object  4   * Author: blog Park-still indifferent  5  */  6  Public   Void  Ondraw (canvas ){  7  8 Paint mpaint = New Paint (); //  Create a paint brush object  9 Canvas. drawbitmap (mbitmap, 0, 0, mpaint ); //  Plotting (original image)  10  11 Colormatrix mcolormatrix =New Colormatrix (); //  Create a color matrix object  12 Mcolormatrix. Set (array ); //  Set the color matrix Value  13 Mpaint. setcolorfilter ( New Colormatrixcolorfilter (mcolormatrix )); //  Set Paint Color Filter  14 Canvas. drawbitmap (mbitmap, 0, 0, mpaint ); // Plotting (processed image)  15 }

Among them, mbitmap is the image object to be processed. Array array is used to store the user input values we obtain from the edittext control. How can I store user input values in the edittext control in an array? You can use the following method.

 1  /**  2   * Function: Obtain the input value from edittext and store it in the array.  3   * Param:  4   * Author: blog Park-still indifferent  5  */ 6  Public   Void  Getvalues (){  7  For ( Int I = 0; I <20; I ++ ){  8 Array [I] = Float. valueof (medittext [I]. gettext (). tostring ());  9   }  10 }

2.3 instance Effects

RunProgramThen, enter the color matrix value in the interface shown in 6 and click the "transform" button to see different image processing effects.

If we increase the red weight of the color matrix by 2 times, we can get the reddish image 7.

Figure 7 effect of a red image

In addition, we can also use the color matrix to easily achieve high contrast, high saturation, color phase conversion and other image processing effects, as shown in 8.

Figure 8 image processing result

 

 

Related information:

Android Image Processing (matrix, colormatrix)

Http://www.cnblogs.com/leon19870907/articles/1978065.html

 

 

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.