Android image processing Series 3-image color saturation, color phase, and brightness Processing

Source: Internet
Author: User

Source image:

After processing:

Below is the code:

I. Image Processing Layer:

Package COM. jacp. tone. view; </P> <p> Import Java. util. arraylist; </P> <p> Import android. content. context; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. canvas; <br/> Import android. graphics. colormatrix; <br/> Import android. graphics. colormatrixcolorfilter; <br/> Import android. graphics. paint; <br/> Import android. view. gravity; <br/> Import android. view. view; <br/> Import android. widget. linearlayout; <br/> Import android. widget. seekbar; <br/> Import android. widget. seekbar. onseekbarchangelistener; <br/> Import android. widget. textview; </P> <p> Import COM. jacp. tone. r; </P> <p>/** <br/> * image color processing <br/> * @ author maylian7700@126.com <br/> */<br/> public class tonelayer {</P> <p>/** <br/> * saturation id <br/> */<br/> Public static final int flag_saturation = 0 x 0; </P> <p>/** <br/> * brightness id <br/> */<br/> Public static final int flag_lum = 0x1; </P> <p>/** <br/> * Color Mark <br/> */<br/> Public static final int flag_hue = 0x2; </P> <p>/** <br/> * saturation <br/> */<br/> private textview msaturation; <br/> private seekbar msaturationbar; </P> <p>/** <br/> * color <br/> */<br/> private textview mhue; <br/> private seekbar mhuebar; </P> <p>/** <br/> * brightness <br/> */<br/> private textview mlum; <br/> private seekbar mlumbar; </P> <p> private float mdensity; <br/> Private Static final int text_width = 50; </P> <p> private linearlayout mparent; </P> <p> private colormatrix mlightnessmatrix; <br/> private colormatrix msaturationmatrix; <br/> private colormatrix mhuematrix; <br/> private colormatrix mallmatrix; </P> <p>/** <br/> * brightness <br/> */<br/> private float mlumvalue = 1f; </P> <p>/** <br/> * saturation <br/> */<br/> private float msaturationvalue = 0f; </P> <p>/** <br/> * color <br/> */<br/> private float mhuevalue = 0f; </P> <p>/** <br/> * median of seekbar <br/> */<br/> Private Static final int middle_value = 127; </P> <p>/** <br/> * maximum seekbar value <br/> */<br/> Private Static final int max_value = 255; </P> <p> private arraylist <seekbar> mseekbars = new arraylist <seekbar> (); </P> <p> Public tonelayer (context) {<br/> Init (context); <br/>}</P> <p> private void Init (context) {<br/> mdensity = context. getresources (). getdisplaymetrics (). density; </P> <p> msaturation = new textview (context); <br/> msaturation. settext (R. string. saturation); <br/> mhue = new textview (context); <br/> mhue. settext (R. string. contrast); <br/> mlum = new textview (context); <br/> mlum. settext (R. string. lightness); </P> <p> msaturationbar = new seekbar (context); <br/> mhuebar = new seekbar (context ); <br/> mlumbar = new seekbar (context); </P> <p> mseekbars. add (msaturationbar); <br/> mseekbars. add (mhuebar); <br/> mseekbars. add (mlumbar); </P> <p> for (INT I = 0, size = mseekbars. size (); I <size; I ++) {<br/> seekbar = mseekbars. get (I); <br/> seekbar. setmax (max_value); <br/> seekbar. setprogress (middle_value); <br/> seekbar. settag (I); <br/>}</P> <p> linearlayout saturation = new linearlayout (context); <br/> saturation. setorientation (linearlayout. horizontal); <br/> saturation. setlayoutparams (New linearlayout. layoutparams (linearlayout. layoutparams. match_parent, linearlayout. layoutparams. wrap_content); </P> <p> linearlayout. layoutparams txtlayoutparams = new linearlayout. layoutparams (INT) (text_width * mdensity), linearlayout. layoutparams. match_parent); <br/> msaturation. setgravity (gravity. center); <br/> saturation. addview (msaturation, txtlayoutparams); </P> <p> linearlayout. layoutparams seeklayoutparams = new linearlayout. layoutparams (linearlayout. layoutparams. match_parent, linearlayout. layoutparams. wrap_content); <br/> saturation. addview (msaturationbar, seeklayoutparams); </P> <p> linearlayout hue = new linearlayout (context); <br/> hue. setorientation (linearlayout. horizontal); <br/> hue. setlayoutparams (New linearlayout. layoutparams (linearlayout. layoutparams. match_parent, linearlayout. layoutparams. wrap_content); </P> <p> mhue. setgravity (gravity. center); <br/> hue. addview (mhue, txtlayoutparams); <br/> hue. addview (mhuebar, seeklayoutparams); </P> <p> linearlayout Lum = new linearlayout (context); <br/> Lum. setorientation (linearlayout. horizontal); <br/> Lum. setlayoutparams (New linearlayout. layoutparams (linearlayout. layoutparams. match_parent, linearlayout. layoutparams. wrap_content); </P> <p> mlum. setgravity (gravity. center); <br/> Lum. addview (mlum, txtlayoutparams); <br/> Lum. addview (mlumbar, seeklayoutparams); </P> <p> mparent = new linearlayout (context); <br/> mparent. setorientation (linearlayout. vertical); <br/> mparent. setlayoutparams (New linearlayout. layoutparams (linearlayout. layoutparams. match_parent, linearlayout. layoutparams. wrap_content); <br/> mparent. addview (saturation); <br/> mparent. addview (Hue); <br/> mparent. addview (Lum); <br/>}</P> <p> Public View getparentview () {<br/> return mparent; <br/>}</P> <p>/** <br/> * set the saturation value <br/> * @ Param saturation <br/> */<br/> Public void setsaturation (INT saturation) {<br/> msaturationvalue = saturation * 1.0f/middle_value; <br/>}</P> <p>/** <br/> * set the color phase value <br/> * @ Param hue <br/> */<br/> Public void sethue (INT Hue) {<br/> mhuevalue = hue * 1.0f/middle_value; <br/>}</P> <p>/** <br/> * set the brightness value <br/> * @ Param Lum <br/> */<br/> Public void setlum (INT Lum) {<br/> mlumvalue = (Lum-middle_value) * 1.0f/middle_value * 180; <br/>}</P> <p> Public arraylist <seekbar> getseekbars () <br/>{< br/> return mseekbars; <br/>}</P> <p>/** <br/> * @ Param flag <br/> * bit 0 indicates whether to change the color., bitwise 1 indicates whether the saturation is changed, and bitwise 2 indicates whether the brightness is changed. <br/> */<br/> Public bitmap handleimage (Bitmap BM, int flag) {<br/> bitmap BMP = bitmap. createbitmap (BM. getwidth (), BM. getheight (), <br/> bitmap. config. argb_8888); <br/> // create a variable bitmap area of the same size to draw a color-adjusted image <br/> canvas = new canvas (BMP ); // obtain the paint brush object <br/> paint = new paint (); // create a paint <br/> paint. setantialias (true); // sets anti-aliasing, that is, edge smoothing <br/> If (null = mallmatrix) {<br/> mallmatrix = new colormatrix (); <br/>}</P> <p> If (null = mlightnessmatrix) {<br/> mlightnessmatrix = new colormatrix (); // color conversion matrix, android bitmap color change processing mainly relies on this object <br/>}</P> <p> If (null = msaturationmatrix) {<br/> msaturationmatrix = new colormatrix (); <br/>}</P> <p> If (null = mhuematrix) {<br/> mhuematrix = new colormatrix (); <br/>}</P> <p> switch (FLAG) {<br/> case flag_hue: // you need to change the color <br/> mhuematrix. reset (); <br/> mhuematrix. setscale (mhuevalue, 1); // the red, green, and Blue values are in the same proportion. The last parameter 1 indicates that the transparency is not changed, for more information about this function, see <br/> /// Android <br/> // doc <br/> break; <br/> case flag_saturation: // you need to change the saturation <br/> // saturation value. The minimum value can be set to 0. At this time, it corresponds to a grayscale image (that is, a "black and white image "), <br/> // if the value is 1, the saturation remains unchanged. If the value is greater than 1, the system is oversaturated. <br/> msaturationmatrix. reset (); <br/> msaturationmatrix. setsaturation (msaturationvalue); <br/> break; <br/> case flag_lum: // brightness <br/> // huecolor indicates the Rotation Angle of the color wheel. A positive value indicates clockwise rotation, negative value indicates clockwise rotation <br/> mlightnessmatrix. reset (); // set to the default value <br/> mlightnessmatrix. setrotate (0, mlumvalue); // controls the angle from which the Red Area rotates on the color wheel. <br/> mlightnessmatrix. setrotate (1, mlumvalue); // controls the angle from which the Green-red area rotates on the color wheel. <br/> mlightnessmatrix. setrotate (2, mlumvalue); // control the angle from which the blue area rotates on the color wheel <br/> // here, the color phase of the entire graph is changed <br/> break; <br/>}< br/> mallmatrix. reset (); <br/> mallmatrix. postconcat (mhuematrix); <br/> mallmatrix. postconcat (msaturationmatrix); // effect superposition <br/> mallmatrix. postconcat (mlightnessmatrix); // effect superposition </P> <p> paint. setcolorfilter (New colormatrixcolorfilter (mallmatrix); // set the color conversion effect <br/> canvas. drawbitmap (BM, 0, 0, paint); // output the color changed image to the newly created bitmap area. <br/> // return the new bitmap, that is, the converted image <br/> return BMP; <br/>}</P> <p >}< br/>

Ii. Main Interface:

Package COM. jacp. tone; </P> <p> Import Java. util. arraylist; </P> <p> Import android. app. activity; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. bitmapfactory; <br/> Import android. OS. bundle; <br/> Import android. widget. imageview; <br/> Import android. widget. linearlayout; <br/> Import android. widget. seekbar; <br/> Import android. widget. seekbar. onseekbarchangelistener; </P> <p> Import COM. jacp. tone. view. tonelayer; </P> <p>/** <br/> * main startup interface <br/> * @ author maylian7700@126.com <br/> */<br/> public class imagetoneactivity extends activity implements onseekbarchangelistener {<br/> private tonelayer mtonelayer; <br/> private imageview mimageview; <br/> private bitmap mbitmap; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> Init (); <br/>}</P> <p> private void Init () <br/>{< br/> mtonelayer = new tonelayer (this); </P> <p> mbitmap = bitmapfactory. decoderesource (getresources (), R. drawable. test); <br/> mimageview = (imageview) findviewbyid (R. id. img_view); <br/> mimageview. setimagebitmap (mbitmap); <br/> (linearlayout) findviewbyid (R. id. tone_view )). addview (mtonelayer. getparentview (); </P> <p> arraylist <seekbar> seekbars = mtonelayer. getseekbars (); <br/> for (INT I = 0, size = seekbars. size (); I <size; I ++) <br/>{< br/> seekbars. get (I ). setonseekbarchangelistener (this); <br/>}</P> <p> @ override <br/> Public void onprogresschanged (seekbar, int progress, <br/> Boolean fromuser) {<br/> int flag = (integer) seekbar. gettag (); <br/> switch (FLAG) <br/>{< br/> case tonelayer. flag_saturation: <br/> mtonelayer. setsaturation (Progress); <br/> break; <br/> case tonelayer. flag_lum: <br/> mtonelayer. setlum (Progress); <br/> break; <br/> case tonelayer. flag_hue: <br/> mtonelayer. sethue (Progress); <br/> break; <br/>}</P> <p> mimageview. setimagebitmap (mtonelayer. handleimage (mbitmap, flag); <br/>}</P> <p> @ override <br/> Public void onstarttrackingtouch (seekbar) {</P> <p >}</P> <p> @ override <br/> Public void onstoptrackingtouch (seekbar) {</P> <p >}< br/>}

3. layout file:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <scrollview xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "match_parent" <br/> Android: layout_height = "match_parent" <br/> </P> <p> <linearlayout <br/> Android: layout_width = "match_parent" <br/> Android: layout_height = "match_parent" <br/> Android: Orientation = "vertical"> </P> <p> <imageview <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_weight = "1" <br/> Android: id = "@ + ID/img_view" <br/> Android: layout_gravity = "center" <br/> <linearlayout <br/> Android: layout_width = "match_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: id = "@ + ID/tone_view" <br/> </linearlayout> <br/> </scrollview>

 

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.