2

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...

evenro
  • 2,626
  • 20
  • 35

2 Answers2

4

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.

Gabriel
  • 561
  • 5
  • 9
  • Hey, I added a code section that I already had in the code that handles the orientation rotation to the connections.. I already had it there. Also when checking the connection object (incoming to the method) it is the right orientation, the display is also good.. the only thing is the image itself that is bad... any ideas? – evenro May 01 '14 at 13:45
  • Yes, there is a method in which you use your instance of AVCaptureSession and set the quality. Here is the one I use: captureSessionInstance.sessionPreset = AVCaptureSessionPresetHigh; //This can also be AVCaptureSessionPresetMedium or AVCaptureSessionPresetLow. Hope this helps you! – Gabriel May 03 '14 at 05:19
0

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...

evenro
  • 2,626
  • 20
  • 35