"Android Advanced" (3) Android image processing

Source: Internet
Author: User

1. Concept

Hue/hue: The color of an object's passing

Saturation: The purity of the color, from 0 (gray) to 100% (saturated) to describe the description

Brightness/lightness: The relative degree of light and shade of color


2. Adjust Image Small Demo

Create a tool class. Three parameters used to set the image:

/** * * @param BM * image * @param hue * hue * @param saturation * saturation * @param lum *  Brightness * @return */public static Bitmap Handleimageeffect (Bitmap BM, float hue, float saturation,float Lum) {Bitmap bmp = Bitmap.createbitmap (Bm.getwidth (), Bm.getheight (), Bitmap.Config.ARGB_8888); Canvas canvas = new canvas (BMP);//Set anti-aliasing paint paint = new paint (paint.anti_alias_flag);//adjust hue colormatrix Huematrix = new ColorMatrix (); huematrix.setrotate (0, Hue), huematrix.setrotate (1, hue); Huematrix.setrotate (2, hue);// Saturation ColorMatrix Satmatrix = new ColorMatrix () satmatrix.setsaturation (saturation);//Set brightness colormatrix LumMatrix = new ColorMatrix (); Lummatrix.setscale (Lum, Lum, Lum, 1);//Three settings fused ColorMatrix Imagematrix = new ColorMatrix (); Imagematrix.postconcat (Huematrix); Imagematrix.postconcat (Satmatrix); Imagematrix.postconcat (LumMatrix); Paint.setcolorfilter (New Colormatrixcolorfilter (Imagematrix)); Canvas.drawbitmap (BM, 0, 0, paint); return bmp;}
Then we create a imageview and three Seekbar in the layout file to adjust the parameters and implement the Onseekbarchangelistener interface in the activity:

Private ImageView mimageview;private SeekBar mseekbarhue;private SeekBar mseekbarsat;private SeekBar mseekbarlum; private static int max_value = 255; Seekbar scale maximum value private static int mid_value = 127; Seekbar Middle value private float Mhue; Hue private float mSat; Saturation of private float mlum; Brightness private Bitmap mbitmap; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_primarycolor); Initview ();} private void Initview () {Mimageview = (ImageView) Findviewbyid (r.id.imageview); Mseekbarhue = (SeekBar) Findviewbyid ( R.id.seekbar_hue); Mseekbarsat = (Seekbar) Findviewbyid (R.ID.SEEKBAR_SAT); mseekbarlum = (Seekbar) Findviewbyid ( R.id.seekbar_lum);//Set the Seekbar maximum value of the scale Mseekbarhue.setmax (max_value); Mseekbarsat.setmax (Max_value); Mseekbarlum.setmax (max_value);//Set Seekbar initial value in the middle mseekbarhue.setprogress (Mid_value); Mseekbarsat.setprogress (MID_ VALUE); mseekbarlum.setprogress (Mid_value); Mseekbarhue.setonseekbarchangelistener (this); Mseekbarsat.Setonseekbarchangelistener (This), Mseekbarlum.setonseekbarchangelistener (this); Mbitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.test3); Mimageview.setimagebitmap (MBitmap);} @Overridepublic void onprogresschanged (SeekBar SeekBar, int progress,boolean fromuser) {switch (Seekbar.getid ()) {case R . Id.seekbar_hue:mHue = (progress-mid_value) * 1.0f/mid_value * 180;break;case R.id.seekbar_sat:msat = progress * 1.0F /Mid_value;break;case R.id.seekbar_lum:mlum = progress * 1.0f/mid_value;break;} Bitmap bmp = Imagehelper.handleimageeffect (Mbitmap, Mhue, MSat, Mlum); Mimageview.setimagebitmap (BMP);} @Overridepublic void Onstarttrackingtouch (SeekBar SeekBar) {} @Overridepublic void Onstoptrackingtouch (SeekBar SeekBar ) {}


3. Color Matrix Transformation

Principle:


Create a new layout:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "vertical" > <imageview android:id= "@+id/imageview" android:layout_width= "300DP" Android oid:layout_height= "0DP" android:layout_gravity= "Center_horizontal" android:layout_margin= "20DP" Andro        id:layout_weight= "2"/> <gridlayout android:id= "@+id/group" android:layout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "3" android:columncount= "5" android:rowcount= "4 "/> <button android:layout_width=" match_parent "android:layout_height=" Wrap_content "Androi d:onclick= "Btnchange" android:text= "Change"/> <button android:layout_width= "Match_parent" a Ndroid:layout_height= "Wrap_content" Androidoid:onclick= "Btnreset" android:text= "Reset"/></linearlayout> 
Write acitvity:

public class Colormatrixactivity extends Activity {private ImageView mimageview;private GridLayout mgroup;private Bitmap mbitmap;private int medittextwidth, medittextheight;private edittext[] mEts = new EDITTEXT[20];p rivate float[] Mcolormatrix = new float[20]; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_colormatrix); Initview ();} private void Initview () {mbitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.test1); Mimageview = ( ImageView) Findviewbyid (r.id.imageview); mgroup = (GridLayout) Findviewbyid (R.id.group); Mimageview.setimagebitmap ( MBITMAP) Mgroup.post (new Runnable () {@Overridepublic void Run () {//Initialize each edittext width and height medittextwidth = mgroup.getwidth ( )/5;medittextheight = Mgroup.getheight ()/4;addedittext (); Initmatrix ();}}); /** * Create each EditText and add to the array to save */private void Addedittext () {for (int i = 0; i <; i++) {EditText EditText = new Editte XT (this); Mets[i] = Edittext;mgroup.addvieW (EditText, Medittextwidth, medittextheight);}} /** * initialization matrix */private void Initmatrix () {for (int i = 0; i <; i++) {if (i% 6 = = 0) {Mets[i].settext (string.valueof ( 1));} else {mets[i].settext (string.valueof (0));}}} /** * Gets the value of each edittext, assigns the array */private void Getmatrix () {for (int i = 0; i <; i++) {Mcolormatrix[i] = float.valueof (mEt S[i].gettext (). toString ());}} /** * Set the new color matrix to the canvas */private void Setimagematrix () {Bitmap Bitmap = Bitmap.createbitmap (Mbitmap.getwidth (), Mbitmap.getheight (), Bitmap.Config.ARGB_8888); ColorMatrix ColorMatrix = new ColorMatrix (); Colormatrix.set (Mcolormatrix); Canvas canvas = new canvas (bitmap); Paint paint = new Paint (paint.anti_alias_flag);p aint.setcolorfilter (New Colormatrixcolorfilter (ColorMatrix)); Canvas.drawbitmap (mbitmap, 0, 0, paint); Mimageview.setimagebitmap (bitmap);} /** * Run Change color matrix * * @param v */public void Btnchange (View v) {Getmatrix (); Setimagematrix ();} public void Btnreset (View v) {Initmatrix (); Getmatrix (); Setimagematrix ();}}


4. Pixel Point Analysis

Negative effect algorithm:

NEWR = 255-oldr;

NEWG = 255-OLDG;

newb = 255-oldb;

Old photo effect:

NEWR = (int) (0.393 * oldr + 0.769 * oldg + 0.189 * oldb);

NEWG = (int) (0.349 * oldr + 0.686 * oldg + 0.168 * oldb);

Newb = (int) (0.272 * oldr + 0534 * oldg + 0.131* oldb);

Emboss Effect:

There are ABC three points. To find the B-spot emboss effect:

B.R = C.R-B.R + 127;

B.G = c.g-b.g + 127;

B.B = c.b-b.b + 127;

Expand the Imagehelper tool class and write a method for converting to a negative effect:

/** * Processing the picture into a negative effect *  * @param BM * @return */public static Bitmap handlerimagenegative (Bitmap BM) {int width = Bm.getwid th (); int height = bm.getheight ();//Use an array of width times high to hold all pixels int[] Oldpx = new int[width * height];int[] newpx = new Int[width * Height];int Color;int R, G, B, A; Save the extracted component in color Bitmap BMP = Bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888); Bm.getpixels (oldpx, 0, width, 0, 0 , width, height), for (int i = 0; I < width * height; i++) {color = oldpx[i];//out each component R = color.red (color); g = Color.gre  En (color), B = color.blue (color), a = Color.alpha (color), r = 255-r;g = 255-g;b = 255-b;if (R > 255) {r = 255;} else if (r < 0) {R = 0;} if (g > 255) {g = 255;} else if (G < 0) {g = 0;} if (b > 255) {b = 255;} else if (b < 0) {b = 0;} Newpx[i] = Color.argb (A, R, G, b);} Bmp.setpixels (newpx, 0, width, 0, 0, width, height); return bmp;}
Write the corresponding code in the activity:

Mimageview2.setimagebitmap (imagehelper.handlerimagenegative (bitmap));
Photo-nostalgic effect processing:

/** * Image processing as a nostalgic effect *  * @param BM * @return */public static Bitmap Handlerimageoldphoto (Bitmap BM) {int width = Bm.getwid th (); int height = bm.getheight ();//Use an array of width times high to hold all pixels int[] Oldpx = new int[width * height];int[] newpx = new Int[width * Height];int Color;int R, G, B, A; Save the extracted component in color Bitmap BMP = Bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888); Bm.getpixels (oldpx, 0, width, 0, 0 , width, height), for (int i = 0; I < width * height; i++) {color = oldpx[i];//out each component R = color.red (color); g = Color.gre En (color), B = color.blue (color), a = Color.alpha (color), r = (int) (0.393 * r + 0.769 * g + 0.189 * b); g = (int) (0.349 * r + 0.686 * g + 0.168 * b); b = (int) (0.272 * r + 0534 * g + 0.131 * b); if (R > 255) {r = 255;} if (g > 255) {g = 255;} if (b > 255) {b = 255;} Newpx[i] = Color.argb (A, R, G, b);} Bmp.setpixels (newpx, 0, width, 0, 0, width, height); return bmp;}
handling of photo relief effects:

/** * Image processing as embossed effect * * @param BM * @return */public static Bitmap Handlerimagerilievo (Bitmap BM) {int width = bm.getwidth () ; int height = bm.getheight ();//Use an array of width times high to hold all pixels int[] Oldpx = new int[width * height];int[] newpx = new Int[width * Heig Ht];int Color;int R, G, B, A; Save the color out of the component int R1, G1, B1; Save the component int colorbefore in the color; The pixel value before saving Bitmap bmp = Bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888); Bm.getpixels (oldpx, 0, width, 0, 0, W Idth, height); for (int i = 1; i < width * height; i++) {Colorbefore = Oldpx[i-1];r = Color.Red (colorbefore); g = Color . Green (colorbefore); b = Color.Blue (Colorbefore); a = Color.alpha (Colorbefore); Color = oldpx[i];//Remove each component R1 = color.red ( color); g1 = Color.green (color); b1 = Color.Blue (color); r = r-r1 + 127;g = g-g1 + 127;b = b-b1 + 127;if (R > 255) { R = 255;} if (g > 255) {g = 255;} if (b > 255) {b = 255;} Newpx[i] = Color.argb (A, R, G, b);} Bmp.setpixels (newpx, 0, width, 0, 0, width, height); return bmp;}

Source code Download



"Android Advanced" (3) Android image processing

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.