Android圖像處理之--水紋效果

主要思想:

主要是利用三角正弦函數與余弦函數的變換效果,完成對像素的位移變換,產生水紋效果,因為自然界中的水紋多少都是正弦波或者余弦波的疊加效果。

參數解析:

支持兩個輸入參數設置,一個是波長,表示像素位移的多少,另外一個是周期表示正弦或者余弦函數的在像素中的變換周期。

代碼如下:

//水紋 
    public static class Point1 {
        private int x;
        private int y;
        public Point1(int x, int y) {
            this.x = x;
            this.y = y;
        }
        public int getRow() {
            return this.y;
        }
        public int getCol() {
            return this.x;
        }
    }

    public static Bitmap WaterWave1(Bitmap bitmap){
        double wave = 10.0;
        double period = 64;

        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap result = Bitmap.createBitmap(width,height, Bitmap.Config.RGB_565);

        int[] inPixels = new int[width*height];
        int[] outPixels = new int[width*height];
        Point1[][] ssPixels = new Point1[height][width];
        bitmap.getPixels(inPixels, 0, width, 0, 0, width, height);
        int index = 0, index2 = 0;
        int xoffset = 0, yoffset = 0;

        for(int row=0; row<height; row++) {
            for(int col=0; col<width; col++) {
                xoffset = (int)((double)wave * Math.sin(2.0 * Math.PI * (float)row / period));
                yoffset = (int)((double)wave * Math.cos(2.0 * Math.PI * (float)col / period));
                xoffset = xoffset + col;
                yoffset = yoffset + row;
                if(xoffset < 0 || xoffset >=width) {
                    xoffset = 0;
                }
                if(yoffset < 0 || yoffset >=height) {
                    yoffset = 0;
                }

                ssPixels[row][col] = new Point1(xoffset, yoffset);
            }
        }

        for(int row=0; row<height; row++) {
            int ta = 0, tr = 0, tg = 0, tb = 0;
            for (int col = 0; col < width; col++) {
                index = row * width + col;
                index2 = ssPixels[row][col].getRow() * width + ssPixels[row][col].getCol();
                ta = (inPixels[index2] >> 24) & 0xff;
                tr = (inPixels[index2] >> 16) & 0xff;
                tg = (inPixels[index2] >> 8) & 0xff;
                tb = inPixels[index2] & 0xff;
                outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;
            }
        }
        result.setPixels(outPixels, 0, width, 0, 0, width, height);
        return result;
    }

                                              效果圖                                                                          原圖

 

 

來自:流浪的魚

 

 

 本文由用戶 jiaohuage 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!