iPhone AVFoundation摄像头方向

Fog*_*ter 35 iphone camera avfoundation orientation

我一直在试图让我的头发试图让AVFoundation相机以正确的方向(即设备方向)拍摄图片,但我无法让它工作.

我看过教程,我看过WWDC演示文稿,我已经下载了WWDC示例程序,但即便如此也没有.

我的应用程序的代码是......

AVCaptureConnection *videoConnection = [CameraVC connectionWithMediaType:AVMediaTypeVideo fromConnections:[imageCaptureOutput connections]];
if ([videoConnection isVideoOrientationSupported])
{
    [videoConnection setVideoOrientation:[UIApplication sharedApplication].statusBarOrientation];
}

[imageCaptureOutput captureStillImageAsynchronouslyFromConnection:videoConnection
                                                completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
{
    if (imageDataSampleBuffer != NULL)
    {
        //NSLog(@"%d", screenOrientation);

        //CMSetAttachment(imageDataSampleBuffer, kCGImagePropertyOrientation, [NSString stringWithFormat:@"%d", screenOrientation], 0);

        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
        UIImage *image = [[UIImage alloc] initWithData:imageData];

        [self processImage:image];
    }
}];
Run Code Online (Sandbox Code Playgroud)

(processImage使用与WWDC代码相同的writeImage ...方法)

并且WWDC应用程序的代码是......

AVCaptureConnection *videoConnection = [AVCamDemoCaptureManager connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self stillImageOutput] connections]];
        if ([videoConnection isVideoOrientationSupported]) {
            [videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
        }

[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
                                                             completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
                                                                 if (imageDataSampleBuffer != NULL) {
                                                                     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
                                                                     UIImage *image = [[UIImage alloc] initWithData:imageData];                                                                 
                                                                     ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
                                                                     [library writeImageToSavedPhotosAlbum:[image CGImage]
                                                                                               orientation:(ALAssetOrientation)[image imageOrientation]
                                                                                           completionBlock:^(NSURL *assetURL, NSError *error){
                                                                                               if (error) {
                                                                                                   id delegate = [self delegate];
                                                                                                   if ([delegate respondsToSelector:@selector(captureStillImageFailedWithError:)]) {
                                                                                                       [delegate captureStillImageFailedWithError:error];
                                                                                                   }                                                                                               
                                                                                               }
                                                                                           }];
                                                                     [library release];
                                                                     [image release];
                                                                 } else if (error) {
                                                                     id delegate = [self delegate];
                                                                     if ([delegate respondsToSelector:@selector(captureStillImageFailedWithError:)]) {
                                                                         [delegate captureStillImageFailedWithError:error];
                                                                     }
                                                                 }
                                                             }];
Run Code Online (Sandbox Code Playgroud)

在他们的代码的开头,他们将AVOrientation设置为肖像,这似乎很奇怪,但我试图让它检测设备的当前方向并使用它.

正如您所看到的,我已经将[UIApplication sharedApplication] statusBarOrientation设置为尝试获取此功能,但它仍然只能以纵向保存照片.

任何人都可以就我需要做的事情提供任何帮助或建议吗?

谢谢!

奥利弗

Fog*_*ter 45

嗯,这是我永远的水力压裂,但我已经做到了!

我正在寻找的代码是

[UIDevice currentDevice].orientation;
Run Code Online (Sandbox Code Playgroud)

这是如此

AVCaptureConnection *videoConnection = [CameraVC connectionWithMediaType:AVMediaTypeVideo fromConnections:[imageCaptureOutput connections]];
if ([videoConnection isVideoOrientationSupported])
{
    [videoConnection setVideoOrientation:[UIDevice currentDevice].orientation];
}
Run Code Online (Sandbox Code Playgroud)

它完美无缺:D

Woop woop!

  • ...当他们得到的第一个回应是有人去找他们提出问题时回到Stack Overflow?Stack Overflow是人们寻找答案的地方.不要让人们因使用错误的标签而大喊大叫.在评论中发布指向Google的链接是讽刺性的.你不能说它不是,这是非常的定义.你觉得如果我知道我的问题的答案我已经问过了吗?停止对待人们就像他们是愚蠢的.好的,你在这里有一个愚蠢的代表,但不要滥用它或认为它让你有权侮辱别人的问题. (5认同)
  • 好吧,我以为我已经修好了但是我错了.它解决了在手机上显示图像并正确保存到库中的问题,但是当上传到Facebook或在我的计算机上查看时,它们仍然是错误的方式:( (2认同)
  • 你的最终解决方案是什么?我注意到你的帖子对我有些奇怪.UIDeviceOrientation和AVCaptureVideoOrientation不完全相同.LandscapeRight和LandscapeLeft在枚举中交换.我做了很长时间(如果是肖像/设置肖像,如果留下风景/设置景观),我的风景照片总是颠倒了!似乎交换是保持映射正确所必需的,但不确定它是如何工作的. (2认同)

Bil*_*ick 14

这不是更干净吗?

    AVCaptureVideoOrientation newOrientation;
    switch ([[UIDevice currentDevice] orientation]) {
    case UIDeviceOrientationPortrait:
        newOrientation = AVCaptureVideoOrientationPortrait;
        break;
    case UIDeviceOrientationPortraitUpsideDown:
        newOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
        break;
    case UIDeviceOrientationLandscapeLeft:
        newOrientation = AVCaptureVideoOrientationLandscapeRight;
        break;
    case UIDeviceOrientationLandscapeRight:
        newOrientation = AVCaptureVideoOrientationLandscapeLeft;
        break;
    default:
        newOrientation = AVCaptureVideoOrientationPortrait;
    }
    [stillConnection setVideoOrientation: newOrientation];
Run Code Online (Sandbox Code Playgroud)

  • 有可能.这个问题虽然已经三年了.当我问它时,我甚至不记得我写的应用程序:) (4认同)

W D*_*son 11

以下是来自AVCam,我也补充说:

- (void)deviceOrientationDidChange{

    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

    AVCaptureVideoOrientation newOrientation;

    if (deviceOrientation == UIDeviceOrientationPortrait){
        NSLog(@"deviceOrientationDidChange - Portrait");
        newOrientation = AVCaptureVideoOrientationPortrait;
    }
    else if (deviceOrientation == UIDeviceOrientationPortraitUpsideDown){
        NSLog(@"deviceOrientationDidChange - UpsideDown");
        newOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
    }

    // AVCapture and UIDevice have opposite meanings for landscape left and right (AVCapture orientation is the same as UIInterfaceOrientation)
    else if (deviceOrientation == UIDeviceOrientationLandscapeLeft){
        NSLog(@"deviceOrientationDidChange - LandscapeLeft");
        newOrientation = AVCaptureVideoOrientationLandscapeRight;
    }
    else if (deviceOrientation == UIDeviceOrientationLandscapeRight){
        NSLog(@"deviceOrientationDidChange - LandscapeRight");
        newOrientation = AVCaptureVideoOrientationLandscapeLeft;
    }

    else if (deviceOrientation == UIDeviceOrientationUnknown){
        NSLog(@"deviceOrientationDidChange - Unknown ");
        newOrientation = AVCaptureVideoOrientationPortrait;
    }

    else{
        NSLog(@"deviceOrientationDidChange - Face Up or Down");
        newOrientation = AVCaptureVideoOrientationPortrait;
    }

    [self setOrientation:newOrientation];
}
Run Code Online (Sandbox Code Playgroud)

并确保将此添加到您的init方法:

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[notificationCenter addObserver:self
    selector:@selector(deviceOrientationDidChange) 
    name:UIDeviceOrientationDidChangeNotification object:nil];
[self setOrientation:AVCaptureVideoOrientationPortrait];
Run Code Online (Sandbox Code Playgroud)