title: Can't use fisheye lens effect on my image in Java
body: <p>I want to use <a href="https://www.google.com/search?q=fisheye%20lens&amp;rlz=1C1ARAB_enSK458SK458&amp;source=lnms&amp;tbm=isch&amp;sa=X&amp;ei=_uG5UtfWKonYswbW_4DYBg&amp;ved=0CAcQ_AUoAQ&amp;biw=1920&amp;bih=922" rel="nofollow noreferrer">fisheye lens algorithm</a> in Java, but I can't implement a main method.</p>
  
  <p>At the bottom of <a href="https://stackoverflow.com/questions/1927145/how-to-simulate-fisheye-lens-effect-by-opencv">this site</a>, I found java code, that implements fisheye algorithm, but there wasn't any main() method. I've tried to implement a main() method by my own, but so far I just can display image, but I can't use the barrel() method on that image which should make fisheye effect on my image.</p>
  
  <p>Could you please correct my main method, or implement a new one, that loads some image(bufferedImage) and then use barrel() method on that image?</p>
  
  <p>My code so far is:</p>
  
  <pre><code>import java.awt.FlowLayout;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.IOException;
  import javax.imageio.ImageIO;
  import javax.swing.ImageIcon;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  
  public class DisplayImage {
  float xscale;
  float yscale;
  float xshift;
  float yshift;
  int [] s;                             
  
  
  public BufferedImage barrel (BufferedImage input, float k){
  
      float centerX=input.getWidth()/2; //center of distortion
      float centerY=input.getHeight()/2;
  
      int width = input.getWidth(); //image bounds
      int height = input.getHeight();
  
      BufferedImage dst = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB); //output pic
  
        xshift = calc_shift(0,centerX-1,centerX,k);
        float newcenterX = width-centerX;
        float xshift_2 = calc_shift(0,newcenterX-1,newcenterX,k);
  
        yshift = calc_shift(0,centerY-1,centerY,k);
        float newcenterY = height-centerY;
        float yshift_2 = calc_shift(0,newcenterY-1,newcenterY,k);
  
        xscale = (width-xshift-xshift_2)/width;
        yscale = (height-yshift-yshift_2)/height;
  
        for(int j=0;j&lt;dst.getHeight();j++){
            for(int i=0;i&lt;dst.getWidth();i++){
              float x = getRadialX((float)i,(float)j,centerX,centerY,k);
              float y = getRadialY((float)i,(float)j,centerX,centerY,k);
              sampleImage(input,x,y);
              int color = ((s[1]&amp;0x0ff)&lt;&lt;16)|((s[2]&amp;0x0ff)&lt;&lt;8)|(s[3]&amp;0x0ff);
  //            System.out.print(i+" "+j+" \\");
  
              dst.setRGB(i, j, color);
  
            }
          }
      return dst;
  }
  
  void sampleImage(BufferedImage arr, float idx0, float idx1)
  {
      s = new int [4];
    if(idx0&lt;0 || idx1&lt;0 || idx0&gt;(arr.getHeight()-1) || idx1&gt;(arr.getWidth()-1)){
      s[0]=0;
      s[1]=0;
      s[2]=0;
      s[3]=0;
      return;
    }
  
    float idx0_fl=(float) Math.floor(idx0);
    float idx0_cl=(float) Math.ceil(idx0);
    float idx1_fl=(float) Math.floor(idx1);
    float idx1_cl=(float) Math.ceil(idx1);
  
    int [] s1 = getARGB(arr,(int)idx0_fl,(int)idx1_fl);
    int [] s2 = getARGB(arr,(int)idx0_fl,(int)idx1_cl);
    int [] s3 = getARGB(arr,(int)idx0_cl,(int)idx1_cl);
    int [] s4 = getARGB(arr,(int)idx0_cl,(int)idx1_fl);
  
    float x = idx0 - idx0_fl;
    float y = idx1 - idx1_fl;
  
    s[0]= (int) (s1[0]*(1-x)*(1-y) + s2[0]*(1-x)*y + s3[0]*x*y + s4[0]*x*(1-y));
    s[1]= (int) (s1[1]*(1-x)*(1-y) + s2[1]*(1-x)*y + s3[1]*x*y + s4[1]*x*(1-y));
    s[2]= (int) (s1[2]*(1-x)*(1-y) + s2[2]*(1-x)*y + s3[2]*x*y + s4[2]*x*(1-y));
    s[3]= (int) (s1[3]*(1-x)*(1-y) + s2[3]*(1-x)*y + s3[3]*x*y + s4[3]*x*(1-y));
  }
  
  int [] getARGB(BufferedImage buf,int x, int y){
      int rgb = buf.getRGB(x, y); // Returns by default ARGB.
      int [] scalar = new int[4];
      scalar[0] = (rgb &gt;&gt;&gt; 24) &amp; 0xFF;
      scalar[1] = (rgb &gt;&gt;&gt; 16) &amp; 0xFF;
      scalar[2] = (rgb &gt;&gt;&gt; 8) &amp; 0xFF;
      scalar[3] = (rgb &gt;&gt;&gt; 0) &amp; 0xFF;
      return scalar;
  }
  
  float getRadialX(float x,float y,float cx,float cy,float k){
    x = (x*xscale+xshift);
    y = (y*yscale+yshift);
    float res = x+((x-cx)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
    return res;
  }
  
  float getRadialY(float x,float y,float cx,float cy,float k){
    x = (x*xscale+xshift);
    y = (y*yscale+yshift);
    float res = y+((y-cy)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
    return res;
  }
  
  float thresh = 1;
  
  float calc_shift(float x1,float x2,float cx,float k){
    float x3 = (float)(x1+(x2-x1)*0.5);
    float res1 = x1+((x1-cx)*k*((x1-cx)*(x1-cx)));
    float res3 = x3+((x3-cx)*k*((x3-cx)*(x3-cx)));
  
    if(res1&gt;-thresh &amp;&amp; res1 &lt; thresh)
      return x1;
    if(res3&lt;0){
      return calc_shift(x3,x2,cx,k);
    }
    else{
      return calc_shift(x1,x3,cx,k);
    }
  }
  
  
  
  public static void main(String avg[]) throws IOException
  {
      BufferedImage img=ImageIO.read(new File("image.jpg"));       
      ImageIcon icon=new ImageIcon(img);   
      JFrame frame=new JFrame();
      frame.setLayout(new FlowLayout());
      frame.setSize(625,648);
      JLabel lbl=new JLabel();
      lbl.setIcon(icon);
      frame.add(lbl);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Code is working, but it only display image. I want to use barrel() method on my image, which will do the fisheye effect. Because I am newbie to java, I can't do that, please help.</p>