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

【Android图像处理】图像处理之-放大镜特效

2016-12-29 12:39 309 查看
  放大镜最基本的原理就是选定图片的一定区域进行等比例放大,其余部分不变。

代码如下:

//放大镜
public static Bitmap Magnifier(Bitmap bitmap, int radius){
int w = bitmap.getWidth();
int h = bitmap.getHeight();

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

int[] oldPx = new int[w * h];
int[] newPx = new int[w * h];
int r, g, b, a, color;
float xishu = 2;

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

for (int i = 0; i < w; i++){
for (int j = 0; j < h; j++){
color = oldPx[j * w + i];
r = Color.red(color);
g = Color.green(color);
b = Color.blue(color);
a = Color.alpha(color);

int newR = r;
int newG = g;
int newB = b;
int newA = a;

int centerX = w / 2;
int centerY = h / 2;

int distance = (int) ((centerX - i) * (centerX - i) + (centerY - j) * (centerY - j));
//放大镜区域内的图像放大
if (distance < radius * radius){
// 图像放大效果
int src_x = (int)((float)(i - centerX) / xishu + centerX);
int src_y = (int)((float)(j - centerY) / xishu + centerY);

color = oldPx[src_y * w + src_x];
newR = Color.red(color);
newG = Color.green(color);
newB = Color.blue(color);
newA = Color.alpha(color);
}

//检查像素值是否超出0~255的范围
newR = Math.min(255, Math.max(0, newR));
newG = Math.min(255, Math.max(0, newG));
newB = Math.min(255, Math.max(0, newB));
newA = Math.min(255, Math.max(0, newA));

newPx[j * w + i] = Color.argb(newA, newR, newG, newB);
}
}
result.setPixels(newPx, 0, w, 0, 0, w, h);
return result;
}
在这个方法中我是选定图片中间宽高一般的区域进行放大。
效果如下:

                        效果图                                                  原图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息