Note that there are some explanatory texts on larger screens.

plurals
  1. POCutting out predefined piece of a photo from camera
    primarykey
    data
    text
    <p>For an application I am developing, I let the user specify dimensions of an object they want to capture on camera (for example 30" x 40"). The next thing I want to do, is show the camera feed with a cameraOverlayView on top of it, showing nothing but a stroked transparent square which has the right ratio to capture that object.</p> <p><strong>So I tried 2 things to get this to work:</strong></p> <p>Use a UIViewController which uses the <code>AVCaptureVideoPreviewLayer</code> to display a view with the live video feed. On top of that feed I display a transparent view, which draws a square with the right dimensions (using the ratio the user specified).</p> <p><strong>and</strong></p> <p>In another attempt I created a UIViewController, containing a button which pops up the <code>UIImagePickerController</code>. Using this controller, I also created a view which I attach to the picker using the <code>cameraOverlayView</code> property.</p> <p>The main problem I am having with both these methods is that the image that is actually captured is always larger then what I see on screen, but I am not entirely sure how to cut out THAT piece of the image, after the picture has been taken.</p> <p>So for example: My UIImagePickerController is shown, I put an overlay over that showing a square that is 300 x 400px large. The user uses this square to take a picture of their object and centers their object inside this square.</p> <p>The picture is taken, but instead of a picture that is 320x480 (or 640x960) I get a result that is 3500x2400 (or something like that. It's a completely different ratio then the screen ratio (of course).</p> <p>How do I then make sure I cut out the right part of the image.</p> <p>The code that actually calculates the size of the square that should be shown (and should be used to determine what piece of the picture should be cut):</p> <pre><code>+ (CGRect) getFrameRect:(CGRect) rect forSize:(CGSize) frameSize { if (CGSizeEqualToSize(frameSize, CGSizeZero)) return CGRectZero; float maxWidth = rect.size.width - 20; float maxHeight = rect.size.height - 20; float ratioX = maxWidth / frameSize.width; float ratioY = maxHeight / frameSize.height; float ratio = MIN(ratioX, ratioY); float newWidth = frameSize.width * ratio; float newHeight = frameSize.height * ratio; float x = (rect.size.width - newWidth) / 2; float y = (rect.size.height - newHeight) / 2; return CGRectMake(x, y, newWidth, newHeight); } </code></pre> <p>This determines the largest square that can be created with the ratio specified in the frameSize parameter, with the dimensions where the square should be drawn in supplied in the rect parameter.</p> <p>Some solutions come to mind, but I am not sure that is doable.</p> <ul> <li>Shrink the photo down to screen width or height (whatever comes first), take the center of the picture and that is the same center that is shown when taking the picture? Not sure this is going to work, I tried this a bit, but that failed.</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload