title: Crop a surface view in Android
body: <p>Here's what I need:</p>
  
  <p>I have a Surface view that has a square (image view) on top of it. I need to capture an image, and crop out the area that was visible only within the square.</p>
  
  <p>This code gives me decent results but specific only to some devices:</p>
  
  <pre><code>int width=(int)(bitmap.getWidth()*60/100);
  int height=(bitmap.getHeight()*100/100); //dont change
  bitmap=Bitmap.createBitmap(bitmap,150,0, width-55, height);
  </code></pre>
  
  <p>Is there any way I could generalize this code? Is there any other way to get what I need?</p>
  
  <p><strong>EDIT:</strong> This is how I got it to work-</p>
  
  <ol>
  <li>Save the image from the surface view as a bitmap (This is very simple. There are many examples available on the internet that show how to do that)</li>
  <li><p>Use this code in a function, and call it after the image is clicked</p>
  
  <pre><code>//bitmap is the object where the image is stored
  int width = bitmap.getWidth();
  int height = bitmap.getHeight();
  
  int left;
  if(width &gt; height){
      left = (width - height)/2;
  }
  else {
      left = (height - width)/2;
  }
  
  bitmap=Bitmap.createBitmap(bitmap,left,0, height, height);
  </code></pre></li>
  </ol>