Android fuzzy processing realizes the _android effect of the picture hair glass

Source: Internet
Author: User

This article explained the Android virtual image, fuzzy picture, image of the implementation of the effect of glass, the specific contents are as follows

Effect as shown:

This method can be easily implemented using Renderscript on Android:

private void Blur (Bitmap bkg, view view, float radius) { 
  Bitmap overlay = Bitmap.createbitmap (View.getmeasuredwidth () , View.getmeasuredheight (), Bitmap.Config.ARGB_8888); 
  Canvas Canvas = new Canvas (overlay); 
  Canvas.drawbitmap (bkg,-view.getleft (),-view.gettop (), null); 
  Renderscript rs = renderscript.create (this); 
  Allocation Overlayalloc = Allocation.createfrombitmap (rs, overlay); 
  Scriptintrinsicblur blur = Scriptintrinsicblur.create (RS, overlayalloc.getelement ()); 
  Blur.setinput (overlayalloc); 
  Blur.setradius (RADIUS); 
  Blur.foreach (overlayalloc); 
  Overlayalloc.copyto (overlay); 
  View.setbackground (New Bitmapdrawable (Getresources (), overlay)); 
  Rs.destroy (); 
 } 

But Renderscript's approach requires Android API17, which is said to be implemented on Android 4.2.
below Android4.2 can be implemented in Java native code. But the efficiency will be much lower: this is a completely compromised way, not recommended.

* * This method is copied from http://stackoverflow.com/a/10028267/694378. 
  * The only modifications I ' ve made are to remove a couple of Log * statements which could-slow things down slightly. */Public Bitmap Fastblur (Bitmap sentbitmap, int radius) {//Stack Blur v1.0 from//http://www.quasimondo.co m/stackblurforcanvas/stackblurdemo.html////Java Author:mario Klingemann <mario at quasimondo.com>//HT tp://incubator.quasimondo.com//Created Feburary, ON//Android Port:yahel Bouaziz <yahel at kayenko.com& 
  Gt 
  http://www.kayenko.com//ported April 5th,%//This is a compromise between Gaussian Blur and Box Blur 
  It creates much better looking blurs than Box Blur, but are//7x faster than my Gaussian Blur. I called it Stack Blur because this describes best do this//filter works Internally:it creates a kind of M Oving stack//of colors whilst scanning through the IMAEel thereby it//just has to add one new blocks of color to the right side//of the stack and remove the leftmost color . The remaining//colors on the topmost layer of the stack are either added in//or reduced by one, depending on if 
  They are on the "right" or//on the left side of the stack. If you are a using this algorithm into your code please add//The following line:///Stack Blur Algorith 
 
  M by Mario Klingemann <mario@quasimondo.com> Bitmap Bitmap = sentbitmap.copy (Sentbitmap.getconfig (), true); 
  if (RADIUS < 1) {return (NULL); 
  int w = bitmap.getwidth (); 
 
  int h = bitmap.getheight (); 
  int[] pix = new int[w * h]; 
 
  Bitmap.getpixels (pix, 0, W, 0, 0, W, h); 
  int wm = W-1; 
  int HM = H-1; 
  int WH = w * h; 
 
  int div = radius + radius + 1; 
  int r[] = new INT[WH]; 
  int g[] = new INT[WH]; 
  int b[] = new INT[WH]; 
  int rsum, gsum, Bsum, x, Y, I, p, yp, Yi, yw; int vmin[] = new Int[math.max(W, h)]; 
  int divsum = (div + 1) >> 1; 
  Divsum *= divsum; 
  int dv[] = new int[256 * Divsum]; 
  for (i = 0; i < 256 * DIVSUM; i++) {Dv[i] = (i/divsum); 
 
  } yw = Yi = 0; 
  int[][] stack = new INT[DIV][3]; 
  int stackpointer; 
  int Stackstart; 
  Int[] Sir; 
  int RBS; 
  int r1 = radius + 1; 
  int routsum, goutsum, boutsum; 
 
  int rinsum, ginsum, binsum; 
   for (y = 0; y < h; y++) {rinsum = Ginsum = Binsum = Routsum = Goutsum = Boutsum = Rsum = gsum = bsum = 0; 
    for (i =-radius i <= radius; i++) {p = pix[yi + math.min (WM, Math.max (i, 0))]; 
    Sir = stack[i + radius]; 
    Sir[0] = (P & 0xff0000) >> 16; 
    SIR[1] = (P & 0x00ff00) >> 8; 
    SIR[2] = (P & 0x0000ff); 
    RBS = R1-math.abs (i); 
    Rsum + = sir[0] * RBS; 
    Gsum + = sir[1] * RBS; 
    Bsum + = sir[2] * RBS; 
     if (i > 0) {rinsum + = sir[0]; 
     Ginsum + = sir[1]; 
    Binsum + = sir[2]; else {routsum + = sir[0]; 
     Goutsum + = sir[1]; 
    Boutsum + = sir[2]; 
 
   } stackpointer = radius; 
    for (x = 0; x < W x + +) {R[yi] = Dv[rsum]; 
    G[yi] = Dv[gsum]; 
 
    B[yi] = Dv[bsum]; 
    Rsum-= routsum; 
    Gsum-= goutsum; 
 
    Bsum-= boutsum; 
    Stackstart = Stackpointer-radius + div; 
 
    sir = Stack[stackstart% div]; 
    Routsum-= sir[0]; 
    Goutsum-= sir[1]; 
 
    Boutsum-= sir[2]; 
    if (y = = 0) {Vmin[x] = math.min (x + radius + 1, WM); 
 
    } p = Pix[yw + vmin[x]]; 
    Sir[0] = (P & 0xff0000) >> 16; 
    SIR[1] = (P & 0x00ff00) >> 8; 
 
    SIR[2] = (P & 0x0000ff); 
    Rinsum + = sir[0]; 
    Ginsum + = sir[1]; 
 
    Binsum + = sir[2]; 
    Rsum + = Rinsum; 
    Gsum + = Ginsum; 
 
    Bsum + = Binsum; 
    Stackpointer = (stackpointer + 1)% Div; 
 
    sir = stack[(stackpointer)% div]; 
    Routsum + = sir[0]; 
    Goutsum + = sir[1]; 
 
    Boutsum + = sir[2]; 
    Rinsum-= sir[0]; Ginsum-= sir[1];
    Binsum-= sir[2]; 
   yi++; 
  } yw + = W; 
   for (x = 0; x < W x + +) {rinsum = Ginsum = Binsum = Routsum = Goutsum = Boutsum = Rsum = gsum = bsum = 0; 
   YP =-radius * W; 
 
    for (i =-radius i <= radius; i++) {Yi = Math.max (0, YP) + x; 
 
    Sir = stack[i + radius]; 
    Sir[0] = R[yi]; 
    SIR[1] = G[yi]; 
 
    SIR[2] = B[yi]; 
 
    RBS = R1-math.abs (i); 
    Rsum + = r[yi] * RBS; 
    Gsum + = g[yi] * RBS; 
 
    Bsum + = b[yi] * RBS; 
     if (i > 0) {rinsum + = sir[0]; 
     Ginsum + = sir[1]; 
    Binsum + = sir[2]; 
     else {routsum + = sir[0]; 
     Goutsum + = sir[1]; 
    Boutsum + = sir[2]; 
    } if (I < HM) {YP + = W; 
   } yi = x; 
   Stackpointer = radius; for (y = 0; y < h; y++) {//Preserve alpha channel: (0xff000000 & Pix[yi]) pix[yi] = (0xff000000 ; Pix[yi]) | (Dv[rsum] << 16) | (Dv[gsum] << 8) | 
 
    Dv[bsum]; 
    Rsum-= routsum; GsuM-= goutsum; 
 
    Bsum-= boutsum; 
    Stackstart = Stackpointer-radius + div; 
 
    sir = Stack[stackstart% div]; 
    Routsum-= sir[0]; 
    Goutsum-= sir[1]; 
 
    Boutsum-= sir[2]; 
    if (x = = 0) {Vmin[y] = math.min (y + r1, HM) * W; 
 
    } p = x + vmin[y]; 
    Sir[0] = r[p]; 
    SIR[1] = g[p]; 
 
    SIR[2] = b[p]; 
    Rinsum + = sir[0]; 
    Ginsum + = sir[1]; 
 
    Binsum + = sir[2]; 
    Rsum + = Rinsum; 
    Gsum + = Ginsum; 
 
    Bsum + = Binsum; 
    Stackpointer = (stackpointer + 1)% Div; 
 
    sir = Stack[stackpointer]; 
    Routsum + = sir[0]; 
    Goutsum + = sir[1]; 
 
    Boutsum + = sir[2]; 
    Rinsum-= sir[0]; 
    Ginsum-= sir[1]; 
 
    Binsum-= sir[2]; 
   Yi + W; 
 
  } bitmap.setpixels (pix, 0, W, 0, 0, W, h); 
 return (bitmap);  }

The above is the entire contents of this article, I hope to learn more about Android software programming help.

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.