Android image Blur processing

Source: Internet
Author: User

/** Horizontal direction Blur */
private static float Hradius = 5;
/** Vertical direction Blur */
private static float Vradius = 5;
/** Fuzzy Iteration Degree */
private static int iterations = 3;

/**
* Image Gaussian Blur processing
* 
*/
public static drawable blurimages (Bitmap BMP, Context context) {

  int width = bmp.getwidth ();
  int height = bmp.getheight ();
  int[] Inpixels = new int[width * height];
  int[] Outpixels = new int[width * height];
  bitmap Bitmap = bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888);
  bmp.getpixels (inpixels, 0, width, 0, 0, width, height);
  for (int i = 0; i < iterations; i++) {
   blur (inpixels, outpixels, width, height, HR Adius);
   blur (outpixels, inpixels, height, width, vradius);
  }
  blurfractional (Inpixels, outpixels, width, height, hradius);
  blurfractional (outpixels, inpixels, height, width, vradius);
  bitmap.setpixels (inpixels, 0, width, 0, 0, width, height);
  drawable drawable = new Bitmapdrawable (Context.getresources (), bitmap);
  return drawable;
 }

/**
* Image Gaussian Blur algorithm
*/
public static void Blur (Int[] in, int[] out, int width, int height, float radius) {
int widthMinus1 = width-1;
int r = (int) radius;
int tablesize = 2 * r + 1;
int divide[] = new int[256 * Tablesize];

for (int i = 0; i < * tablesize; i++)
Divide[i] = i/tablesize;

int inindex = 0;

for (int y = 0; y < height; y++) {
int outindex = y;
int ta = 0, tr = 0, TG = 0, TB = 0;

for (int i =-R; I <= R; i++) {
int RGB = In[inindex + clamp (i, 0, width-1)];
Ta + = (RGB >>) & 0xFF;
TR + = (RGB >>) & 0xFF;
TG + = (RGB >> 8) & 0xFF;
TB + = RGB & 0xFF;
}

for (int x = 0; x < width; + +) {
Out[outindex] = (Divide[ta] << 24) | (Divide[tr] << 16) | (DIVIDE[TG] << 8) | DIVIDE[TB];

    int i1 = x + R + 1;
    if (I1 > WidthMinus1)
     i1 = WidthMinus1;
    int i2 = x-r;
    if (I2 < 0)
     i2 = 0;
    int RGB1 = In[inindex + I1];
    int rgb2 = In[inindex + i2];

Ta + = ((rgb1 >>) & 0xFF)-((Rgb2 >>) & 0xff);
TR + = ((RGB1 & 0xff0000)-(Rgb2 & 0xff0000)) >> 16;
TG + = ((RGB1 & 0xff00)-(Rgb2 & 0xff00)) >> 8;
TB + = (RGB1 & 0xff)-(Rgb2 & 0xff);
Outindex + = height;
}
Inindex + = width;
}
}

/**
* Image Gaussian Blur algorithm
* 
*/
public static void Blurfractional (int[] in, int[] out, int width, int height, float radius) {
Radius-= (int) radius;
float F = 1.0f/(1 + 2 * radius);
int inindex = 0;

for (int y = 0; y < height; y++) {
int outindex = y;

Out[outindex] = in[0];
Outindex + = height;
for (int x = 1; x < width-1; × x + +) {
int i = Inindex + x;
int rgb1 = in[i-1];
int rgb2 = In[i];
int RGB3 = in[i + 1];

int a1 = (rgb1 >>) & 0xFF;
int r1 = (rgb1 >>) & 0xFF;
int G1 = (RGB1 >> 8) & 0xFF;
int B1 = RGB1 & 0xFF;
int a2 = (rgb2 >>) & 0xFF;
int r2 = (rgb2 >>) & 0xFF;
int g2 = (rgb2 >> 8) & 0xFF;
int b2 = rgb2 & 0xFF;
int a3 = (rgb3 >>) & 0xFF;
int r3 = (rgb3 >>) & 0xFF;
int g3 = (rgb3 >> 8) & 0xFF;
int B3 = rgb3 & 0xFF;
a1 = A2 + (int) ((A1 + A3) * radius);
R1 = r2 + (int) ((r1 + R3) * radius);
G1 = g2 + (int) ((G1 + g3) * radius);
B1 = b2 + (int) ((B1 + b3) * radius);
A1 *= F;
R1 *= F;
G1 *= F;
B1 *= F;
Out[outindex] = (A1 << 24) | (R1 << 16) | (G1 << 8) | B1;
Outindex + = height;
}
Out[outindex] = in[width-1];
Inindex + = width;
}
}

public static int clamp (int x, int a, int b) {
Return (x < a)? A: (x > B)? b:x;
}

Android image Blur processing

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.