您的位置:首页 > 移动开发 > Android开发

【Android图像处理】图像处理之-幻觉滤镜

2016-12-28 20:20 218 查看
所谓的幻觉,其实就是将在原有图片的基础上生成几个幻象,看上去就像自己出现了幻觉一样。

具体的代码如下:

//幻觉
public static Bitmap Illusion(Bitmap bitmap){
int w = bitmap.getWidth();
int h = bitmap.getHeight();

Bitmap result = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);

int r, g, b, color;
int r1, g1, b1, color1;

int[] oldPx = new int[w * h];
int[] newPx = new int[w * h];

bitmap.getPixels(oldPx, 0, w, 0, 0, w, h);

for(int x = 0 ; x < (w - 1) ; x++){
for(int y = 0 ; y < (h - 1) ; y++){
//得到当前点的r,g,b值
color = oldPx[x * h + y];
r = Color.red(color);
g = Color.green(color);
b = Color.blue(color);

double _scale = Math.sqrt(w*w + h*h) / 2 ;
double _offset = (int)(_scale / 2) ;
double cx = (x - w / 2.0) / _scale ;
double cy = (y - h / 2.0) / _scale ;
double _amount = Math.PI / 3 ;
double angle = Math.floor ( Math.atan2(cy,cx) / 2.0 / _amount) * 2.0 * _amount + _amount;
double radius = Math.sqrt(cx*cx + cy*cy) ;
int xx = (int)(x - _offset * Math.cos(angle)) ;
int yy = (int)(y - _offset * Math.sin(angle)) ;

xx = Function.FClamp(xx, 0, (int)(w-1));
yy = Function.FClamp(yy, 0, (int)(h-1));

//得到当前点的r,g,b值
color1 = oldPx[xx * h + yy];
r1 = Color.red(color1);
g1 = Color.green(color1);
b1 = Color.blue(color1);

r = Function.FClamp0255 (r + radius * (r1 - r)) ;
g = Function.FClamp0255 (g + radius * (g1 - g)) ;
b = Function.FClamp0255 (b + radius * (b1 - b)) ;

newPx[x * h + y] = Color.rgb(r, g, b);
}
}
result.setPixels(newPx, 0, w, 0, 0, w, h);
return result;
}
其中Function类如下:
public class Function {

public static int FClamp(final int t, final int tLow, final int tHigh)
{
if (t < tHigh)
{
return ((t > tLow) ? t : tLow) ;
}
return tHigh ;
}

public static double FClampDouble(final double t, final double tLow, final double tHigh)
{
if (t < tHigh)
{
return ((t > tLow) ? t : tLow) ;
}
return tHigh ;
}

public static int FClamp0255(final double d)
{
return (int)(FClampDouble(d, 0.0, 255.0) + 0.5) ;
}
}具体的效果如下:
                     效果图                                                          原图



右边的原图是经过压缩后的图,在使用这个算法的时候,最好采用压缩算法,将图片截取成宽高相等的图片,否则达不到预期的效果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息