Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

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

$
0
0
主要思想:
主要是利用三角正弦函数与余弦函数的变换效果,完成对像素的位移变换,产生水纹效果,因为自然界中的水纹多少都是正弦波或者余弦波的叠加效果。

参数解析:

支持两个输入参数设置,一个是波长,表示像素位移的多少,另外一个是周期表示正弦或者余弦函数的在像素中的变换周期。

代码如下:

	//水纹 
	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;
	}
效果如下:

                   效果图                                               原图



转载自:流浪的鱼

作者:qq_32353771 发表于2017/1/5 19:56:55 原文链接
阅读:49 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>