Tôi có một số mã khá cơ bản để chụp ảnh tĩnh bằng cách sử dụng AVFoundation.Tại sao chụp ảnh bằng AVFoundation cho tôi hình ảnh 480x640 khi cài đặt trước là 640x480?
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];
Phương pháp của tôi để chụp hình ảnh vẫn còn là khá đơn giản và in ra các định hướng đó là AVCaptureVideoOrientationPortrait:
- (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);
}];
}
Vì vậy, các thiết bị hiểu được nó đang ở chế độ chân dung như nó phải được, attachements exif chỉ cho tôi:
PixelXDimension = 640;
PixelYDimension = 480;
nên nó dường như biết rằng chúng ta đang ở 640x480 và đó có nghĩa là RxC (rõ ràng ...)
Tuy nhiên, khi tôi gửi ảnh của mình qua email từ ứng dụng Ảnh của Apple, tôi nhận được hình ảnh 480x640 nếu tôi kiểm tra các thuộc tính trong Xem trước. Điều này không có ý nghĩa gì đối với tôi cho đến khi tôi đào sâu hơn vào các thuộc tính hình ảnh để tìm ra hướng hình ảnh được đặt thành "6 (Xoay 90 độ CCW)" Tôi chắc chắn CCW đang truy cập theo chiều kim đồng hồ
Vì vậy, hãy xem hình ảnh trong trình duyệt: http://tonyamoyal.com/stuff/things_that_make_you_go_hmm/photo.JPG Chúng tôi thấy hình ảnh được xoay 90 độ CCW và nó là 640x480.
Tôi thực sự bối rối về hành vi này. Khi tôi chụp ảnh tĩnh 640x480 bằng AVFoundation, tôi mong đợi mặc định không có hướng xoay. Tôi mong đợi một hình ảnh 640x480 được định hướng chính xác khi mắt tôi nhìn thấy hình ảnh trong lớp xem trước. Ai đó có thể giải thích lý do tại sao điều này xảy ra và làm thế nào để cấu hình chụp để khi tôi lưu hình ảnh của tôi đến máy chủ để hiển thị sau trong một lần xem web, nó không được xoay 90 độ CCW?
Điều này cần phải có nhiều phiếu vượt trội hơn.Cảm ơn bạn đời – Youssef
Giải pháp tuyệt vời !! –
Rất hữu ích, cảm ơn bạn! –