6

我正在使用从相机捕获图像captureStillImageAsynchronouslyFromConnection

图像被颠倒保存。我尝试使用保存图像

UIImage *flippedImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUp]

但是这个方向只存在于元数据中,当我从文件中复制数据来处理它时,处理过的图像是颠倒的。(快速浏览显示原件是直立的,但处理/过滤的图像是颠倒的)。

设置视频捕获方向的正确方法是什么?

我在视频预览层上设置了方向,但这不会影响AVCaptureStillImageOutput对象缓冲区中捕获的数据。

我阅读了AVCaptureStillImageOutput标题,但似乎没有解释如何强制相机预览方向到图像缓冲区。

例如- (AVMetadataObject *)transformedMetadataObjectForMetadataObject:(AVMetadataObject *)metadataObject connection:(AVCaptureConnection *)connection NS_AVAILABLE_IOS(6_0);

谢谢

4

2 回答 2

4

“问题”是图像方向,iOS 相机仅返回一个方向的图像,除非您设置了-videoOrientation转换已拍摄图像的属性。所以可能会有不同的问题:

  1. videoOrientation未正确设置
  2. 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>

首先它根据预览视图设置方向,然后拍摄,然后以正确的方向将其保存在资源库中

于 2014-03-16T12:38:54.943 回答
1

正如您所提到的,这样做您只是在更改元数据。您需要使用此方法来实际翻转图像。

尝试这个:

- (UIImage *)flipImage:(UIImage *)image
{
    UIGraphicsBeginImageContext(image.size);
    CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(0, 0, image.size.width, image.size.height),image.CGImage);
    UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return i;
}

更多信息在这里

于 2015-04-16T16:13:07.293 回答