Implementation of a custom algorithm based on the PS effect of Android platform Created with Rapha?l 2.1.2 Start Image Bitmap Get pixel getpixels PS algorithm modifies pixel point Color.red/green/blue Filter Image End
In the ARGB color space, using a (Transparency), R (Red), G (Green), B (Blue) four values to describe a pixel, then for a wide W h picture, a total of w*h pixels, you can use an array of object int [] pixels to represent the corresponding picture, pixels = {p1,p2,p3 ...}. By using ARGB to represent each pixel, the image can be described using a matrix of [w*h,4]:
pixels = { pa1,pr1,pg1,pb1, pa2,pr2,pg2,pb2, pa3,pr3,pg3,pb3, ……}
The Android platform provides the Bitmap.getpixels method for capturing pixels, and what I need to do is to traverse the pixel points of the image and calculate each pixel. Then, after the calculated pixel points are processed by the Color.red/green/blue method, the pixels are filled back into the bitmap to get the image after the filter. This method is more flexible than colormatrix, and can satisfy the effect of PS effect.
1, simple anti-color filter implementation
Take out the pixels of the image and subtract each pixel by 255, so you get a picture with a reversed color effect.
The algorithm is as follows:
/** * @author Neil * * Public class anticolorfilter implements imagefilterinterface { PrivateImageData image =NULL;//Picture information class Public Anticolorfilter(Bitmap BMP) {image =NewImageData (BMP); } PublicImageDataimageprocess() {intwidth = Image.getwidth ();intHeight = image.getheight ();intR, G, B, pixel; for(inty =0; Y < height; y++) { for(intx =0; x < width; × x + +) {R = Image.getrcomponent (x, y);//Get RGB tri-ColorsG = Image.getgcomponent (x, y); B = Image.getbcomponent (x, y); R =255R B =255B G =255G Image.setpixelcolor (x, Y, R, G, B); }//x}//Y returnImage }}
2, the implementation of oil painting filter
By checking the data to understand the oil painting filter algorithm is "the current point around a certain range of color to replace the current point color, the most common is random use of adjacent points to replace"
The algorithm is as follows:
Public ImageData imageprocess () {intwidth =Image. GetWidth ();intHeight =Image. GetHeight ();intR, G, B, pixel,xx =0, yy =0; for(inty =0; Y < height; y++) { for(intx =0; x < width; X + +) {intpos = Getrandomint (1,10000)% Model; xx = (x + pos) < width? (x + pos): (X-pos) >=0? (X-pos): x; yy = (y + pos) < height? (Y + pos): (Y-pos) >=0? (y-pos): y; R =Image. getrcomponent (xx, yy);//Get RGB tri-ColorsG =Image. getgcomponent (xx, yy); B =Image. getbcomponent (xx, yy);Image. setpixelcolor (x, Y, R, G, B); }//x}//Y return Image; } public staticintGetrandomint (intAintb) {int min= Math.min(A, B);int Max= Math.Max(A, B);return min+ (int) (Math.random () * (Max-min+1)); }
3, the implementation of frozen filter
The algorithm of freeze filter is to deepen the pixel color, each pixel is used RGB three colors to represent, (0,0,0) is pure black, and (255,255,255) is pure white, therefore will not have a pixel point of the RGB refers to become smaller, the color will deepen
intwidth =Image. GetWidth ();intHeight =Image. GetHeight ();intR, G, B, pixel; for(inty =0; Y < height; y++) { for(intx =0; x < width; X + +) {R =Image. getrcomponent (x, y);//Get RGB tri-ColorsG =Image. getgcomponent (x, y); B =Image. getbcomponent (x, y); Pixel = r-g-B; Pixel = pixel *3/2;if(Pixel <0) pixel =-pixel;if(Pixel >255) pixel =255; R = pixel; Pixel = g-b-R; Pixel = pixel *3/2;if(Pixel <0) pixel =-pixel;if(Pixel >255) pixel =255; G = pixel; Pixel = b-r-G; Pixel = pixel *3/2;if(Pixel <0) pixel =-pixel;if(Pixel >255) pixel =255; B = pixel;Image. setpixelcolor (x, Y, R, G, B); }//x}//Y
Android Camera Live Filter (iv)