Android image Processing (iii) negative, nostalgia, emboss, grayscale, fillet, oil painting and left and right symmetry

Source: Internet
Author: User

Last shot, a total of negative, nostalgia, relief, grayscale, fillet, oil painting and symmetry 7 kinds of effects:
The following imagehelper are implemented:

 Packagecom.zms.imageprocess;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.ColorMatrix;ImportAndroid.graphics.ColorMatrixColorFilter;ImportAndroid.graphics.Matrix;ImportAndroid.graphics.Paint;ImportAndroid.graphics.PorterDuff;ImportAndroid.graphics.PorterDuffXfermode;ImportAndroid.graphics.Rect;ImportAndroid.graphics.RectF;ImportJava.util.Random;/** * Created by Administrator on 2015/4/1. * * Public  class imagehelper {    //hue-hue saturation-saturation lum-brightness     Public StaticBitmapImageeffect(Bitmap Bitmap,floatHuefloatSaturation,floatLuminance) {Bitmap Mbitmap = Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight (), Bitmap.Config.ARGB_8888); Canvas Canvas =NewCanvas (MBITMAP); Paint paint =NewPaint (Paint.anti_alias_flag);//hue-hue/ HueColorMatrix Huematrix =NewColorMatrix (); Huematrix.setrotate (0, hue);//REDHuematrix.setrotate (1, hue);//GREENHuematrix.setrotate (2, hue);//BLUE        //saturation-saturationColorMatrix Saturationmatrix =NewColorMatrix (); Saturationmatrix.setsaturation (saturation);//luminance-brightness/lightnessColorMatrix Luminancematrix =NewColorMatrix ();//Setscale (float rscale, float gscale, float bscale, float ascale)Luminancematrix.setscale (luminance, luminance, luminance,1); ColorMatrix Imagematrix =NewColorMatrix ();        Imagematrix.postconcat (Huematrix);        Imagematrix.postconcat (Saturationmatrix);        Imagematrix.postconcat (Luminancematrix); Paint.setcolorfilter (NewColormatrixcolorfilter (Imagematrix));//Drawbitmap (@NonNull Bitmap Bitmap, float left, float top, @Nullable paint paint)Canvas.drawbitmap (Bitmap,0,0, paint);returnMbitmap; } Public StaticBitmapHandleimage(Bitmap BM,inteffect) {Bitmap Bitmap = Bitmap.createbitmap (Bm.getwidth (), Bm.getheight (), Bitmap.Config.ARGB_8888 );intwidth = Bm.getwidth ();intHeight = bm.getheight ();intcolor =0;intR, G, B, A, R1, G1, B1;int[] Oldpx =New int[Width * height];int[] newpx =New int[Width * height]; Bm.getpixels (OLDPX,0, Width,0,0, width, height);Switch(effect) { Case 0://Negative effect                 for(inti =0; I < width * height;                    i++) {color = Oldpx[i];                    R = color.red (Color);                    g = Color.green (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); } bitmap.setpixels (NEWPX,0, Width,0,0, width, height); Break; Case 1://Nostalgic effect                 for(inti =0; I < width * height;                    i++) {color = Oldpx[i];                    A = Color.alpha (Color);                    R = color.red (Color);                    g = Color.green (Color);                    b = Color.Blue (Color); R1 = (int) (0.393* R +0.769* g +0.189* b); G1 = (int) (0.349* R +0.686* g +0.168* b); B1 = (int) (0.272* R +0.534* g +0.131* b);if(R1 >255) {R1 =255; }if(G1 >255) {G1 =255; }if(B1 >255) {B1 =255;                } Newpx[i] = Color.argb (A, R1, G1, B1); } bitmap.setpixels (NEWPX,0, Width,0,0, width, height); Break; Case 2://Emboss effect                intColorbefore =0; for(inti =1; I < width * height; i++) {Colorbefore = Oldpx[i-1];                    A = Color.alpha (Colorbefore);                    R = color.red (Colorbefore);                    g = Color.green (Colorbefore);                    b = Color.Blue (Colorbefore);                    color = Oldpx[i];                    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); } bitmap.setpixels (NEWPX,0, Width,0,0, width, height); Break; Case 3://Grayscale                intAlpha =0xFF<< -; for(inti =0; i < height; i++) { for(intj =0; J < width; J + +) {intGrey = Oldpx[width * i + j];intRed = ((Grey &0x00ff0000) >> -);intGreen = ((Grey &0x0000FF00) >>8);intBlue = (Grey &0x000000ff); Grey = (int) ((float) Red *0.3+ (float) Green *0.59+ (float) Blue *0.11); Grey = Alpha | (Grey << -) | (Grey <<8) |                        Grey                    Newpx[width * i + j] = grey; }} bitmap.setpixels (NEWPX,0, Width,0,0, width, height); Break; Case 4://Fillet                intROUNDPX = -; Canvas Canvas =NewCanvas (bitmap);intColorround =0xff424242; Paint paint =NewPaint (); Rect rect =NewRect (0,0, Bm.getwidth (), Bm.getheight ()); RECTF RECTF =NewRECTF (rect); Paint.setantialias (true); Canvas.drawargb (0,0,0,0);                Paint.setcolor (Colorround);                Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint); Paint.setxfermode (NewPorterduffxfermode (PorterDuff.Mode.SRC_IN)); Canvas.drawbitmap (BM, rect, rect, paint); Break; Case 5://oil painting                intRadio =0; Random rnd =NewRandom ();intIModel =Ten;inti = Width-imodel; while(I >1) {intj = Height-imodel; while(J >1) {intIPos = Rnd.nextint (Ten)% IModel;                        color = Bm.getpixel (i + iPos, j + iPos);                        Bitmap.setpixel (i, J, color); j = J-1; } i = i-1; } Break; Case 6://Left /right symmetryCanvas CANVAS2 =NewCanvas (bitmap);                Canvas2.drawcolor (Color.Black); Canvas2.drawbitmap (BM,0,0,NULL); Matrix Matrix =NewMatrix ();float[] values = {-1F0.0F0.0F0.0F1F0.0F0.0F0.0F1.0f};                Matrix.setvalues (values); Bitmap = Bitmap.createbitmap (BM,0,0, Bm.getwidth (), Bm.getheight (), Matrix,true); Canvas2.drawbitmap (Bitmap, Bm.getwidth (),0,NULL); Break; }returnBitmap }}

Android image Processing (iii) negative, nostalgia, emboss, grayscale, fillet, oil painting and left and right symmetry

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.