title: Cropping a Perspective Transformation of Image on Android
body: <p>I'm trying to do a perspective transform on a bitmap that I capture via the camera. The user adjusts a bounding quad (as depicted by the white box) around a rectangular object. I then attempt to transform this to a rectangular image using the code below:</p>
  
  <pre><code>public static Bitmap perspectiveTransformation(Bitmap bitmap,BoundingQuad boundingQuad)
  {
      Matrix matrix = new Matrix();
      float[] dst = new float[] {
              0,
              0,
              bitmap.getWidth(),
              0,
              bitmap.getWidth(),
              bitmap.getHeight(),
              0,
              bitmap.getHeight()
      };
      float[] src = new float[] {
              boundingQuad.topLeft.x,
              boundingQuad.topLeft.y,
              boundingQuad.topRight.x,
              boundingQuad.topRight.y,
              boundingQuad.bottomRight.x,
              boundingQuad.bottomRight.y,
              boundingQuad.bottomLeft.x,
              boundingQuad.bottomLeft.y
      };
      matrix.setPolyToPoly(src, 0, dst, 0, src.length &gt;&gt; 1);
      return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
  }
  </code></pre>
  
  <p>However my resulting image contains image data that is outside the boundary of my quad. This would be acceptable if I could figure out what the coordinates of the quad are after transformation so I could crop the result but I have absolutely no idea how to do that.</p>
  
  <p>Any help would be greatly appreciated in either finding the coordinates of the quad after transformation or ideally finding a way to prevent this situation from occurring in the first place.</p>
  
  <p>Input:</p>
  
  <p><a href="http://i.stack.imgur.com/33RfN.png">http://i.stack.imgur.com/33RfN.png</a></p>
  
  <p>Output:</p>
  
  <p><a href="http://i.stack.imgur.com/zWFvA.png">http://i.stack.imgur.com/zWFvA.png</a></p>