【问题标题】:iOS app camera orientation in captureOutputcaptureOutput 中的 iOS 应用程序相机方向
【发布时间】:2014-06-17 08:39:16
【问题描述】:

首先,我知道很多人都问过类似的问题 - 但我找不到任何类似的问题。

调用该接口时:

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

    CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(pixelBuffer, 0);

    // get buffer dimensions
    size_t bufferHeight = CVPixelBufferGetHeight(pixelBuffer);
    size_t bufferWidth  = CVPixelBufferGetWidth(pixelBuffer);
...

当手机处于“纵向模式” - 我得到 480 的高度和 640 的宽度。 奇怪的是,当我检查[connection videoOrientation] 时,它被正确设置为纵向。

  1. 为什么连接方向不影响采样缓冲区?
  2. 我应该自己旋转图像吗?
  3. 有没有办法将样本配置为由设备方向给出?

谢谢


@加布里埃尔: 我已经有了这个代码:

-(void) viewWillLayoutSubviews
{
    // Handle camera
    if (_videoPreviewLayer)
    {
        _videoPreviewLayer.frame = self.view.bounds;

        for (AVCaptureOutput* outcap in _captureSession.outputs)
        {
            for(AVCaptureConnection* con in outcap.connections)
            {
                switch ([[UIDevice currentDevice] orientation])
                {

                    case UIInterfaceOrientationPortrait:
                        [con setVideoOrientation:AVCaptureVideoOrientationPortrait];

                        break;
                    case UIInterfaceOrientationLandscapeRight:
                        [con setVideoOrientation:AVCaptureVideoOrientationLandscapeRight]; //home button on right. Refer to .h not doc
                        break;
                    case UIInterfaceOrientationLandscapeLeft:
                        [con setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft]; //home button on left. Refer to .h not doc
                        break;
                    case UIInterfaceOrientationPortraitUpsideDown:
                        [con setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown]; //home button on left. Refer to .h not doc
                        break;
                    default:
                        [con setVideoOrientation:AVCaptureVideoOrientationPortrait]; //for portrait upside down. Refer to .h not doc
                        break;
                }
            }
        }
    }
}

正如我上面提到的,在检查连接时 - 它是正确的方向,图像虽然是错误的方向..

您是否通过手动修复它进行参考 - 我将使用什么方法自动更改为正确的方向?

关键是显示器不错,就是拍照而已……

【问题讨论】:

    标签: ios objective-c avfoundation


    【解决方案1】:

    解决您的问题:

    1 - 检查您是否禁用或修改了方向模式。

    2 - 使用 AVFoundation 时,您必须自己旋转图像,这是我使用的代码:

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

    3 - 使用上面的代码

    希望这会有所帮助。

    【讨论】:

    • 嘿,我在处理连接的方向旋转的代码中添加了一个代码部分。我已经在那里了。此外,在检查连接对象(传入方法)时,它的方向正确,显示效果也很好.. 唯一不好的是图像本身......有什么想法吗?
    • 是的,有一种方法可以使用您的 AVCaptureSession 实例并设置质量。这是我使用的一个: captureSessionInstance.sessionPreset = AVCaptureSessionPresetHigh; //这也可以是 AVCaptureSessionPresetMedium 或 AVCaptureSessionPresetLow。希望这对你有帮助!
    【解决方案2】:

    好的...

    太奇怪了!!!

    当默认应用程序方向为横向时 - 它不起作用。

    当我将默认应用程序方向更改为纵向(底部按钮)时 - 它突然开始工作......

    我想知道这是一个错误还是预期的行为......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多