Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does capturing images with AVFoundation give me 480x640 images when the preset is 640x480?
    primarykey
    data
    text
    <p>I have some pretty basic code to capture a still image using AVFoundation. </p> <pre><code>AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self backFacingCamera] error:nil]; AVCaptureStillImageOutput *newStillImageOutput = [[AVCaptureStillImageOutput alloc] init]; NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil]; [newStillImageOutput setOutputSettings:outputSettings]; [outputSettings release]; AVCaptureSession *newCaptureSession = [[AVCaptureSession alloc] init]; [newCaptureSession beginConfiguration]; newCaptureSession.sessionPreset = AVCaptureSessionPreset640x480; [newCaptureSession commitConfiguration]; if ([newCaptureSession canAddInput:newVideoInput]) { [newCaptureSession addInput:newVideoInput]; } if ([newCaptureSession canAddOutput:newStillImageOutput]) { [newCaptureSession addOutput:newStillImageOutput]; } self.stillImageOutput = newStillImageOutput; self.videoInput = newVideoInput; self.captureSession = newCaptureSession; [newStillImageOutput release]; [newVideoInput release]; [newCaptureSession release]; </code></pre> <p>My method that captures the still image is also pretty simple and prints out the orientation which is AVCaptureVideoOrientationPortrait:</p> <pre><code>- (void) captureStillImage { AVCaptureConnection *stillImageConnection = [AVCamUtilities connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self stillImageOutput] connections]]; if ([stillImageConnection isVideoOrientationSupported]){ NSLog(@"isVideoOrientationSupported - orientation = %d", orientation); [stillImageConnection setVideoOrientation:orientation]; } [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:stillImageConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { ALAssetsLibraryWriteImageCompletionBlock completionBlock = ^(NSURL *assetURL, NSError *error) { if (error) { // HANDLE } }; if (imageDataSampleBuffer != NULL) { CFDictionaryRef exifAttachments = CMGetAttachment(imageDataSampleBuffer, kCGImagePropertyExifDictionary, NULL); if (exifAttachments) { NSLog(@"attachements: %@", exifAttachments); } else { NSLog(@"no attachments"); } self.stillImageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; self.stillImage = [UIImage imageWithData:self.stillImageData]; UIImageWriteToSavedPhotosAlbum(self.stillImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); } else completionBlock(nil, error); }]; } </code></pre> <p>So the device understands it's in portrait mode as it should be, the exif attachements show me:</p> <pre><code>PixelXDimension = 640; PixelYDimension = 480; </code></pre> <p>so it seems to know that we're in 640x480 and that means WxH (obviously...)</p> <p>However when I email the photo to myself from Apples Photos app, I get a 480x640 image if I check the properties in Preview. This didn't make any sense to me until I dug further into image properties to find out that the image orientation is set to "6 (Rotated 90 degrees CCW)" I'm sure CCW is counter clockwise</p> <p>So looking at the image in a browser: <a href="http://tonyamoyal.com/stuff/things_that_make_you_go_hmm/photo.JPG">http://tonyamoyal.com/stuff/things_that_make_you_go_hmm/photo.JPG</a> We see a the image rotated 90 degrees CCW and it is 640x480.</p> <p>I'm really confused about this behavior. When I take a 640x480 still image using AVFoundation, I would expect the default to have no rotated orientation. I expect a 640x480 image oriented exactly as my eye sees the image in the preview layer. Can someone explain why this is happening and how to configure the capture so that when I save my image to the server to later display in a web view, it is not rotated 90 degrees CCW? </p>
    singulars
    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