Androidk开发之图像局部放大算法

来源:互联网 发布:d3.js圆形动态加载 编辑:程序博客网 时间:2024/05/01 04:39

Androidk开发之图像局部放大算法

前段时间有需求要做一个类似美图秀秀的软件,结果卡在增大眼睛这个功能了,到处搜索,最多只能见到各种论文,最气的是有个家伙声称自己已经实现了该方法,却不愿意贡献出自己的源码,还说他那里不欢迎只会伸手的人,这种没有开源精神的人,迟早会被淘汰!查看了很多论文,终于写出了该方法。


直接贴出代码,如下

public static Bitmap bigEyes(Bitmap bitmap, int PointX, int PointY,            int Radius, int Strength) {        int width = bitmap.getWidth();        int height = bitmap.getHeight();        // int[] pixNew = new int[width * height];        // int[] pixOld = new int[width * height];        int Left = PointX - Radius < 0 ? 0 : PointX - Radius; // 计算边界值        int Top = PointY - Radius < 0 ? 0 : PointY - Radius;        int Bottom = PointY + Radius >= height ? height - 1 : PointY + Radius;        int Right = PointX + Radius >= width ? width - 1 : PointX + Radius;        Bitmap newBitmap = bitmap.copy(Bitmap.Config.RGB_565, true);        // bitmap.getPixels(pixNew, Top * width + Left, width, Left, Top,        // Radius * 2, Radius * 2);        // bitmap.getPixels(pixOld, Top * width + Left, width, Left, Top,        // Radius * 2, Radius * 2);        int PowRadius = Radius * Radius;        for (int Y = Top; Y <= Bottom; Y++) {            int offSetY = Y - PointY;            for (int X = Left; X <= Right; X++) {                int offSetX = X - PointX;                double XY = offSetX * offSetX + offSetY * offSetY;                if (XY <= PowRadius) {                    double ScaleFactor = 1 - XY / PowRadius;                    ScaleFactor = 1 - (double) Strength / 100 * ScaleFactor; // 按照这种关系计算取样点的位置                    int PosX = (int) (offSetX * ScaleFactor + PointX);                    int PosY = (int) (offSetY * ScaleFactor + PointY);                    if (PosX < 0) { // 放置越界                        PosX = 0;                    } else if (PosX >= width) {                        PosX = width - 1;                    }                    if (PosY < 0) {                        PosY = 0;                    } else if (PosY >= height) {                        PosY = height - 1;                    }                    // int Speed = Y * width + X;                    // int Index = PosY * width + PosX;                    newBitmap.setPixel(X, Y, bitmap.getPixel(PosX, PosY));                    // pixNew[Speed] = pixOld[Index];                }            }        }        // bitmap.setPixels(pixNew, Top * width + Left, width, Left, Top,        // Radius * 2, Radius * 2);        return newBitmap;    }

应该很明了了,就不做注释了,这还要注释就是真正的伸手党了。

0 0
原创粉丝点击