iOS app camera orientation in captureOutput

2019-07-20 05:38发布

First, I know many people asked similar question - but I couldn't find any question that is similar.

When calling this interface:

- (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);
...

When the phone is in "portrait mode" - I get height of 480, and width of 640. The weird thing - is that when I check [connection videoOrientation] it is set correctly to portrait.

  1. Why does the connection orientation does not affect the sampled buffer?
  2. Should I rotate the image myself?
  3. Is there a way to configure the sample to be given by the device orientation?

Thanks


@Gabriel : I already have this code:

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

and as I mentioned above, when checking the connection - it is the right orientation, the image although is the wrong orientation..

Did you refer by fixing it manually - using what method would I use for it, to automatically change to the right orientation?

The thing is that the display is good, it's only the taking of the picture...

2条回答
狗以群分
2楼-- · 2019-07-20 06:16

addressing your questions:

1 - Check if you have disabled or modified the orientation mode.

2 - When working with AVFoundation you have to rotate the image yourself, here is the code I use:

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 - Use the code above

Hope this helps.

查看更多
爷、活的狠高调
3楼-- · 2019-07-20 06:19

OK...

It was super weird!!!

When the default application orientation was Landscape right - it did not work.

When I change the default application orientation to be portrait (button at the bottom) - it suddenly started working...

I wonder if it is a bug or expected behavior...

查看更多
登录 后发表回答