Android图像处理之--水纹效果

jiaohuage 7年前
   <p><strong>主要思想:</strong></p>    <p>主要是利用三角正弦函数与余弦函数的变换效果,完成对像素的位移变换,产生水纹效果,因为自然界中的水纹多少都是正弦波或者余弦波的叠加效果。</p>    <p><strong>参数解析:</strong></p>    <p>支持两个输入参数设置,一个是波长,表示像素位移的多少,另外一个是周期表示正弦或者余弦函数的在像素中的变换周期。</p>    <p>代码如下:</p>    <pre>  //水纹    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;   }</pre>    <p style="text-align: center;">                                              效果图                                                                          原图</p>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/7ebe41be9b927193fbbe3da63a94e993.png"></p>    <p> </p>    <p> </p>    <p>来自:流浪的鱼</p>    <p> </p>    <p> </p>