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

来源:互联网 发布:淘宝男装优质标签卖家 编辑:程序博客网 时间:2024/05/16 23:38

  放大镜最基本的原理就是选定图片的一定区域进行等比例放大,其余部分不变。

代码如下:

//放大镜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;}
在这个方法中我是选定图片中间宽高一般的区域进行放大。

效果如下:

                        效果图                                                  原图



1 0