为什么AVCaptureSession输出错误的方向?

时间:2010-08-24 23:14:00

标签: iphone avcapturesession avcapturedevice

因此,我按照Apple的说明使用AVCaptureSessionhttp://developer.apple.com/iphone/library/qa/qa2010/qa1702.html捕获视频会话。我面临的一个问题是,即使相机/ iPhone设备的方向是垂直的(并且AVCaptureVideoPreviewLayer显示垂直相机流),输出图像似乎处于横向模式。我检查了示例代码imageFromSampleBuffer:内imageBuffer的宽度和高度,分别得到了640px和480px。有谁知道为什么会这样?

谢谢!

11 个答案:

答案 0 :(得分:40)

看一下标题AVCaptureSession.h。枚举名为AVCaptureVideoOrientation的定义定义了各种视频方向。在AVCaptureConnection对象上有一个名为videoOrientation的属性,它是AVCaptureVideoOrientation。您应该可以将其设置为更改视频的方向。您可能需要AVCaptureVideoOrientationLandscapeRightAVCaptureVideoOrientationLandscapeLeft

您可以通过查看会话的输出来查找会话的AVCaptureConnections。输出具有连接属性,该属性是该输出的连接数组。

答案 1 :(得分:20)

我对imageFromSampleBuffer做了一个简单的单行修改,以纠正方向问题(请参阅“我修改过...”代码中的注释)。希望它对某人有所帮助,因为我花了太多时间在这上面。

// Create a UIImage from sample buffer data
- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer  {
    // Get a CMSampleBuffer's Core Video image buffer for the media data
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
    // Lock the base address of the pixel buffer
    CVPixelBufferLockBaseAddress(imageBuffer, 0); 

    // Get the number of bytes per row for the pixel buffer
    void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer); 

    // Get the number of bytes per row for the pixel buffer
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
    // Get the pixel buffer width and height
    size_t width = CVPixelBufferGetWidth(imageBuffer); 
    size_t height = CVPixelBufferGetHeight(imageBuffer); 

    // Create a device-dependent RGB color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

    // Create a bitmap graphics context with the sample buffer data
    CGContextRef context1 = CGBitmapContextCreate(baseAddress, width, height, 8, 
                                                 bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);

    // Create a Quartz image from the pixel data in the bitmap graphics context
    CGImageRef quartzImage = CGBitmapContextCreateImage(context1); 
    // Unlock the pixel buffer
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);

    // Free up the context and color space
    CGContextRelease(context1); 
    CGColorSpaceRelease(colorSpace);

    // Create an image object from the Quartz image
    //I modified this line: [UIImage imageWithCGImage:quartzImage]; to the following to correct the orientation:
    UIImage *image =  [UIImage imageWithCGImage:quartzImage scale:1.0 orientation:UIImageOrientationRight]; 

    // Release the Quartz image
    CGImageRelease(quartzImage);

    return (image);
}

答案 2 :(得分:18)

你们都在努力解决这个问题。

在DidOutputSampleBuffer中,只需在抓取图像之前更改方向。它是单声道,但你有

    public class OutputRecorder : AVCaptureVideoDataOutputSampleBufferDelegate {    
        public override void DidOutputSampleBuffer (AVCaptureOutput captureOutput, CMSampleBuffer sampleBuffer, AVCaptureConnection connection)
        {
            try {
                connection.videoOrientation = AVCaptureVideoOrientation.LandscapeLeft;

在objC中就是这个方法

- ( void ) captureOutput: ( AVCaptureOutput * ) captureOutput
   didOutputSampleBuffer: ( CMSampleBufferRef ) sampleBuffer
      fromConnection: ( AVCaptureConnection * ) connection

答案 3 :(得分:15)

这是一个正确的序列:

AVCaptureVideoDataOutput *videoCaptureOutput = [[AVCaptureVideoDataOutput alloc] init];

if([self.captureSession canAddOutput:self.videoCaptureOutput]){
    [self.captureSession addOutput:self.videoCaptureOutput];
}else{
    NSLog(@"cantAddOutput");
}

// set portrait orientation
AVCaptureConnection *conn = [self.videoCaptureOutput connectionWithMediaType:AVMediaTypeVideo];
[conn setVideoOrientation:AVCaptureVideoOrientationPortrait];

答案 4 :(得分:10)

例如:

AVCaptureConnection *captureConnection = <a capture connection>;
if ([captureConnection isVideoOrientationSupported]) {
    captureConnection.videoOrientation = AVCaptureVideoOrientationPortrait;
}

默认值为AVCaptureVideoOrientationLandscapeRight

另见QA1744: Setting the orientation of video with AV Foundation

答案 5 :(得分:7)

对于那些需要使用CIImage并且缓冲区方向错误的人来说,我使用了这种修正。

就这么简单。 BTW数字3,1,6,8来自https://developer.apple.com/reference/imageio/kcgimagepropertyorientation

不要问我为什么3,1,6,8是正确的组合。我用暴力法找到它。如果你知道为什么让评论中的解释请...

- (void)captureOutput:(AVCaptureOutput *)captureOutput
    didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
           fromConnection:(AVCaptureConnection *)connection
{

    // common way to get CIImage

    CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

    CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, kCMAttachmentMode_ShouldPropagate);

    CIImage *ciImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer
                                                      options:(__bridge NSDictionary *)attachments];

    if (attachments) {
       CFRelease(attachments);
    }

    // fixing the orientation of the CIImage

    UIInterfaceOrientation curOrientation = [[UIApplication sharedApplication] statusBarOrientation];

    if (curOrientation == UIInterfaceOrientationLandscapeLeft){
        ciImage = [ciImage imageByApplyingOrientation:3];
    } else if (curOrientation == UIInterfaceOrientationLandscapeRight){
        ciImage = [ciImage imageByApplyingOrientation:1];
    } else if (curOrientation == UIInterfaceOrientationPortrait){
        ciImage = [ciImage imageByApplyingOrientation:6];
    } else if (curOrientation == UIInterfaceOrientationPortraitUpsideDown){
        ciImage = [ciImage imageByApplyingOrientation:8];
    }



    // ....

}

答案 6 :(得分:5)

如果AVCaptureVideoPreviewLayer方向正确,您只需在捕获图像之前设置方向。

AVCaptureStillImageOutput *stillImageOutput;
AVCaptureVideoPreviewLayer *previewLayer;
NSData *capturedImageData;

AVCaptureConnection *videoConnection = [stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
if ([videoConnection isVideoOrientationSupported]) {
    [videoConnection setVideoOrientation:previewLayer.connection.videoOrientation];
}
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
    CFDictionaryRef exifAttachments =
            CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
    if (exifAttachments) {
        // Do something with the attachments.
    }
    // TODO need to manually add GPS data to the image captured
    capturedImageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
    UIImage *image = [UIImage imageWithData:capturedImageData];
}];

另外,请注意UIImageOrientationAVCaptureVideoOrientation不同。 UIImageOrientationUp指横向模式,音量控制向下朝向地面(如果您考虑将音量控制用作快门按钮,向上)。

因此,电源按钮指向天空(AVCaptureVideoOrientationPortrait)的纵向方向实际上是UIImageOrientationLeft

答案 7 :(得分:2)

定位问题是前置摄像头,所以检查设备类型并生成新图像,肯定会解决定位问题:

-(void)capture:(void(^)(UIImage *))handler{

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; }
}

[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {

    if (imageSampleBuffer != NULL) {
        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
        **UIImage *capturedImage = [UIImage imageWithData:imageData];
        if (self.captureDevice == [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo][1]) {
            capturedImage = [[UIImage alloc] initWithCGImage:capturedImage.CGImage scale:1.0f orientation:UIImageOrientationLeftMirrored];
        }**

        handler(capturedImage);
    }
}];
}

答案 8 :(得分:1)

首先,在视频输出的配置中,放置以下行:

guard let connection = videoOutput.connection(withMediaType: 
AVFoundation.AVMediaTypeVideo) else { return }
guard connection.isVideoOrientationSupported else { return }
guard connection.isVideoMirroringSupported else { return }
connection.videoOrientation = .portrait
connection.isVideoMirrored = position == .front

然后,通过在常规配置中取消选中横向模式,将目标配置为仅支持Portait。

Source

答案 9 :(得分:1)

// #1
AVCaptureVideoOrientation newOrientation = AVCaptureVideoOrientationLandscapeRight;
if (@available(iOS 13.0, *)) {
    // #2
    for (AVCaptureConnection *connection in [captureSession connections]) {
        if ([connection isVideoOrientationSupported]) {
            connection.videoOrientation = newOrientation;
            break;
        }
    } // #3
} else if ([previewLayer.connection isVideoOrientationSupported]) {
    previewLayer.connection.videoOrientation = newOrientation;
}

一旦您可以正确使用AVCaptureSession,就可以设置视频方向。 这里是上面代码的详细描述。请记住,必须在执行[captureSession startRunning]后执行此代码:

  1. 选择您喜欢的方向
  2. 对于ios版本> = 13.0,您必须从captureSession检索活动的连接。请记住:仅视频连接支持videoOrientation
  3. 对于<13.0版的ios,您可以使用previewLayer
  4. 中的连接

如果viewController没有固定的方向,则可以在设备方向更改后为连接设置新的videoOrientation

答案 10 :(得分:-1)

你可以试试这个:

findOne()