用于Android的图书​​馆为Google纸板桶变形

时间:2016-02-22 08:43:07

标签: android image-processing google-cardboard 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

我已将其更改为:

console.log

但它没有此镜头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; } ,因此生成的图像始终为整圆/椭圆。

你有可能指点我一些工作的Java代码或库或者(甚至可能更好)帮助我修改这段代码以考虑镜头因素(0.0 <=因素&lt; = 1.0)?< / p>

1 个答案:

答案 0 :(得分:0)

我设法让它发挥作用。

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

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

我使用我的问题中发布的Java算法来创建新Bitmap的效果。这很有效。