【问题标题】:AVCaptureVideoOrientation landscape modes result in upside down still imagesAVCaptureVideoOrientation 横向模式导致颠倒的静止图像
【发布时间】:2013-07-25 04:58:26
【问题描述】:

我正在使用 AVCaptureSession 拍照并将图片存储到相册。当我单击按钮时,它会拍摄快照并存储到相册。但是当我使用横向模式时,然后单击它存储横向模式的按钮会导致倒置的静止图像。

代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self setCaptureSession:[[AVCaptureSession alloc] init]];


    [self  addVideoInputFrontCamera:NO]; // set to YES for Front Camera, No for Back camera

    [self  addStillImageOutput];

    [self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]] ];

      [[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];

      CGRect layerRect = [[[self view] layer] bounds];


    [[self previewLayer]setBounds:layerRect];
    [[self  previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))];
    [[[self view] layer] addSublayer:[self  previewLayer]];

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveImageToPhotoAlbum) name:kImageCapturedSuccessfully object:nil];


    [[self captureSession] startRunning];

camera=[UIButton buttonWithType:UIButtonTypeCustom];
[camera setImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
[camera setFrame:CGRectMake(150, 10, 40, 30)];
[camera addTarget:self action:@selector(takephoto:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:camera];

}

拍照按钮:

-(void)takephoto:(id)sender{

[self captureStillImage];

}

- (void)captureStillImage
{
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
        for (AVCaptureInputPort *port in [connection inputPorts]) {
            if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) {
            break;
        }
    }

    NSLog(@"about to request a capture from: %@", [self stillImageOutput]);

    [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
                                                         completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
                                                             CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
                                                             if (exifAttachments) {
                                                                 NSLog(@"attachements: %@", exifAttachments);
                                                             } else {
                                                                 NSLog(@"no attachments");
                                                             }

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


                                                            [self setStillImage:image];



                                                             //  [image release];
                                                             [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];

                                                         }];


}

【问题讨论】:

  • @HeWas:我已经看到了。但我不知道该怎么做?
  • @user2474320... 我将整天离线,但如果你今晚没有有用的答案,我会给你更多细节
  • @HeWas:在纵向模式下它工作正常。在横向模式下它不起作用。一些文档说只使用横向或纵向。但我需要同时使用两种模式
  • @HeWas:我没有得到答案

标签: iphone ios6 avcapturesession image-capture avcapturedevice


【解决方案1】:

您需要根据设备的方向设置 videoConnection 的 videoOrientation 属性。在设置 AVCaptureConnection 后,在 captureStillImage 中执行此操作。

    UIDeviceOrientation deviceOrientation = 
                    [[UIDevice currentDevice] orientation];
    AVCaptureVideoOrientation avcaptureOrientation;
    if ( deviceOrientation == UIDeviceOrientationLandscapeLeft )
            avcaptureOrientation  = AVCaptureVideoOrientationLandscapeRight;

    else if ( deviceOrientation == UIDeviceOrientationLandscapeRight )
            avcaptureOrientation  = AVCaptureVideoOrientationLandscapeLeft;

    [videoConnection setVideoOrientation:avcaptureOrientation];

【讨论】:

  • 现在让读者了解为什么 UI Landscape left 会转换为 AVCapture Landscape right。苹果充满惊喜!
  • 这里有一个提示:设备的每一侧都可以有摄像头。留给后置摄像头留给右摄像头?
猜你喜欢
  • 2011-12-12
  • 2012-03-10
  • 2015-12-26
  • 1970-01-01
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 2022-08-05
  • 1970-01-01
相关资源
最近更新 更多