我正在使用 captureStillImageAsynchronouslyFromConnection
从相机捕捉图像
图像正在倒置保存。我尝试使用
保存图像UIImage *flippedImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUp]
但是这种方向只存在于元数据中,当我从文件中复制数据来处理它时,处理过的图像是颠倒的。 (快速查看显示原件是直立的,但处理/过滤后的图像是颠倒的)。
设置视频捕获方向的正确方法是什么?
我在视频预览层上设置了方向,但这不会影响 AVCaptureStillImageOutput
对象缓冲区中的捕获数据。
我阅读了 AVCaptureStillImageOutput
header ,但似乎没有解释如何强制相机预览朝向图像缓冲区。
例如- (AVMetadataObject *)transformedMetadataObjectForMetadataObject:(AVMetadataObject *)metadataObject connection:(AVCaptureConnection *)connection NS_AVAILABLE_IOS(6_0);
谢谢
最佳答案
“问题”是图像方向,iOS 相机仅在一个方向返回图像,除非您设置 -videoOrientation
属性来转换已经拍摄的图像。所以可能会有不同的问题:
videoOrientation
未正确设置videoOrientation
未全部设置
videoOrientation 属性仅适用于静态图像和视频捕获,不适用于单个缓冲区。
此片段来自 Apple AVCam示例代码。
[[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] videoOrientation]];
[AVCamViewController setFlashMode:AVCaptureFlashModeAuto forDevice:[[self videoDeviceInput] device]];
[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer)
{
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[[[ALAssetsLibrary alloc] init] writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:nil];
}
}]; <br>
首先根据预览 View 设置方向,然后拍摄,然后以正确的方向将其保存在 Assets 库中
关于ios - AVCaptureStillImageOutput 图像倒置返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22431813/