【轉】七、android圖片特效處理之光暈效果

來源:互聯網
上載者:User

標籤:android   c   style   class   blog   code   

這篇將講到圖片特效處理的圖片光暈效果。跟前面一樣是對像素點進行處理,本篇實現的思路可參見android影像處理系列之九--圖片特效處理之二-模糊效果和android影像處理系列之十三--圖片特效處理之六-光照效果。實現的效果是圓圈之內圖片像素點不變,圓圈之外的點做模糊處理。所以用到了模糊效果和光照效果裡面的是否是在圓圈內的演算法,可以說是上面提到的兩篇的效果的組合。

下面看:

原圖:

 

光暈效果看得不是很明顯,模糊強度不夠,但是還能明顯看到圖片中有一個圓圈,圈內地區要比圈外地區看得清楚一點(MM的左右臉就可以看到效果)。處理效果不是很理想,在此只能拋磚引玉。下面貼代碼:

/**     * 光暈效果     * @param bmp     * @param x 光暈中心點在bmp中的x座標     * @param y 光暈中心點在bmp中的y座標     * @param r 光暈的半徑     * @return     */    public Bitmap halo(Bitmap bmp, int x, int y, float r)    {        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 = 18; // 值越小圖片會越亮,越大則越暗                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;                int distance = (int) (Math.pow(k - x, 2) + Math.pow(i - y, 2));                // 不是中心地區的點做模糊處理                if (distance > r * r)                {                    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;    }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.