Android image effects: black and white effects, rounded corner effects, Gaussian blur

Source: Internet
Author: User

1. black/white Effect

Copy codeThe Code is as follows :/**
* Convert a color chart to a black or white chart
*
* @ Param bitmap
* @ Return returns the converted bitmap.
*/
Public static Bitmap convertToBlackWhite (Bitmap bmp ){
Int width = bmp. getWidth (); // obtain the width of the bitmap.
Int height = bmp. getHeight (); // obtain the height of the bitmap.

Int [] pixels = new int [width * height]; // creates a pixel array based on the bitmap size.

Bmp. getPixels (pixels, 0, width, 0, 0, width, height );
Int alpha = 0xFF <24;
For (int I = 0; I For (int j = 0; j <width; j ++ ){
Int gray = pixels [width * I + j];

Int red = (gray & 0x00FF0000)> 16 );
Int green = (gray & 0x0000FF00)> 8 );
Int blue = (gray & 0x000000FF );

Gray = (int) (red * 0.3 + green * 0.59 + blue * 0.11 );
Gray = alpha | (gray <16) | (gray <8) | gray;
Pixels [width * I + j] = gray;
}
}
Bitmap newBmp = Bitmap. createBitmap (width, height, Config. RGB_565 );
NewBmp. setPixels (pixels, 0, width, 0, 0, width, height );
Return newBmp;
}

2. rounded corners

Copy codeThe Code is as follows :/**
* Convert to rounded corner
*
* @ Param bmp
* @ Param roundPx
* @ Return
*/
Public static Bitmap convertToRoundedCorner (Bitmap bmp, float roundPx ){

Bitmap newBmp = Bitmap. createBitmap (bmp. getWidth (), bmp. getHeight (),
Config. ARGB_8888 );
// Get the canvas
Canvas canvas = new Canvas (newBmp );

Final int color = 0xff0000242;
Final Paint paint = new Paint ();
Final Rect rect = new Rect (0, 0, bmp. getWidth (), bmp. getHeight ());
Final RectF rectF = new RectF (rect );

Paint. setAntiAlias (true );
Canvas. drawARGB (0, 0, 0, 0 );
Paint. setColor (color );
// If the second and third parameters are the same, they are the corner of the positive circle. Otherwise, they are the corner of the elliptic.
Canvas. drawRoundRect (rectF, roundPx, roundPx, paint );

Paint. setXfermode (new porterduxfermode (Mode. SRC_IN ));
Canvas. drawBitmap (bmp, rect, rect, paint );

Return newBmp;
}

3. Gaussian blur

Copy codeThe Code is as follows :/**
* Gaussian blur
*
* @ Param bmp
* @ Return
*/
Public static Bitmap convertToBlur (Bitmap bmp ){
// Gaussian Matrix
Int [] gauss = new int [] {1, 2, 1, 2, 4, 2, 1, 2, 1 };

Int width = bmp. getWidth ();
Int height = bmp. getHeight ();
Bitmap newBmp = Bitmap. createBitmap (width, height,
Bitmap. Config. RGB_565 );

Int pixR = 0;
Int pixG = 0;
Int pixB = 0;

Int pixColor = 0;

Int newR = 0;
Int newG = 0;
Int newB = 0;

Int delta = 16; // The smaller the value, the brighter the image. The larger the value, the darker the image.

Int idx = 0;
Int [] pixels = new int [width * height];
Bmp. getPixels (pixels, 0, width, 0, 0, width, height );
For (int I = 1, length = height-1; I <length; I ++ ){
For (int k = 1, len = width-1; k <len; k ++ ){
Idx = 0;
For (int m =-1; m <= 1; m ++ ){
For (int n =-1; n <= 1; n ++ ){
PixColor = pixels [(I + m) * width + k + n];
PixR = Color. red (pixColor );
PixG = Color. green (pixColor );
PixB = Color. blue (pixColor );

NewR = newR + pixR * gauss [idx];
NewG = newG + pixG * gauss [idx];
NewB = newB + pixB * gauss [idx];
Idx ++;
}
}

NewR/= delta;
NewG/= delta;
NewB/= delta;

NewR = Math. min (255, Math. max (0, newR ));
NewG = Math. min (255, Math. max (0, newG ));
NewB = Math. min (255, Math. max (0, newB ));

Pixels [I * width + k] = Color. argb (255, newR, newG, newB );

NewR = 0;
NewG = 0;
NewB = 0;
}
}

NewBmp. setPixels (pixels, 0, width, 0, 0, width, height );

Return newBmp;
}

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.