In Android, the color of the image processing, such as black and white photos, yellowing old photos, high contrast, low saturation and other effects can be achieved by using the color matrix (ColorMatrix).
1. Color matrix (ColorMatrix) Introduction
The color matrix M is a 5*4 matrix, shown in 1. In Android, the color matrix m is stored in the form of 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].
Figure 1 Color matrix M
In one picture, the Rgba (red, green, blue, transparency) value of an image determines the color effect that the picture renders. The RGBA value of the image is stored in a 5*1 color component matrix C, which can control the color effect of the image by the color component matrix C. The color component matrix is shown in C2.
Figure 2 Color Component Matrix C
To change the color effect of a picture, you only need to change the color component matrix of the image. The color matrix makes it easy to modify the color component matrix of the image. Assuming that the modified Image Color Component matrix is C1, there is a 3 color component matrix Calculation formula.
Fig. 3 Calculation formula of color component matrix
Thus, through the color matrix modified the original image of the RGBA value, so as to achieve the purpose of changing the color effect of the picture. And, by the operation shown in 3, the first row of the color matrix m parameter ABCDE determines the red component of the image, the second row of parameters Fghij determines the green component of the image, the third row of parameters Klmno determines the blue component of the image, the fourth row of parameters Pqrst determines the transparency of the image, The fifth Column parameter ejot is the offset of the color.
In general, change the color component can be achieved by modifying the color offset of the 5th column, 4 of the color matrix M1, through the calculation can be learned that the role of the color matrix is to make the image of the red and green components are increased by 100, the effect is the picture yellowing (because the red and green mixed to get yellow).
Figure 4 Color Matrix M1
In addition, it is possible to change the color component by multiplying the color value by a factor directly. The color matrix shown in 5 M2, which amplifies the green component by twice times, is the result of a pan-green image.
Figure 5 Color Matrix M2
2. Image Color Processing Example
Having learned how the color matrix works, we can use the color matrix to manipulate the image.
Layout code:
<LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android" 2 Xmlns:tools="Http://schemas.android.com/tools" 3 Android:layout_width="Match_parent" 4 Android:layout_height="Match_parent" 5 android:orientation="Vertical" 6 Android:paddingbottom="@dimen/activity_vertical_margin" 7 Android:paddingleft="@dimen/activity_horizontal_margin" 8 Android:paddingright="@dimen/activity_horizontal_margin" 9 Android:paddingtop="@dimen/activity_vertical_margin"Ten Tools:context=". Mainactivity ">11 12<TextView13android:layout_width="Wrap_content"android: Layout_height= "wrap_content"android:text="R" /> 16 17<SeekBar18android:id="@+id/sb_red"android:layout_width= "Match_parent" - android:layout_height="Wrap_content"android:max="255" A android:progress="/>" 23 24<TextView25android:layout_width="Wrap_content"toandroid:layout_ Height="Wrap_content"android:text="G" /> 28 29<SeekBar30android:id="@+id/sb_green"toandroid:layout_width ="Match_parent"android:layout_height="Wrap_content" Android:max= "255"android:progress= " /> "35 36<TextView37android:layout_width="Wrap_content"android:layout_ Height="Wrap_content"android:text="B" /> 40 41<SeekBar42android:id="@+id/sb_blue"android:layout_width= "Match_parent" - android:layout_height= "wrap_content"android:max="255" $ android:progress="/>" 47 48<TextView49android:layout_width="Wrap_content"android:layout_ Height="Wrap_content"wuyiandroid:text="A" /> 52 53<SeekBar54android:id="@+id/sb_alpha"android:layout_width ="Match_parent"android:layout_height="Wrap_content" Android:max= "255"android:progress=" /> "59 60<ImageView61android:id="@+id/iv_show"android:layout_width ="Wrap_content"android:layout_height="Wrap_content" android:src="@drawable/painter" /> 65 66</linearlayout>Copy Code
Implementation code:
1 PackageCn.bgxt.colormatrixdemo;2 3 ImportAndroid.os.Bundle;4 ImportAndroid.util.Log;5 ImportAndroid.widget.ImageView;6 ImportAndroid.widget.SeekBar;7 ImportAndroid.widget.SeekBar.OnSeekBarChangeListener;8 Importandroid.app.Activity;9 ImportAndroid.graphics.Bitmap;Ten ImportAndroid.graphics.BitmapFactory; One ImportAndroid.graphics.Canvas; A ImportAndroid.graphics.ColorMatrix; - ImportAndroid.graphics.ColorMatrixColorFilter; - ImportAndroid.graphics.Matrix; the ImportAndroid.graphics.Paint; - - Public class mainactivity extends Activity { - PrivateSeekBar sb_red, Sb_green, Sb_blue,sb_alpha; + PrivateImageView iv_show; - PrivateBitmap Afterbitmap; + PrivatePaint paint; A PrivateCanvas canvas; at PrivateBitmap Basebitmap; - - @Override - protected void onCreate(Bundle savedinstancestate) { - Super. OnCreate (Savedinstancestate); -Setcontentview (R.layout.activity_main); in -Iv_show = (ImageView) Findviewbyid (r.id.iv_show); tosb_red = (SeekBar) Findviewbyid (r.id.sb_red); +Sb_green = (SeekBar) Findviewbyid (R.id.sb_green); -Sb_blue = (SeekBar) Findviewbyid (R.id.sb_blue); theSb_alpha = (SeekBar) Findviewbyid (R.id.sb_alpha); * $Sb_red.setonseekbarchangelistener (Seekbarchange);Panax NotoginsengSb_green.setonseekbarchangelistener (Seekbarchange); -Sb_blue.setonseekbarchangelistener (Seekbarchange); theSb_alpha.setonseekbarchangelistener (Seekbarchange); + A //Get the picture from the resource file theBasebitmap = Bitmapfactory.decoderesource (Getresources (), +R.drawable.painter); - //Get an editable empty picture that is consistent with basebitmap size $Afterbitmap = Bitmap.createbitmap (Basebitmap.getwidth (), $Basebitmap.getheight (), Basebitmap.getconfig ()); -Canvas =NewCanvas (AFTERBITMAP); -Paint =NewPaint (); the} - Wuyi PrivateSeekbar.onseekbarchangelistener Seekbarchange =NewOnseekbarchangelistener () { the - @Override Wu Public void Onstoptrackingtouch(SeekBar SeekBar) { - //Get the current value of each Seekbar About floatPROGRESSR = sb_red.getprogress ()/ -F $ floatPROGRESSG = sb_green.getprogress ()/ -F - floatPROGRESSB = sb_blue.getprogress ()/ -F - floatProgressa=sb_alpha.getprogress ()/ -F -LOG.I ("Main","r:g:b="+progressr+":"+progressg+":"+PROGRESSB); A //The matrix of Rgba defined according to Seekbar + float[] src =New float[]{ thePROGRESSR,0,0,0,0, - 0, PROGRESSG,0,0,0, $ 0,0, PROGRESSB,0,0, the 0,0,0, Progressa,0}; the //define ColorMatrix and specify the RGBA matrix theColorMatrix ColorMatrix =NewColorMatrix (); theColormatrix.set (SRC); - //Set the color of paint inPaint.setcolorfilter (NewColormatrixcolorfilter (SRC)); the //By specifying the RGBA matrix of the paint to the original picture to the blank picture theCanvas.drawbitmap (Basebitmap,NewMatrix (), paint); AboutIv_show.setimagebitmap (AFTERBITMAP); the} the the @Override + Public void Onstarttrackingtouch(SeekBar SeekBar) { -} the Bayi @Override the Public void onprogresschanged(SeekBar SeekBar,intprogress,83BooleanFromuser) { -} -}; the}
:
Android turns color images into grayscale (black and white)
Code:
Public class mainactivity extends Activity { @Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); ImageView IV =NewImageView ( This); Setcontentview (iv); Bitmap originimg = Bitmapfactory.decoderesource (Getresources (), r.drawable.painter); Bitmap grayimg = Bitmap.createbitmap (Originimg.getwidth (), Originimg.getheight (), Bitmap.Config.ARGB_8888); Canvas Canvas =NewCanvas (GRAYIMG); Paint paint =NewPaint (); ColorMatrix ColorMatrix =NewColorMatrix (); Colormatrix.setsaturation (0); Colormatrixcolorfilter Colormatrixfilter =NewColormatrixcolorfilter (ColorMatrix); Paint.setcolorfilter (Colormatrixfilter); Canvas.drawbitmap (Originimg,0,0, paint); Iv.setimagebitmap (GRAYIMG); }}
Mainly colormatrix.setsaturation (0); the function of this line of code.
, or just after the color map processing results:
Black and white photos, yellowing old photos, high contrast, low saturation and other effects
Pan Red Effect
High contrast, high saturation, hue transform and other image processing effects
Android ColorMatrix class Image Color processing-black and white photos, yellowing old photos, high contrast and other effects