【发布时间】:2011-12-12 07:56:00
【问题描述】:
我正在使用 AVFoundation 类在我的应用中实现自定义摄像头。我只捕捉静止图像,而不是视频。我一切正常,但被某些事情难住了。我在捕获静止图像时会考虑设备方向,并适当地设置视频连接的 videoOrientation。一个代码sn-p:
// set the videoOrientation based on the device orientation to
// ensure the pic is right side up for all orientations
AVCaptureVideoOrientation videoOrientation;
switch ([UIDevice currentDevice].orientation) {
case UIDeviceOrientationLandscapeLeft:
// Not clear why but the landscape orientations are reversed
// if I use AVCaptureVideoOrientationLandscapeLeft here the pic ends up upside down
videoOrientation = AVCaptureVideoOrientationLandscapeRight;
break;
case UIDeviceOrientationLandscapeRight:
// Not clear why but the landscape orientations are reversed
// if I use AVCaptureVideoOrientationLandscapeRight here the pic ends up upside down
videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
break;
case UIDeviceOrientationPortraitUpsideDown:
videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
break;
default:
videoOrientation = AVCaptureVideoOrientationPortrait;
break;
}
videoConnection.videoOrientation = videoOrientation;
请注意我在景观案例中的 cmets。我必须反转方向映射,否则生成的图像是颠倒的。我使用以下代码捕获并保存图像:
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
self.stillImage = [UIImage imageWithData:imageData];
// notify observers (image gets saved to the camera roll)
[[NSNotificationCenter defaultCenter] postNotificationName:CaptureSessionManagerDidCaptureStillImageNotification object:self];
self.stillImage = nil;
}];
没有其他图像处理或操作。
我的应用可以使用上面的代码。我只是想了解为什么横向方向的方向常数必须反转。谢谢!
【问题讨论】:
-
当你真正想使用 UIInterface 方向时,不要使用 UIDevice 方向。
-
在这种情况下您不能使用
UIInterface方向。拍摄图像时的设备方向始终正确。捕获数据后stillImage的图像方向不正确。我能够使用 UIDeviceOrientation 解决问题,并在旋转时转换为适当的 UIImageOrientation。当时,没有人在这里插话,所以我没有发布代码。如果我有时间,我会添加代码。 -
@XJones,请用确切的解决方案更新您的答案。
标签: iphone ios avcapturesession image-capture avcapturedevice