Android photo effects: Black and white effects, rounded corners, Gaussian blur _android

Source: Internet
Author: User

1. Black and white effect

Copy Code code as follows:

/**
* Convert color map to black and white
*
* @param bitmap
* @return returns the converted Bitmap
*/
public static Bitmap Converttoblackwhite (Bitmap bmp) {
int width = bmp.getwidth (); Get the width of a bitmap
int height = bmp.getheight (); Gets the height of the bitmap

int[] pixels = new int[width * height]; Create a set of pixel points from the size of a bitmap

Bmp.getpixels (pixels, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24;
for (int i = 0; i < height; i++) {
for (int j = 0; J < width; j + +) {
int grey = Pixels[width * i + j];

int red = ((Grey & 0x00ff0000) >> 16);
int green = ((Grey & 0X0000FF00) >> 8);
int blue = (grey & 0X000000FF);

Grey = (int) (RED * 0.3 + green * 0.59 + blue * 0.11);
Grey = Alpha | (Grey << 16) | (Grey << 8) | Grey
Pixels[width * i + j] = grey;
}
}
Bitmap newbmp = bitmap.createbitmap (width, height, config.rgb_565);
Newbmp.setpixels (pixels, 0, width, 0, 0, width, height);
return newbmp;
}

2. Picture Fillet

Copy Code code as follows:

/**
* Convert to rounded corners
*
* @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 = 0xff424242;
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);
The second and third parameters draw the corner of a positive circle, otherwise the corner of the ellipse
Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint);

Paint.setxfermode (New Porterduffxfermode (mode.src_in));
Canvas.drawbitmap (BMP, Rect, rect, paint);

return newbmp;
}

3. Gaussian Blur

Copy Code code as follows:

/**
* Gaussian Blur
*
* @param bmp
* @return
*/
public static Bitmap Converttoblur (Bitmap bmp) {
Gauss 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 picture, the darker it becomes.

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.