android Google 纸板桶形失真的库

Library for android for Google cardboard barrel distortion

我正在尝试为 Android 实现一个 3D 应用程序,该应用程序也应该像查看器一样支持纸板。我看过其中一些图像,它们似乎有某种桶形畸变,以便通过纸板镜头保持正交。

所以我正在寻找专门用于 Java/Android 的算法或库来帮助我实现这一目标。

我找到了这个实现:http://www.helviojunior.com.br/fotografia/barrel-and-pincushion-distortion/

如果有这样的东西就好了,因为它拥有我需要的一切。不幸的是,它是针对 C# 的,它有一些我无法轻易转换为更通用代码的特定代码。

那么这里有一个更简单的Java实现:http://popscan.blogspot.de/2012/04/fisheye-lens-equation-simple-fisheye.html

我已经改成:

public static Bitmap fisheye(Bitmap srcimage) {
/*
 *    Fish eye effect
 *    tejopa, 2012-04-29
 *    http://popscan.blogspot.com
 *    http://www.eemeli.de
 */

    // get image pixels
    double w = srcimage.getWidth();
    double h = srcimage.getHeight();
    int[] srcpixels = new int[(int)(w*h)];
    srcimage.getPixels(srcpixels, 0, (int)w, 0, 0, (int)w, (int)h);

    Bitmap resultimage = srcimage.copy(srcimage.getConfig(), true);

    // create the result data
    int[] dstpixels = new int[(int)(w*h)];
    // for each row
    for (int y=0;y<h;y++) {
        // normalize y coordinate to -1 ... 1
        double ny = ((2*y)/h)-1;
        // pre calculate ny*ny
        double ny2 = ny*ny;
        // for each column
        for (int x=0;x<w;x++) {
            // preset to black
            dstpixels[(int)(y*w+x)] = 0;

            // normalize x coordinate to -1 ... 1
            double nx = ((2*x)/w)-1;
            // pre calculate nx*nx
            double nx2 = nx*nx;
            // calculate distance from center (0,0)
            // this will include circle or ellipse shape portion
            // of the image, depending on image dimensions
            // you can experiment with images with different dimensions
            double r = Math.sqrt(nx2+ny2);
            // discard pixels outside from circle!
            if (0.0<=r&&r<=1.0) {
                double nr = Math.sqrt(1.0-r*r);
                // new distance is between 0 ... 1
                nr = (r + (1.0-nr)) / 2.0;
                // discard radius greater than 1.0
                if (nr<=1.0) {
                    // calculate the angle for polar coordinates
                    double theta = Math.atan2(ny,nx);
                    // calculate new x position with new distance in same angle
                    double nxn = nr*Math.cos(theta);
                    // calculate new y position with new distance in same angle
                    double nyn = nr*Math.sin(theta);
                    // map from -1 ... 1 to image coordinates
                    int x2 = (int)(((nxn+1)*w)/2.0);
                    // map from -1 ... 1 to image coordinates
                    int y2 = (int)(((nyn+1)*h)/2.0);
                    // find (x2,y2) position from source pixels

                    int srcpos = (int)(y2*w+x2);
                    // make sure that position stays within arrays
                    if (srcpos>=0 & srcpos < w*h) {
                        // get new pixel (x2,y2) and put it to target array at (x,y)
                        dstpixels[(int)(y*w+x)] = srcpixels[srcpos];
                    }
                }
            }
        }

    }

    resultimage.setPixels(dstpixels, 0, (int)w, 0, 0, (int)w, (int)h);
    //return result pixels
    return resultimage;
}

但是它没有这个镜头factor,所以生成的图像总是完整的circle/ellipse。

如果有机会,您可以向我指出一些有效的 Java 代码或库,或者(甚至更好)帮助我修改此代码以考虑镜头因素(0.0 <= 因素 <= 1.0)?

我设法让它工作了。

底线:我创建了一个比原始位图更大的位图,然后使用

在新位图上绘制了原始位图(并将其居中)
Canvas canvas = new Canvas(newBitmap);
canvas.drawBitmap(originalBitmap, null, new Rect(x, y, r, b), null);

我使用问题中发布的 Java 算法在新位图上创建效果。效果很好。