Colormatrix for Android Image Processing

Source: Internet
Author: User

First of all, I declare that this article is for your reference only and does not guarantee that all texts are properly described. You are welcome to point out the shortcomings and errors. Thank you for your attention and reading. If you need code, leave a message and contact me.


0 front nonsense

A few days ago, when I was doing image recognition, I used colormatrix and found an article which was well written. It was actually implemented by his method. The original text didn't provide XML, so it took some time, And I improved some. The previous descriptions are the original text descriptions, which are well written and the code behind them is my own.

Android Image Processing (matrix, colormatrix) http://www.cnblogs.com/leon19870907/articles/1978065.html

I. Principles of colormatrix

In programming, you sometimes need to perform special processing on images, such as black and white images or the effects of old photos. Sometimes you need to transform the images to stretch, distort, and so on. These effects are well supported in Android, and the effects described above can be perfectly achieved through the color matrix and coordinate transformation matrix. The following describes the usage and related functions of these two matrices.

Color Matrix
In Android, the color matrix (colormatrix class) can be used to operate colors. The color matrix is a matrix of 5x4.

It can be used to modify the values of each rgba component in the image. The color matrix is stored as a one-dimensional array as follows:
[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, t]
He uses the four rgba channels to directly operate the corresponding color. If Photoshop is used, he will know that sometimes the processing image is made special by controlling the different color channels of rgba.

The calculation rule of the matrix is to multiply one row of matrix A by one column of matrix C as a row of matrix R,

The C matrix is the argb information contained in the image. The R matrix is a new color component after applying the color matrix to C. The calculation result is as follows:
 
R' = A * r + B * G + C * B + D * A + E;
G' = f * r + G * g + H * B + I * A + J;
B '= K * r + L * g + M * B + N * A + O;
A' = p * r + Q * g + R * B + S * A + T;
 
The color matrix does not look so esoteric. In fact, there are few parameters to use, and the first line of the regular determines the red and the second line determines the green.

The third row determines the blue color, the fourth row determines the transparency, and the Fifth Column determines the color offset. The following is a color matrix used in practice.


If this matrix is applied to each color component, r = A * C. After calculation, we will find that, the color components do not actually change (R' = r G' = g B '= B a' = ).

As shown in Figure 1.5, after matrix calculation, the red component increases by 100 and the green component increases by 100,

The effect is that the image is yellow, because the red and green colors are yellow, and the yellow color increases by 100. Of course, the image is yellow.

To change the color components, you can not only modify the color offset of the 5th column, but also multiply the corresponding color value by a multiple as shown in the preceding matrix to directly zoom in.

1.6 is to multiply the green component by 2 to the original 2 times. I believe that the reader has understood how to change the color components through the color matrix.

Below is a piece of code to adjust the color matrix to obtain different color effects.

Code 2 Implementation

First, write a myimage. Java file, which mainly writes a custom view. Service for the following view

Package COM. example. android. APIS. view. colormatrix; import COM. example. android. APIS. r; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. colormatrix; import android. graphics. colormatrixcolorfilter; import android. graphics. paint; import android. util. attributeset; import android. util. log; import android. view. view; public class myimage extends view {private paint mpaint = new paint (paint. anti_alias_flag); Private bitmap mbitmap; private float [] array = new float [20]; Public myimage (context, attributeset attrs) {super (context, attrs ); // mbitmap = bitmapfactory. decoderesource (context. getresources (), R. drawable. ic_launcher); string dir = android. OS. environment. getexternalstoragedirectory () + "/1317835083240.jpg"; mbitmap = bitmapfactory. decodefile (DIR); log. I ("1", android. OS. environment. getexternalstoragedirectory () + ""); // mbitmap = bitmapfactory. decoderesource (context. getresources (), R. drawable. icon); invalidate ();} public void setvalues (float [] A) {for (INT I = 0; I <20; I ++) {array [I] = A [I] ;}@ override protected void ondraw (canvas) {paint = mpaint; paint. setcolorfilter (null); canvas. drawbitmap (mbitmap, 0, 0, paint); colormatrix CM = new colormatrix (); // sets the color matrix cm. set (array); // color filter. Apply the color matrix to the image paint. setcolorfilter (New colormatrixcolorfilter (CM); // drawing canvas. drawbitmap (mbitmap, 0, 0, paint); log. I ("cmatrix", "---------> ondraw ");}}

Cmatrix. Java

Package COM. example. android. APIS. view. colormatrix; import COM. example. android. APIS. r; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. view; import android. widget. button; import android. widget. edittext; public class cmatrix extends activity {private button change, Change1, Change2, Change3; private edittext [] ET = new edittext [20]; private float [] carray = new float [20]; private myimage Sv; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. imgmain); change = (button) findviewbyid (R. id. set); Change1 = (button) findviewbyid (R. id. set1); Change2 = (button) findviewbyid (R. id. set2); Change3 = (button) findviewbyid (R. id. set3); Sv = (myimage) findviewbyid (R. id. myimage); For (INT I = 0; I <20; I ++) {et [I] = (edittext) findviewbyid (R. id. indexa + I); carray [I] = float. valueof (et [I]. gettext (). tostring (); log. I ("et [" + I + "]", et [I]. gettext (). tostring ();} change. setonclicklistener (l); change1.setonclicklistener (L1); change2.setonclicklistener (L2); change3.setonclicklistener (L3);} private button. onclicklistener L = new button. onclicklistener () {@ override public void onclick (view arg0) {// todo auto-generated method stub getvalues (); Sv. setvalues (carray); Sv. invalidate () ;}}; private button. onclicklistener L1 = new button. onclicklistener () {@ override public void onclick (view arg0) {// todo auto-generated method stub getvalue1 (); setvalue (); Sv. setvalues (carray); Sv. invalidate () ;}}; private button. onclicklistener L2 = new button. onclicklistener () {@ override public void onclick (view arg0) {// todo auto-generated method stub log. I ("free", "click"); getvalue2 (); setvalue (); Sv. setvalues (carray); Sv. invalidate () ;}}; private button. onclicklistener l3 = new button. onclicklistener () {@ override public void onclick (view arg0) {// todo auto-generated method stub getvalue3 (); setvalue (); Sv. setvalues (carray); Sv. invalidate () ;}}; public void getvalues () {for (INT I = 0; I <20; I ++) {carray [I] = float. valueof (et [I]. gettext (). tostring (); log. I ("1", carray [I] + "") ;}} public void getvalue1 () {// high saturation carray [0] = 5; carray [1] = 0; carray [2] = 0; carray [3] = 0; carray [4] =-254; carray [5] = 0; carray [6] = 5; carray [7] = 0; carray [8] = 0; carray [9] =-254; carray [10] = 0; carray [11] = 0; carray [12] = 5; carray [13] = 0; carray [14] =-254; carray [15] = 0; carray [16] = 0; carray [17] = 0; carray [18] = 5; carray [19] =-254;} public void getvalue2 () {// color phase change carray [0] = (float)-0.36; carray [1] = (float) 1.691; carray [2] = (float)-0.32; carray [3] = 0; carray [4] = 0; carray [5] = (float) 0.325; carray [6] = (float) 0.398; carray [7] = (float) 0.275; carray [8] = 0; carray [9] = 0; carray [10] = (float) 0.79; carray [11] = (float) 0.79; carray [12] = (float)-0.76; carray [13] = 0; carray [14] = 0; carray [15] = 0; carray [16] = 0; carray [17] = 0; carray [18] = 1; carray [19] = 0 ;} public void getvalue3 () {// black/white carray [0] = (float) 0.308; carray [1] = (float) 0.609; carray [2] = (float) 0.082; carray [3] = 0; carray [4] = 0; carray [5] = (float) 0.308; carray [6] = (float) 0.609; carray [7] = (float) 0.082; carray [8] = 0; carray [9] = 0; carray [10] = (float) 0.308; carray [11] = (float) 0.609; carray [12] = (float) 0.082; carray [13] = 0; carray [14] = 0; carray [15] = 0; carray [16] = 0; carray [17] = 0; carray [18] = 1; carray [19] = 0 ;} public void setvalue () {for (INT I = 0; I <20; I ++) {et [I] = (edittext) findviewbyid (R. id. indexa + I); et [I]. settext (carray [I] + ""); carray [I] = float. valueof (et [I]. gettext (). tostring (); log. I ("et [" + I + "]", et [I]. gettext (). tostring ());}}}

XML file

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <linearlayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> <textview Android: textsize = "15.0sp" Android: textstyle = "bold" Android: textcolor = "# ffffffff" Android: padding = "2.0sp" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_marginleft = "2.0sp" Android: TEXT = "red (r)"/> <edittext Android: text = "0" Android: textsize = "10dip" Android: Id = "@ + ID/Indexa" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"> <requestfocus/> </edittext> <edittext Android: text = "0" Android: textsize = "10dip" Android: Id = "@ + ID/indexa1" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"/> <edittext Android: text = "0" Android: textsize = "10dip" Android: Id = "@ + ID/indexa2" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"/> <edittext Android: text = "0" Android: textsize = "10dip" Android: id = "@ + ID/indexa3" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"/> <edittext Android: TEXT = "0" Android: textsize = "10dip" Android: Id = "@ + ID/indexa4" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"/> </linearlayout> <linearlayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> <textview Android: textsize = "15.0sp" Android: textstyle = "bold" Android: textcolor = "# ffffff" Android: padding = "2.0sp" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_marginleft = "2.0sp" Android: text = "Green (g)"/> <edittext Android: text = "0" Android: textsize = "10dip" Android: Id = "@ + ID/indexa5" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"> </edittext> <edittext Android: text = "0" Android: textsize = "10dip" Android: Id = "@ + ID/indexa6" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"/> <edittext Android: text = "0" Android: textsize = "10dip" Android: id = "@ + ID/indexa7" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"/> <edittext Android: TEXT = "0" Android: textsize = "10dip" Android: Id = "@ + ID/indexa8" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"/> <edittext Android: text = "0" Android: textsize = "10dip" Android: Id = "@ + ID/indexa9" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"/> </linearlayout> <linearlayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> <textview Android: textsize = "15.0sp" Android: textstyle = "bold" Android: textcolor = "# ffffffff" Android: padding = "2.0sp" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_marginleft = "2.0sp" Android: text = "blue (B) "/> <edittext Android: text =" 0 "Android: textsize =" 10dip "Android: Id =" @ + ID/indexa10 "Android: layout_width =" 60dp "Android: layout_height = "40dp" Android: layout_weight = "1"> </edittext> <edittext Android: text = "0" Android: textsize = "10dip" Android: id = "@ + ID/indexa11" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"/> <edittext Android: TEXT = "0" Android: textsize = "10dip" Android: Id = "@ + ID/indexa12" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"/> <edittext Android: text = "0" Android: textsize = "10dip" Android: Id = "@ + ID/indexa13" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"/> <edittext Android: text = "0" Android: textsize = "10dip" Android: id = "@ + ID/indexa14" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"/> </linearlayout> <linearlayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> <textview Android: textsize = "15.0sp" Android: textstyle = "bold" Android: textcolor = "# ffffffff" Android: padding = "2.0sp" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_marginleft = "2.0sp" Android: TEXT = "transparent (a)"/> <edittext Android: text = "0" Android: textsize = "10dip" Android: Id = "@ + ID/indexa15" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"> </edittext> <edittext Android: text = "0" Android: textsize = "10dip" Android: Id = "@ + ID/indexa16" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"/> <edittext Android: text = "0" Android: textsize = "10dip" Android: Id = "@ + ID/indexa17" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"/> <edittext Android: text = "0" Android: textsize = "10dip" Android: id = "@ + ID/indexa18" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"/> <edittext Android: TEXT = "0" Android: textsize = "10dip" Android: Id = "@ + ID/indexa19" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: layout_weight = "1"/> </linearlayout> <linearlayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> <button Android: id = "@ + ID/set" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: text = "free"/> <button Android: id = "@ + ID/set1" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: text = ""/> <button Android: id = "@ + ID/set2" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: text = ""/> <button Android: id = "@ + ID/set3" Android: layout_width = "60dp" Android: layout_height = "40dp" Android: text = "Black and White"/> </linearlayout> <COM. example. android. APIS. view. colormatrix. myimage Android: Id = "@ + ID/myimage" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> </linearlayout>

3. effect demonstration

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.