http://blog.csdn.net/qq_21430549/article/details/49635965
版权声明:本文为博主原创文章,未经博主允许不得转载。如有错误,还请指出。
这篇博客将会通过对像素的RGB分量做一个处理,然后达到一些特效。并没有很高端大气的代码。也没用使用ImageFilter等一些库。多数参考了别人,大神勿喷。
首先看一下今天的效果图。
由于上传大小限制的关系,只有一小部分。当然,功能中除了光晕,其他都是实现了的。如果可以的话,我回上传到github上gif图。代码请在文末下载。那么接下来,我们就来看下如何实现这些。
再次声明,绝大多数是操作像素实现的。速度上可能会很慢。不过不要紧,要的是思想。
由于代码太多的原因,下面只会给出关键性代码,更多代码请前往github。
- 图片灰度化
灰度化原理:当前像素值=0.3r+0.59g+0.11b
灰度化我在这里用2中方法实现的。一种是操作ColorMatrix,另一种是颜色分量处理。代码如下
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> Bitmap <span class="hljs-title" style="box-sizing: border-box;">doPro</span>(Bitmap src) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> width,height; height = src.getHeight(); width = src.getWidth(); Bitmap bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888); Canvas canvas = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Canvas(bitmap); Paint paint = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Paint(); ColorMatrix colorMatrix = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ColorMatrix(); colorMatrix.setSaturation(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>); ColorMatrixColorFilter filter = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ColorMatrixColorFilter(colorMatrix); paint.setColorFilter(filter); canvas.drawBitmap(src, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, paint); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> bitmap; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul>
关于ColorMatrix,参考官方文档。
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> Bitmap <span class="hljs-title" style="box-sizing: border-box;">doProByPix</span>(Bitmap src) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> width = src.getWidth(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> height = src.getHeight(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//创建像素点数组</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>[] pixels = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>[width*height]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> alpha,grey,red,green,blue; src.getPixels(pixels,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,width,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,width,height); alpha = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xFF</span><<<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">24</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> ; i < height ; i++){ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> j = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> ; j < width ; j++){ grey = pixels[width*i+j]; red = ((grey & <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00FF0000</span>)>><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>); green = ((grey & <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x0000FF00</span>)>><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>); blue = ((grey & <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x000000FF</span>)); grey = (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>)((<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span>)red*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.3</span>+(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span>)green*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.59</span>+(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span>)blue*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.11</span>); grey = alpha | (grey<<<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>)|(grey<<<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>)|grey; pixels[width*i+j]=grey; } } Bitmap pro = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888); pro.setPixels(pixels,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,width,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,width,height); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> pro; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li></ul>
上面这个通过对颜色的 与,左移,右移来拿到颜色分量,当然,也有更简单的方法拿到分离,后面会说。
- 对亮度/饱和度/对比度的操作
这里的操作就会用到颜色矩阵,颜色矩阵,我并不会多说。如果感兴趣就取查看官方文档。这并不是重点
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> Bitmap <span class="hljs-title" style="box-sizing: border-box;">doPro</span>(Bitmap src) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> width = src.getWidth(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> height = src.getHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); ColorMatrix colorMatrix = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ColorMatrix(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//设置饱和度 设置为0.7 范围 0 - 1</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//colorMatrix.setSaturation((float) 0.7);</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//设置亮度</span> colorMatrix.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">set</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span>[]{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">70</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">70</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">70</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>}); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//改变对比度</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// colorMatrix.set(new float[]{2, 0, 0, 0, 0,</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 0, 2, 0, 0, 0,</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 0, 0, 2, 0, 0,</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 0, 0, 0, 1, 0});</span> Paint paint = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Paint(); paint.setColorFilter(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ColorMatrixColorFilter(colorMatrix)); Canvas canvas = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Canvas(bitmap); canvas.drawBitmap(src,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,paint); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> bitmap; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li></ul>
- 水印文字
就是在图片之上绘制文字,详情看代码。 - 怀旧效果的实现
原理:
R=0.393r+0.769g+0.189b
G=0.349r+0.686g+0.168b
B=0.272r+0.534g+0.131b
怀旧效果也可以通过颜色矩阵来实现,下面是通过颜色矩阵实现的部分代码
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">colorMatrix.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">set</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span>[]{ (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span>) <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.393</span>, (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span>) <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.768</span>, (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span>) <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.189</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span>) <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.349</span>, (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span>) <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.686</span>, (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span>) <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.168</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span>) <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.272</span>, (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span>) <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.534</span>, (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span>) <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.131</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> });</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>
接下来我们看如何通过操作像素分量来实现
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> Bitmap <span class="hljs-title" style="box-sizing: border-box;">doProByPix</span>(Bitmap src) { <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 怀旧效果原理 * R=0.393r+0.769g+0.189b * G=0.349r+0.686g+0.168b * B=0.272r+0.534g+0.131b */</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">long</span> startTime = System.currentTimeMillis(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> width = src.getWidth(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> height = src.getHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> pixColor,pixR,pixG,pixB,newR,newG,newB; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>[] pixels= <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>[width*height]; src.getPixels(pixels, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, width, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, width, height); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < height; i++) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> j = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; j < width; j++) { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取对应点的像素</span> pixColor = pixels[width*i+j]; pixR = Color.red(pixColor); pixG = Color.green(pixColor); pixB = Color.blue(pixColor); newR = (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>) (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.393</span> * pixR + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.769</span> * pixG + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.189</span> * pixB); newG = (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>) (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.349</span> * pixR + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.686</span> * pixG + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.168</span> * pixB); newB = (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>) (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.272</span> * pixR + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.534</span> * pixG + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.131</span> * pixB); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> newColor = Color.argb(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>,Math.min(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>,newR),Math.min(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>,newG),Math.min(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>, newB)); pixels[width*i+j]=newColor; } } bitmap.setPixels(pixels,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,width,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,width,height); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">long</span> endTime = System.currentTimeMillis(); Log.e(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"tag"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"this is old used time "</span>+(endTime-startTime)+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ms"</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> bitmap; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li></ul>
代码都很简单,而且相同部分很多,重点就在于颜色分量的处理,这个处理是根据原理 来的。
- 模糊效果
原理:周边像素平均值
高斯模糊原理:周边像素的加权平均值
1.通过RenderScript来实现模糊
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">public Bitmap RSblur(Bitmap src){ long startTime = System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.currentTimeMillis</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> Bitmap bitmap = Bitmap<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.createBitmap</span>(src<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getWidth</span>(),src<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getHeight</span>(), Bitmap<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Config</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ARGB</span>_8888)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> RenderScript renderScript = RenderScript<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.create</span>(MyApplication<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.app</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> ScriptIntrinsicBlur scriptIntrinsicBlur = ScriptIntrinsicBlur<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.create</span>(renderScript, Element<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.U</span>8_4(renderScript))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> Allocation allIn = Allocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.createFromBitmap</span>(renderScript, src)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> Allocation allOut = Allocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.createFromBitmap</span>(renderScript,bitmap)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> // 控制模糊程度 scriptIntrinsicBlur<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setRadius</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2.</span>f)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> scriptIntrinsicBlur<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setInput</span>(allIn)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> scriptIntrinsicBlur<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.forEach</span>(allOut)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> allOut<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.copyTo</span>(bitmap)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> src<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.recycle</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> renderScript<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.destroy</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> long endTime = System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.currentTimeMillis</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> Log<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.e</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"tag"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"this is RenderScript blur use time "</span>+(endTime-startTime)+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ms"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> return bitmap<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li></ul>
2.操作像素RGB
这里我用了网上的一个FastBlur类,这个类实现了模糊并且做了优化。
- 锐化效果
原理:分别获取当前像素点和八个周围像素点的RGB值,先求出当前像素点的RGB值与八个像素点RGB值的和的平均数,再乘以相应的系数,然后在与当前像素点之和
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">pixColor = pixels[(i + n) * width + k + m]; pixR = Color.red(pixColor); pixG = Color.green(pixColor); pixB = Color.blue(pixColor); newR = newR + (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>) (pixR * laplacian[idx] * alpha); newG = newG + (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>) (pixG * laplacian[idx] * alpha); newB = newB + (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>) (pixB * laplacian[idx] * alpha); idx++;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>
这里利用拉普拉斯矩阵。
- 浮雕效果
原理:前一像素分量值-当前像素分量值+127
核心代码:
<code class="hljs rib has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">oldColor=oldPixels[i-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>]; oldPixR = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Color</span>.red(oldColor); oldPixG = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Color</span>.green(oldColor); oldPixB = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Color</span>.blue(oldColor); newColor = newPixels[i]; newPixR = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Color</span>.red(newColor); newPixG = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Color</span>.green(newColor); newPixB = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Color</span>.blue(newColor); //计算 newPixR = (oldPixR-newPixR +<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">127</span>)><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>?<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>:(oldPixR-newPixR +<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">127</span>) ; newPixG = (oldPixG -newPixG+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">127</span>)><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>?<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>:(oldPixG -newPixG+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">127</span>) ; newPixB = (oldPixB-newPixB+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">127</span>)><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>?<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>:(oldPixB-newPixB+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">127</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul>
- 底片效果
原理:取当前RGB分量与255差值作为最终分量值
代码:
<code class="hljs rsl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span> = piexls[i]; pixR = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span> - Color.red(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>); pixG = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span> - Color.green(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>); pixB = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span> - Color.blue(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
- 光照效果
原理,以指定点为中心,置顶光照半径,计算点到光照中心的距离,根据距离增加光照值
核心代码:
<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span> = pixels[j<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*width</span>+i]; pixR= Color.red(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>); pixG= Color.green(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>); pixB = Color.blue(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> distance = (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>) (Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">pow</span>((centerY-j),<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)+Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">pow</span>((centerX-i),<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (distance<radius<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*radius</span>); { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//按照距离大小计算增强的光照值</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> result = (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>) (strength<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*(</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.0</span>-Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sqrt</span>(distance)/radius)); pixR = pixR+result; pixG = pixG+result; pixB = pixB+result; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//做左右限制</span> pixR = Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">min</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>,Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,pixR)); pixG = Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">min</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>,Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,pixG)); pixB = Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">min</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>,Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,pixB));</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul>
- 熔铸效果
原理:当前RGB分量*128然后对(其他2个分量之和+1)取整,并保留分量的修改
核心代码:
<code class="hljs rsl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>=pixels[i]; pixR = Color.red(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>); pixG = Color.green(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>); pixB = Color.blue(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//R 分量</span> pixR = pixR * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">128</span>/(pixG+pixB+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>); pixR = Math.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">min</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>,Math.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,pixR)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//G 分量</span> pixG = pixG*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">128</span>/(pixB+pixR+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>); pixG =Math.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">min</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>,Math.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,pixG)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//B 分量</span> pixB = pixB*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">128</span>/(pixR+pixG+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>); pixB = Math.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">min</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>,Math.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,pixB));</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>
- 冰冻效果
原理:分量值 = (当前分量值-其他2个分量值的绝对值)*3/2;
核心代码:
<code class="hljs rsl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span> = pixels[i]; pixR = Color.red(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>); pixG = Color.green(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>); pixB = Color.blue(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>); pixR = Math.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">abs</span>((pixR - pixG - pixB) * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>); pixR = Math.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">min</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>, pixR); pixG = Math.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">abs</span>((pixG - pixR - pixB) * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>); pixG = Math.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">min</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>, pixG); pixB = Math.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">abs</span>((pixB - pixR - pixG) * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>); pixB = Math.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">min</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>,pixB);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>
- 雾化效果
原理:在图像中引入一定的随机值,打乱图像中的像素值
核心代码:
<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">k = random.nextInt(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">123456</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> dx = i+k<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%8</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> dy = j+k<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%8</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (dx>=width){ dx=width-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (dy>=height){ dy=height-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">pos</span> = dy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*width</span> +dx; pos1 = j<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*width</span>+i; pixels[pos1]=pixels[<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">pos</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>
- 积木效果
原理:取3分量平均值,若大于128,则取255,否则取0 作为新分量值
核心代码:
<code class="hljs axapta has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">color = pixels[i]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sum</span> = (Color.red(color)+Color.blue(color)+Color.green(color))/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sum</span>>=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">128</span>){ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sum</span> = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>; }<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>{ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sum</span> = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; } pixels[i]=Color.argb(Color.alpha(color),<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sum</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sum</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sum</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>
- 连环画效果
原理:
R=|g-b+g+r|*r/256
G=|b-g+b+r|*r/256
B=|b-g+b+r|*g/256
核心代码
<code class="hljs rsl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>=pixels[i]; pixR= Color.red(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>); pixG = Color.green(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>); pixB = Color.blue(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//r</span> pixR = Math.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">abs</span>(pixG-pixB+pixG+pixR) * pixR /<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">256</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (pixR><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>){ pixR = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>; } pixG =Math.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">abs</span>(pixB-pixG+pixB+pixR) *pixR/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">256</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (pixG><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>){ pixG = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//B=|b-g+b+r|*g/256</span> pixB = Math.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">abs</span>(pixB-pixG+pixB+pixR) * pixG/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">256</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (pixB><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">256</span>){ pixB=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul>
- 边缘高亮效果(霓虹处理)
原理:计算原像素的RGB分量与相同行(i+1)以及相同列(j+1)相邻像素的梯度,就是差的平方和的平方根,然后将梯度值作为处理后的像素
核心代码:
<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>=pixels[j<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*width</span>+i]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取i+1像素</span> color_right = pixels[j<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*width</span>+i+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取j+1像素</span> color_bottom = pixels[(j+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*width</span>+i]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//计算R分量</span> pixR = (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>) (Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">pow</span>((Color.red(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>)-Color.red(color_right)),<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>) +Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">pow</span>((Color.red(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>)-Color.red(color_bottom)),<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)); pixR = ((<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>) (Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sqrt</span>(pixR)<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*2</span>)); pixR = Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">min</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>,Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,pixR)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//计算G 分量</span> pixG = (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>) (Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">pow</span>((Color.green(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>)-Color.green(color_right)),<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>) +Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">pow</span>((Color.green(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>)-Color.green(color_bottom)),<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)); pixG = ((<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>) (Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sqrt</span>(pixG)<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*2</span>)); pixG = Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">min</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>,Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,pixG)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//计算B分量</span> pixB = (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>) (Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">pow</span>((Color.blue(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>)-Color.blue(color_right)),<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>) +Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">pow</span>((Color.blue(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>)-Color.blue(color_bottom)),<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)); pixB = ((<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>) (Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sqrt</span>(pixB)<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*2</span>)); pixB = Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">min</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>,Math.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,pixB)); pixels[j<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*width</span>+i] = Color.argb(Color.alpha(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">color</span>),pixR,pixG,pixB);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li></ul><div class="save_code tracking-ad" data-mod="popu_249" style="box-sizing: border-box; position: absolute; height: 60px; right: 30px; top: 5px; color: rgb(255, 255, 255); cursor: pointer; z-index: 2;"><a target=_blank target="_blank" style="color: rgb(202, 0, 0); box-sizing: border-box;"><img src="http://static.blog.csdn.net/images/save_snippets.png" style="border: none; box-sizing: border-box; max-width: 100%;" alt="" /></a></div>
到这里就结束了,当然,图片处理特效还有很多,我这里仅仅是一小部分。在这里推荐一个库:ImageFilter