欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

一行java代码实现高斯模糊效果

程序员文章站 2022-06-29 20:12:34
本文实例为大家分享了本地图片或者网络图片高斯模糊效果(毛玻璃效果),具体内容如下 首先看效果图 1.本地图片高斯模糊 2.网络图片高斯模糊 github...

本文实例为大家分享了本地图片或者网络图片高斯模糊效果(毛玻璃效果),具体内容如下

首先看效果图

1.本地图片高斯模糊

一行java代码实现高斯模糊效果

2.网络图片高斯模糊

一行java代码实现高斯模糊效果

github网址:https://github.com/qiushi123/blurimageqcl

下面是使用步骤

一、实现本地图片或者网络图片的毛玻璃效果特别方便,只需要把下面的fastblurutil类复制到你的项目中就行

package com.testdemo.blur_image_lib10; 
 
import android.graphics.bitmap; 
import android.graphics.bitmapfactory; 
 
 
import java.io.bufferedinputstream; 
import java.io.bufferedoutputstream; 
import java.io.bytearrayoutputstream; 
import java.io.ioexception; 
import java.io.inputstream; 
import java.io.outputstream; 
import java.net.url; 
 
 
/** 
 * created by qcl on 14/7/15. 
 */ 
public class fastblurutil { 
 /** 
  * 根据imagepath获取bitmap 
  */ 
 /** 
  * 得到本地或者网络上的bitmap url - 网络或者本地图片的绝对路径,比如: 
  * <p> 
  * a.网络路径: url="http://blog.foreverlove.us/girl2.png" ; 
  * <p> 
  * b.本地路径:url="file://mnt/sdcard/photo/image.png"; 
  * <p> 
  * c.支持的图片格式 ,png, jpg,bmp,gif等等 
  * 
  * @param url 
  * @return 
  */ 
 public static int io_buffer_size = 2 * 1024; 
 public static bitmap geturlbitmap(string url, int scaleratio) { 
  int blurradius = 8;//通常设置为8就行。 
  if (scaleratio <= 0) { 
   scaleratio = 10; 
  } 
 
  bitmap originbitmap = null; 
  inputstream in = null; 
  bufferedoutputstream out = null; 
  try { 
   in = new bufferedinputstream(new url(url).openstream(), io_buffer_size); 
   final bytearrayoutputstream datastream = new bytearrayoutputstream(); 
   out = new bufferedoutputstream(datastream, io_buffer_size); 
   copy(in, out); 
   out.flush(); 
   byte[] data = datastream.tobytearray(); 
   originbitmap = bitmapfactory.decodebytearray(data, 0, data.length); 
 
 
   bitmap scaledbitmap = bitmap.createscaledbitmap(originbitmap, 
     originbitmap.getwidth() / scaleratio, 
     originbitmap.getheight() / scaleratio, 
     false); 
   bitmap blurbitmap = doblur(scaledbitmap, blurradius, true); 
   return blurbitmap; 
  } catch (ioexception e) { 
   e.printstacktrace(); 
   return null; 
  } 
 } 
 
 private static void copy(inputstream in, outputstream out) 
   throws ioexception { 
  byte[] b = new byte[io_buffer_size]; 
  int read; 
  while ((read = in.read(b)) != -1) { 
   out.write(b, 0, read); 
  } 
 } 
 
 // 把本地图片毛玻璃化 
 public static bitmap toblur(bitmap originbitmap, int scaleratio) { 
  //  int scaleratio = 10; 
  // 增大scaleratio缩放比,使用一样更小的bitmap去虚化可以到更好的得模糊效果,而且有利于占用内存的减小; 
  int blurradius = 8;//通常设置为8就行。 
  //增大blurradius,可以得到更高程度的虚化,不过会导致cpu更加intensive 
 
 
  /* 其中前三个参数很明显,其中宽高我们可以选择为原图尺寸的1/10; 
  第四个filter是指缩放的效果,filter为true则会得到一个边缘平滑的bitmap, 
  反之,则会得到边缘锯齿、pixelrelated的bitmap。 
  这里我们要对缩放的图片进行虚化,所以无所谓边缘效果,filter=false。*/ 
  if (scaleratio <= 0) { 
   scaleratio = 10; 
  } 
  bitmap scaledbitmap = bitmap.createscaledbitmap(originbitmap, 
    originbitmap.getwidth() / scaleratio, 
    originbitmap.getheight() / scaleratio, 
    false); 
  bitmap blurbitmap = doblur(scaledbitmap, blurradius, true); 
  return blurbitmap; 
 } 
 
 
 public static bitmap doblur(bitmap sentbitmap, int radius, boolean canreuseinbitmap) { 
  bitmap bitmap; 
  if (canreuseinbitmap) { 
   bitmap = sentbitmap; 
  } else { 
   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); 
 } 
 
 
} 

二、使用实例

package com.testdemo; 
 
 import android.app.activity; 
 import android.content.res.resources; 
 import android.graphics.bitmap; 
 import android.graphics.bitmapfactory; 
 import android.os.bundle; 
 import android.text.textutils; 
 import android.view.view; 
 import android.widget.edittext; 
 import android.widget.imageview; 
 
 import com.testdemo.blur_image_lib10.fastblurutil; 
 
 public class mainactivity10_blurimage extends activity { 
  imageview image; 
  edittext edit; 
 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
   super.oncreate(savedinstancestate); 
   setcontentview(r.layout.activity_main10_blur_image); 
   image = (imageview) findviewbyid(r.id.image); 
   edit = (edittext) findviewbyid(r.id.edit); 
 
 
   findviewbyid(r.id.button2).setonclicklistener(new view.onclicklistener() { 
    @override 
    public void onclick(view v) { 
     string pattern = edit.gettext().tostring(); 
     int scaleratio = 0; 
     if (textutils.isempty(pattern)) { 
      scaleratio = 0; 
     } else if (scaleratio < 0) { 
      scaleratio = 10; 
     } else { 
      scaleratio = integer.parseint(pattern); 
     } 
 
     //  获取需要被模糊的原图bitmap 
     resources res = getresources(); 
     bitmap scaledbitmap = bitmapfactory.decoderesource(res, r.drawable.filter); 
 
     //  scaledbitmap为目标图像,10是缩放的倍数(越大模糊效果越高) 
     bitmap blurbitmap = fastblurutil.toblur(scaledbitmap, scaleratio); 
     image.setscaletype(imageview.scaletype.center_crop); 
     image.setimagebitmap(blurbitmap); 
    } 
   }); 
 
   findviewbyid(r.id.button).setonclicklistener(new view.onclicklistener() { 
    @override 
    public void onclick(view v) { 
     //url为网络图片的url,10 是缩放的倍数(越大模糊效果越高) 
     final string pattern = edit.gettext().tostring(); 
 
     final string url = 
       //  "http://imgs.duwu.me/duwu/doc/cover/201601/18/173040803962.jpg"; 
       "http://b.hiphotos.baidu.com/album/pic/item/caef76094b36acafe72d0e667cd98d1000e99c5f.jpg?psign=e72d0e667cd98d1001e93901213fb80e7aec54e737d1b867"; 
     new thread(new runnable() { 
      @override 
      public void run() { 
       int scaleratio = 0; 
       if (textutils.isempty(pattern)) { 
        scaleratio = 0; 
       } else if (scaleratio < 0) { 
        scaleratio = 10; 
       } else { 
        scaleratio = integer.parseint(pattern); 
       } 
 //      下面的这个方法必须在子线程中执行 
       final bitmap blurbitmap2 = fastblurutil.geturlbitmap(url, scaleratio); 
        
 //      刷新ui必须在主线程中执行 
        app.runonuithread(new runnable() {//这个是我自己封装的在主线程中刷新ui的方法。 
        @override 
        public void run() { 
         image.setscaletype(imageview.scaletype.center_crop); 
         image.setimagebitmap(blurbitmap2); 
 
        } 
       }); 
      } 
     }).start(); 
 
 
    } 
   }); 
 
 
  } 
 
 }  

下面是上面的布局文件

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical"> 
 
 
<imageview 
android:id="@+id/image2" 
android:layout_width="match_parent" 
android:layout_height="220dp" 
android:background="@drawable/filter"/> 
 
 
<linearlayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal"> 
 
 
<edittext 
android:id="@+id/edit" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_margintop="15dp" 
android:hint="输入模糊度" 
/> 
 
 
<button 
android:id="@+id/button2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="转化毛玻璃"/> 
 
 
<button 
android:id="@+id/button" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_marginleft="4dp" 
android:text="转化网络图片毛玻璃"/> 
</linearlayout> 
 
 
<imageview 
android:id="@+id/image" 
android:layout_width="match_parent" 
android:layout_height="220dp" 
android:layout_below="@+id/image2" 
/> 
</linearlayout> 

三、注意事项

1.一定不要忘记intent权限
2.加载网络图片时一定要在子线程中执行。

github网址:https://github.com/qiushi123/blurimageqcl

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。