title: Scaling a rectangle up to match a bitmap
body: <p>I'm trying to crop a bitmap. I first scale the original bitmap down to fit inside a view, then I draw a rectangle on the view that the user can move around and scale to crop the image, much like you can do in instagram when you import a picture from your gallery. The problem I'm having is scaling the rectangle up to match the original bitmap scale to crop from. Here is what's happening:</p>
  
  <p>The middle of the square is what the final result should be</p>
  
  <p><img src="https://i.stack.imgur.com/vMru3.jpg" alt="enter image description here"></p>
  
  <p>And what we actually get back:
  <img src="https://i.stack.imgur.com/X7oKX.jpg" alt="enter image description here"></p>
  
  <p>I know it's just math but I spent hours last night trying to get it figured out.. What am I doing wrong?</p>
  
  <pre><code>Bitmap bmp;
  
  
  float scalefactor = (float)this.mOriginalBitmap.getWidth() / (float)this.mBmpScaledForView.getWidth();
  
  
  float dh = (this.mCropBoxRect.right - this.mCropBoxRect.left) * (scalefactor-1f);
  float dv = (this.mCropBoxRect.bottom - this.mCropBoxRect.top) * (scalefactor-1f);
  
  float l = (float)this.mCropBoxRect.left + dh/2f;
  float r = (float)this.mCropBoxRect.right + dh/2f;
  
  float t = (float)this.mCropBoxRect.top + dv/2f;
  float b = (float)this.mCropBoxRect.bottom + dv/2f;
  
  RectF scaleRec = new RectF(l, t, r, b);
  
  bmp = Bitmap.createBitmap(this.mOriginalBitmap, (int)scaleRec.left, (int)scaleRec.top, (int)scaleRec.width(), (int)scaleRec.height());
  
  bmp = Bitmap.createScaledBitmap(bmp, MyConsts.outputSize, MyConsts.outputSize, false);
  </code></pre>