【问题标题】:UIImage orientation incorrect from AVCaptureStillImageOutput来自 AVCaptureStillImageOutput 的 UIImage 方向不正确
【发布时间】:2011-08-10 02:22:50
【问题描述】:

我正在尝试从 AVCaptureStillImageOutput 捕获像素数据,并注意到在将图像裁剪为 CGImage 后,它会重新定向。为了测试这一点,我将一个临时图像输出到照片库。现在我注意到,即使在图像被裁剪之前,它的缩略图也会旋转,而完整图像则不会。 (当我将 UIImage 传递给需要适当尺寸的图像的自定义 pixelDataManager 时,这稍后会成为问题。)

设置captureVideoPreviewLayer.orientation = AVCaptureVideoOrientationPortrait;[videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait]; 似乎没有任何改变。

有什么想法吗??? 我会把它分成几个部分...

1) 设置会话、输入和输出:

    // Create a capture session
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPresetMedium;

    AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

    // Add capture layer to visible view    
    captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResize;
    captureVideoPreviewLayer.frame = screenBounds;
    captureVideoPreviewLayer.bounds = screenBounds;
    captureVideoPreviewLayer.orientation = AVCaptureVideoOrientationPortrait;
    [self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    // Setup error handling
    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (!input) {
        // Handle the error appropriately.
        NSLog(@"ERROR: trying to open camera: %@", error);
    }
    [session addInput:input];

    // Start session
    [session startRunning];

    // Output image data 
    stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [stillImageOutput setOutputSettings:outputSettings];

    [session addOutput:stillImageOutput];

2) 设置视频连接:

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

if ([videoConnection isVideoOrientationSupported])
{
    [videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
}

3) 输出捕获的图像:

NSLog(@"about to request a capture from: %@", stillImageOutput);
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{         
    NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
    UIImage *image = [[UIImage alloc] initWithData:imageData];
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    //......
}

【问题讨论】:

  • 我会尝试查看 [image imageOrientation] 并查看它放置的方向。如果设置错误,您可以先旋转图像,然后将其写入相册。希望有帮助!
  • 谢谢,今晚我会看看。我实际上什至不需要相册输出。我只是作为一个快速检查来解释为什么我的像素数据表现得好像图像被旋转了,而且确实如此。
  • 即使您将视频会话设置为一个方向,您仍然需要决定您的图像在显示/保存时应该是什么方向。在这里查看我对类似问题的回答:stackoverflow.com/a/54225440/753964

标签: iphone ios uiimage avcapturesession avcapturedevice


【解决方案1】:

当您执行此操作时,虽然图像是作为 UIImage 提供给您的,但它实际上使用的是具有不同原点(我认为是左下角)的底层 Quartz CGImage 数据,这意味着当您使用图像时它会旋转到一边。

我找到了一个 C 函数,您可以使用 UIImage 作为参数调用它来修复它并返回固定的 UIImage。

http://blog.logichigh.com/2008/06/05/uiimage-fix/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-24
    • 2017-01-22
    • 2011-02-25
    • 2020-09-21
    • 1970-01-01
    • 2017-04-09
    • 2014-04-12
    • 1970-01-01
    相关资源
    最近更新 更多