【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;
}
在这个方法中我是选定图片中间宽高一般的区域进行放大。
效果如下:
效果图 原图
代码如下:
//放大镜
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;
}
在这个方法中我是选定图片中间宽高一般的区域进行放大。
效果如下:
效果图 原图
相关文章推荐
- Android高手进阶教程(二十二)之---Android中几种图像特效处理的集锦!!
- Android高手进阶教程(二十二)之---Android中几种图像特效处理的集锦!!
- Android实战经验之图像处理及特效处理的集锦(总结版)
- Android中几种图像特效处理的集锦
- Android 中图像特效处理方法小结
- Android高手进阶教程(十九)---Android中几种图像特效处理的集锦!!
- Android中几种图像特效处理方法
- Android中几种图像特效处理
- Android中几种图像特效处理的小技巧,比如圆角,倒影,还有就是图片缩放,Drawable转化为Bitmap,Bitmap转化为Drawable等等
- Android实战经验之图像处理及特效处理的集锦(总结版)
- Android中几种图像特效处理的集锦!!!
- Android高手进阶教程(二十二)之---Android中几种图像特效处理的集锦!!
- Android高手进阶教程(二十二)之---Android中几种图像特效处理的集锦!!
- Android中几种图像特效处理
- 转载 Android中几种图像特效处理的集锦!!
- Android高手进阶教程(二十二)之---Android中几种图像特效处理的集锦!!
- Android高手进阶教程(二十二)之---Android中几种图像特效处理的集锦!
- Android高手进阶教程之---Android中几种图像特效处理的集锦!!
- Android中几种图像特效处理方法小结
- Android中几种图像特效处理的集锦