Does captureOutput:didOutputSampleBuffer:fromConnection: carry any orientation information? - ios

I apply tons of image processing to a camera live preview layer via OpenGL, and I want to get some information about the input`s orientation before I ask for the image from OpenGL (to apply corresponding transformations).
The capturing takes place in a portrait only viewController, and having notifications from the device orientation, or stuff may not be syncronized with the actual image orientation, I'm afraid.
Has CMSampleBuffer any orientation info? Or AVCaptureConnection?

Related

Know if picture is Selfie or Portrait

I'm coding an app where users can upload pictures and add some filters to it.
The problem is that when I apply filter on it, the picture is rotating, ONLY if the picture has been taken with back camera.
If it was a selfie the picture is not rotating
If the picture is in portrait mode, the picture is not rotating
The problem is that I don't know how I could get these information, in order to rotate the picture only when I need it.
You're thinking about this the wrong way. It may be the case that images taken with your phone's rear camera appear rotated after applying a filter, but you cannot make this assumption for all devices. Instead, you can read the imageOrientation property on UIImage to obtain information about whether the image has an unusual rotation.

iOS - Capture video in landscape while the device is in portrait mode

Is it possible to capture video in landscape while the device is in portrait mode?
something like this:
actually what i need is to capture in portrait but with width > height, i dont want the user to rotate the device, but i do want to capture a wider picture like in landscape mode.
just changing the preview layer frame to be wide (width>height) wont be enough of course.
i tried changing the video orientation of the preview layer, but that will rotate the picture, and thats not what i want.
previewLayer.connection.videoOrientation = .landscapeRight
is that make any sense?
No its not possible as you would have to physically rotate the camera.
You can CROP the output video to whatever aspect ratio you desire.
This will however make your vertical resolution be at most what your horizontal resolution currently is.
As well as decreasing your field of view.
If you still wanna crop the video to simulate this "smaller landscape mode" in real time i suggest you use the "GPUImageCropFilter" from the library GPUImage
You can, you need to use AVAssetWriter and set the dimensions of the output video.
However, remember that you're going to be reducing quality. If the camera orientation is portrait, then what you're receiving is a video that is (for arguments sake) 720H x 360W.
So you want to make that landscape, if you preserve the aspect ratio, you're going to end up with a video (by cropping the input) that's 180H x 360W.
Remember, there is a difference between what the camera sees, what you send to the preview layer and what you record to a file - they can all be independent of each other (you spoke about changing the preview layer frame, remember that has nothing to do with the video you write out).
Have you tried with setting gravity & bounds of previewLayer?
var bounds:CGRect = self.view.layer.bounds
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer?.bounds = bounds
previewLayer?.position = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds))

CMSampleBufferRef have always same video resolution?

I' trying to capture video by AVAssetWriter and AVCaptureOutput
You can find sample project here.
The video should be in portrait mode with any resolution. The main problem that it should be in portrait mode.
I'm trying to set different setting, but in the end, video is rotated and scaled to size (1920x1080) on iPhone SE.
Is it possible to control this resolution? Or at least orientation?
Video resolution is determined by the AVCaptureSession sessionPreset. You're setting that to medium, so you're getting the resolution that comes with that. If you want a different resolution, pass a different session preset, or use AVCaptureDevice to set a specific capture format. (For a good overview of capture session presets vs device formats, go back to this WWDC13 video.)
Per this Apple Developer Q&A, you need to set an orientation on the capture connection after you start the capture session in order to get "physically" rotated frame buffers (at a capture performance cost), or set the transform property on your asset writer (so that buffers are recorded in the sensor's native orientation, but clients display it in your intended orientation).

UIImage from UIImagePickerController orientation issue

I'm using UIImagePickerController to fetch images from the user's photo library and/or taken with the camera. Works great.
I'm noticing that fetched images are often (always?) coming back with their imageOrientation set to UIImageOrientationRight. But the image was captured with the device in portrait orientation. Why is this? This is an iPhone4S, iOS6, using the rear camera - so the resolution is 8MP.
In the simulator, grabbing photos from the photo library, images come back UIImageOrientationUp.
When I display the image in a UIImageView the orientation looks correct (portrait/up). But when I go to crop the image the coordinate system isn't what I would expect. 0,0 is in the upper-right of the image, which I guess makes sense when it reports UIImageOrientationRight.
I'm looking for an explanation of what's going on and the correct approach to dealing with the odd coordinate system.
EDIT: it sure appears to me that, on iPhone4S at least, the camera always takes UIImageOrientationRight/"landscape" images, and that UIImageView is respecting the imageOrientation on display. However, if I save the image using UIImagePNGRepresentation the orientation is not preserved (I think I read about this somewhere.)
It has to do with the orientation the phone was in when the image was taken. The phone doesn't rotate the image data from the camera sensor to make up in the image be up but instead sets the imageOrientation and then UIImage will take care of rendering things the right way.
When you try and crop, you typically change the image to be a CGImage and that loses the orientation information so suddenly you get the image with a strange orientation.
There are several categories on UIImage that you can get that will perform image cropping while taking imageOrientation into account.
Have a look at link or link

iOS AVFoundation Video Capture Orientation Options

I have an app that I would like to have video capture for the front-facing camera only. That's no problem. But I would like the video capture to always be in landscape, even when the phone is being held in portrait.
I have a working implementation based on the AVCamDemo code that Apple published. And borrowing from the information in this tech note, I am able to specify the orientation. There's just one trick: while the video frame is oriented correctly, the contents still appear as though shot in portrait:
I'm wondering if I'm just getting boned by the physical constraints of the hardware: is the image sensor just oriented this way? The referenced tech note above makes this note:
Important: Setting the orientation on a still image output and movie
file output doesn't physically rotate the buffers. For the movie file
output, it applies a track transform (matrix) to the video track so
that the movie is rotated on playback, and for the still image output
it inserts exif metadata that image viewers use to rotate the image
properly when viewing later.
But my playback of that video suggests otherwise. Any insight or suggestions would be appreciated!
Thanks,
Aaron.
To answer your question, yes, the image sensor is just oriented that way. The video camera is an approx 1-megapixel "1080p" camera that has a fixed orientation. The 5MP (or 8MP for 4S, etc) still camera also has a fixed orientation. The lenses themselves don't rotate nor do any of the other camera bits, and hence the feed itself has a fixed orientation.
"But wait!", you say, "pictures I take with the camera app (or API) get rotated correctly. Why is that?" That's cuz iOS takes a look at the orientation of the phone when a picture is taken and stores that information with the picture (as an Exif attachment). Yet video isn't so flagged -- and each frame would have to be individually flagged, and then there's issues about what to do when the user rotates the phone during video....
So, no, you can't ask a video stream or a still image what orientation the phone was in when the video was captured. You can, however, directly ask the phone what orientation it is in now:
UIDeviceOrientation currentOrientation = [UIDevice currentDevice].orientation;
If you do that at the start of video capture (or when you grab a still image from a video feed) you can then use that information to do your own rotation of playback.

Resources