【轉】一、android圖片特效處理之模糊效果

來源:互聯網
上載者:User

標籤:android   c   style   class   blog   code   

這篇將講到圖片特效處理的模糊效果。跟前面一樣是對像素點進行處理,演算法是通用的,但耗時會更長,至於為什麼,看了下面的代碼你就會明白。

演算法:

一、簡單演算法:將像素點周圍八個點包括自身一共九個點的RGB值分別相加後平均,作為當前像素點的RGB值,即可實現效果。

舉例:

ABC

DEF

GHI

假如當前點是E,那麼會有:

E.r = (A.r + B.r + C.r + D.r + E.r + F.r + G.r + H.r + I.r) / 9  // r表示的是E像素點RGB值的R值

E像素點的GB值類似。

二、採用高斯模糊:

高斯矩陣:

int[] gauss = new int[] { 1, 2, 1, 2, 4, 2, 1, 2, 1 };

演算法是:將九個點的RGB值分別與高斯矩陣中的對應項相乘的和,然後再除以一個相應的值作為當前像素點的RGB值。

舉例:(還是上面的九個點)
假如當前點是E,那麼會有:

int delta = 16;E.r =( A.r * gauss[0] + B.r * gauss[1] + C.r * gauss[2] + D.r * gauss[3] + E.r * gauss[4] + F.r * gauss[5] + G.r * gauss[6] + H.r * gauss[7] + I.r * gauss[8]) / delta

E像素點的GB值類似,delta的取值貌似沒有規定值,可以自己設定任意值,但要想達到效果,能設的值很少,下面圖片是值為16的效果。
處理效果:

原圖片:

處理後:

 

兩種處理方式的代碼:

/**     * 模糊效果     * @param bmp     * @return     */    private Bitmap blurImage(Bitmap bmp)    {        int width = bmp.getWidth();        int height = bmp.getHeight();        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);                int pixColor = 0;                int newR = 0;        int newG = 0;        int newB = 0;                int newColor = 0;                int[][] colors = new int[9][3];        for (int i = 1, length = width - 1; i < length; i++)        {            for (int k = 1, len = height - 1; k < len; k++)            {                for (int m = 0; m < 9; m++)                {                    //(s,p)就是上面的E點,switch語句的0~8共9個分支代表了圍繞E(包括E點)的9個點座標                    int s = 0;                    int p = 0;                    switch(m)                    {                    case 0:                        s = i - 1;                        p = k - 1;                        break;                    case 1:                        s = i;                        p = k - 1;                        break;                    case 2:                        s = i + 1;                        p = k - 1;                        break;                    case 3:                        s = i + 1;                        p = k;                        break;                    case 4:                        s = i + 1;                        p = k + 1;                        break;                    case 5:                        s = i;                        p = k + 1;                        break;                    case 6:                        s = i - 1;                        p = k + 1;                        break;                    case 7:                        s = i - 1;                        p = k;                        break;                    case 8:                        s = i;                        p = k;                    }                    pixColor = bmp.getPixel(s, p);                    //分別取得這些點的R,G,B值                    colors[m][0] = Color.red(pixColor);                    colors[m][1] = Color.green(pixColor);                    colors[m][2] = Color.blue(pixColor);                }                //9個點的R,G,B值分別相加                for (int m = 0; m < 9; m++)                {                    newR += colors[m][0];                    newG += colors[m][1];                    newB += colors[m][2];                }                //再取平均                newR = (int) (newR / 9F);                newG = (int) (newG / 9F);                newB = (int) (newB / 9F);                //保證每個值的範圍在0~255                newR = Math.min(255, Math.max(0, newR));                newG = Math.min(255, Math.max(0, newG));                newB = Math.min(255, Math.max(0, newB));                                newColor = Color.argb(255, newR, newG, newB);                bitmap.setPixel(i, k, newColor);                                newR = 0;                newG = 0;                newB = 0;            }        }                return bitmap;    }        /**     * 柔化效果(高斯模糊)(最佳化後比上面快三倍)     * @param bmp     * @return     */    private Bitmap blurImageAmeliorate(Bitmap bmp)    {        long start = System.currentTimeMillis();        // 高斯矩陣        int[] gauss = new int[] { 1, 2, 1, 2, 4, 2, 1, 2, 1 };                int width = bmp.getWidth();        int height = bmp.getHeight();        Bitmap bitmap = 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; // 值越小圖片會越亮,越大則越暗                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 + (int) (pixR * gauss[idx]);                        newG = newG + (int) (pixG * gauss[idx]);                        newB = newB + (int) (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;            }        }                bitmap.setPixels(pixels, 0, width, 0, 0, width, height);        long end = System.currentTimeMillis();        Log.d("may", "used time="+(end - start));        return bitmap;    }

在最佳化後的代碼中要注意了,pixels數組不能超過規定的大小,也就是說圖片的尺寸不能太大,否則會棧記憶體溢出。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.