Reprint Please specify Source:http://blog.csdn.net/forwardyzk/article/details/44410833
The following describes Android image processing.
RGBA model: Red Green Blue Alpha
1. Change the color, transparency and brightness of the image.
2. Processing images using a color matrix
3. Change pixel processing image
1. Use ColorMatrix to change hue, transparency and brightness
To get a new image:
(1) Create a new bitmap, using the CreateBitmap () method
(2) Use the ColorMatrix object to change its hue, color value and brightness
/** * Set the rotation on a color axis by the specified values. * <code>axis=0</code> correspond to a rotation around the RED color * <code>axis=1</code> Cor Respond to a rotation around the GREEN color * <code>axis=2</code> correspond to a rotation around the BLU E Color */
setRotate (parameter 1, parameter 2) changes the hue, the first parameter: 0 is the change of the red, 1 for the change of the green, 2 for the change is blue, the second parameter: indicates the depth.
Setsaturation (): Set transparency
Postconcat (): Set brightness
(3) Draw a new bitmap on the canvas with a brush
Set the color picker for the brush to connect the color matrix to the brush.
Setcolorfilter ();
/** * Get changed color value, transparency, brightness of bitmap * * @param bitmap * @param rorate color value * @param saturation transparency * @p Aram Scale Brightness * @return */public static Bitmap Handleimageeffect (Bitmap Bitmap, float rorate, float Satur ation, float scale) {Bitmap Newbitmap = Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight (), BITMAP.CONFIG.A rgb_8888); Color value ColorMatrix Roratematrix = new ColorMatrix (); Roratematrix.setrotate (0, rorate);//red roratematrix.setrotate (1, rorate);//green roratematrix.setrotate (2, rorate);//blue//Transparency ColorMatrix Saturationmatrix = new ColorMatrix (); Saturationmatrix.setsaturation (saturation); Brightness ColorMatrix Scalematrix = new ColorMatrix (); Scalematrix.setscale (scale, scale, scale, 1); ColorMatrix ColorMatrix = new ColorMatrix (); Colormatrix.postconcat (Roratematrix); Colormatrix.postconcat (Saturationmatrix); Colormatrix.postconcat (Scalematrix); Canvas canvas = new canvas (NEWBITMAP); Paint paint = new paint (Paint.anti_alias_flag); Paint.setcolorfilter (New Colormatrixcolorfilter (ColorMatrix)); Canvas.drawbitmap (bitmap, 0, 0, paint); return newbitmap; }
Primary_color_activity.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_centerhorizontal=" true "Android:lay out_height= "Match_parent" tools:context= ". Mainactivity "> <imageview android:id=" @+id/image "android:layout_width=" Wrap_content "Androi d:layout_height= "Wrap_content" android:layout_margintop= "10DP" android:layout_marginbottom= "10DP"/> & lt;! --Adjust the color image--<seekbar android:id= "@+id/seek_rorate" android:layout_width= "Match_parent" Androi d:layout_height= "Wrap_content" android:layout_margintop= "10DP" android:layout_marginbottom= "10DP" and roid:layout_below= "@id/image"/> <!--adjust Transparency--<seekbar android:id= "@+id/seek_saturation" a Ndroid:layout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_margintop= "10DP" Android:layout_marginbottom= "10DP" android:layout_below= "@id/seek_rorate"/> <!--adjust Brightness-< Seekbar<span style= "FONT-SIZE:18PX;" > android:id= "@+id/seek_sacle" android:layout_width= "match_parent" android:layout_height= "Wrap_con Tent "android:layout_margintop=" 10DP "android:layout_marginbottom=" 10DP "android:layout_below=" @id/se Ek_saturation "/></relativelayout></span>
Primarycolor.java
Get the bitmap displayed in InitData () ImageView and show the maximum and default progress for Seekbar
To set the hue as an example:
The maximum color value is 255, the minimum value is 0,
Private final int max_value = 255; Private final int mid_value = 127;
Seek_rorate.setmax (max_value); Seek_rorate.setprogress (Mid_value);
Setting a progress change listener for Seekbar
@Override public void onprogresschanged (SeekBar SeekBar, int progress, Boolean fromuser) { switch ( Seekbar.getid ()) {case r.id.seek_rorate://hue rorate = ((progress-mid_value) * 1.0f/mid_value * +); break; Case r.id.seek_sacle://Brightness sacle = progress * 1.0F/MID_VALUE; break; Case r.id.seek_saturation://transparency saturation = progress * 1.0F/MID_VALUE; break; } Image.setimagebitmap (Imagehelper.handleimageeffect (bitmap, rorate, saturation, sacle)); }
:
The first Seekbar adjusts the hue, the second Seekbar adjusts the transparency, the third Seekbar adjusts the brightness
2. Using the color matrix to process Android pictures
The color matrix is the 4*5 matrix
Use a color matrix to set up a picture
(1) Create a new bitmap
(2) Setting the color matrix using ColorMatrix
(3) using the canvas to connect the new bitmap
(4) Set the color picker of the brush-color matrix
(5) Draw a new bitmap on the canvas with a brush
/** * Get bitmap * * @param bitmap * @param flagcolor color matrix by color matrix * @return */public static Bitmap Handleimagecolormatrix (Bitmap Bitmap, float[] flagcolor) { Bitmap bitmap1 = Bitmap.createbitmap ( Bitmap.getwidth (), Bitmap.getheight (), Bitmap.Config.ARGB_8888); ColorMatrix matrix = new ColorMatrix (); Matrix.set (Flagcolor); Canvas canvas = new canvas (BITMAP1); Paint paint = new paint (paint.anti_alias_flag); Paint.setcolorfilter (new Colormatrixcolorfilter (Matrix)); Canvas.drawbitmap (bitmap, 0, 0, paint); return bitmap1; }
Matrix_color_activity.xml
<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" android:gravity= "center_horizontal" tools:context= ". Mainactivity "> <imageview android:id=" @+id/image "android:layout_width=" Wrap_content "Androi d:layout_height= "Wrap_content" android:layout_margintop= "10DP" android:layout_marginbottom= "10DP" and roid:layout_weight= "2"/> <gridlayout android:layout_gravity= "center_horizontal" android:id= "@+id/g Ridlayout "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_we ight= "3" android:rowcount= "4" android:columncount= "5" ></GridLayout> <linearlayout Android Oid:layout_width= "Match_parent" android:layout_height= "Wrap_content" Android:orientation= "Horizontal" android:padding= "5DP" android:layout_gravity= "Bottom" > <button Android:id= "@+id/change" android:text= "Change" android:layout_weight= "1" android:layout _width= "Wrap_content" android:layout_height= "wrap_content"/> <button android:id= "@+id/ Reset "android:text=" reset "android:layout_weight=" 1 "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content"/> </LinearLayout></LinearLayout>
Initialize color values
Private float[] ColorMatrix = new FLOAT[20];
private void Initcolormatrix () {for (int i = 0; i <; i++) { if (i% 6 = = 0) { colormatrix[i] = 1; } else { colormatrix[i] = 0;}} }
add EditText to GridLayout
private void Initedittextmatrix () { EditText EditText; for (int i = 0; i <; i++) { editText = new EditText (matrixcolor.this); Edittext.settext (string.valueof (colormatrix[i)); Edittextmatrix[i] = EditText; Gridlayout.addview (EditText); } }
Why default is to set such a matrix value, because such a matrix and other matrices multiplied, will not change the original matrix value.
When changing the values in the matrix, we want to click Change, set the new matrix value to bitmap,
private void Change (Bitmap Bitmap, float[] flagcolor) { getedittext (); Bitmap Bitmap1 = Imagehelper.handleimagecolormatrix (Bitmap, flagcolor); Set a ImageView image.setimagebitmap (BITMAP1); }
when reset is clicked, the default value is set to bitmap
private void Reset () { initcolormatrix (); Setedittext (); Change (bitmap, ColorMatrix); } private void Setedittext () {for (int i = 0; i < edittextmatrix.length; i++) { Edittextmatrix[i].settext (stri Ng.valueof (Colormatrix[i])); } }
:
The first line changes the red, the second line changes the green, the third line changes the blue
3. Image processing via pixel values
(1) Create a new bitmap
(2) Gets the current bitmap of the pixel array
(3) Get a new array of pixels with the algorithm
(4) Set the number of pixel groups for the new bitmap
/** * Get negatives-by controlling pixels * * @param bitmap * @return */public static bitmap Handleimagepixel (bitmap bitm AP) {//Gets the length and width of the bitmap, which is actually the value of the pixel int width = bitmap.getwidth (); int height = bitmap.getheight (); Create a new Bitmap Bitmap Newbitmap = bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888); int[] Oldpixel = new int[width * height]; int[] Newpixel = new int[width * height]; int R, G, B, A, R1, G1, B1; Gets an array of pixels//received pixels of the current bitmap, gets the starting position of the pixel array, gets the width of the pixels, reads the x-coordinate of the pixel's start, reads the y-coordinate of the pixel's start, calculates the width range of the pixel, calculates the pixel's height range bitmap.getpixel S (oldpixel, 0, width, 0, 0, width, height); for (int i = 0; i < oldpixel.length; i++) {//get pixel value int color = Oldpixel[i]; Converts pixel values to red, green, blue, and transparency R = color.red (Color); g = Color.green (Color); b = Color.Blue (Color); A = Color.alpha (Color); Get new red, green, blue and transparency values, which is just one of the algorithms r1 = 255-r; G1 = 255-g; B1 = 255-b; Because the color value range is 0~255, it cannot exceed this range if (R1 < 0) {r1 = 0; } else if (R1 > 255) {r1 = 255; } if (G1 < 0) {g1 = 0; } else if (G1 > 255) {g1 = 255; } if (B1 < 0) {B1 = 0; } else if (B1 > 255) {b1 = 255; }//Convert the new red, green, blue and transparency to ARGB color values. int newcolor = Color.argb (A, R1, G1, B1); Add to new pixel array newpixel[i] = Newcolor; }//give the new bitmap set the pixel group Newbitmap.setpixels (newpixel, 0, width, 0, 0, width, height); return newbitmap; }
Pixel_color_activity.xml
<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:orientation= "vertical" android:gravity= "center_horizontal" tools:context= ". Mainactivity "> <linearlayout android:layout_width=" match_parent " android:layout_height=" wrap _content " android:orientation=" horizontal " android:layout_weight=" 1 "> <imageview Android:id= "@+id/image1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layout_weight= "1"/> <imageview android:id= "@+id/image2" android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" android:layout_weight= "1"/> </ Linearlayout></linearlayout>
Pixelcolor.java
Bitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.beauty); Image1 = (ImageView) Findviewbyid (r.id.image1); Image2 = (ImageView) Findviewbyid (r.id.image2); Image1.setimagebitmap (bitmap); Image2.setimagebitmap (Imagehelper.handleimagepixel (bitmap));
:
source Download: http://download.csdn.net/detail/forwardyzk/8512191
Android image processing