0

I'm struggling to find a resolution to this issue I have. I can't make heads or tails from various examples and other SO posts I have sifted through.

My problem is i'm wanting to take a still image from a live video feed on an iPad. I successful see the live feed and can take a still image successfully, however I have two problems:

  1. The live feed orientation is wrong. I'm developing the iPad app to work in LANDSCAPE mode ONLY. The live feed that comes up is tilted 90 degrees to the right for some reason? (Obviously I have the iPad oriented in landscape at this time)

  2. The still image that is taken is flipped upside down (and still tilted to the side however ends up being 90 degrees to the left obviously due to the upside down flip it does).

I've looked through other posts but I just don't get the fixes offered, how to implement them. Hope someone can help here. Thanks.

Code:

-(void)viewDidAppear:(BOOL)animated
{
AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetMedium;

//CALayer *viewLayer = self.cameraView.layer;
//NSLog(@"viewLayer = %@", viewLayer);

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

captureVideoPreviewLayer.frame = self.cameraView.bounds;
[self.cameraView.layer addSublayer:captureVideoPreviewLayer];

AVCaptureDevice *device = [self frontCamera];

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];
stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[stillImageOutput setOutputSettings:outputSettings];

[session addOutput:stillImageOutput];
[session startRunning];
}



- (AVCaptureDevice *)frontCamera {
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
    if ([device position] == AVCaptureDevicePositionFront) {
        return device;
    }
}
return nil;
}

- (IBAction)takePhoto:(id)sender {

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

//NSLog(@"about to request a capture from: %@", stillImageOutput);
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
 {
     //CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
     /*if (exifAttachments)
     {
         // Do something with the attachments.
         NSLog(@"attachements: %@", exifAttachments);
     }
     else
         NSLog(@"no attachments");*/

     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
     UIImage *image = [[UIImage alloc] initWithData:imageData];

     self.imageView.image = image;
 }];
}
Bobster4300
  • 254
  • 4
  • 15
  • Apologies for all the commented-out code in there. I used this code from elsewhere and commented out the parts I don't need right now. – Bobster4300 Jan 01 '13 at 05:02
  • I managed to get the video feed the right way up but using the deprecated code `captureVideoPreviewLayer.orientation = videoOrientation;` along with the `AVCaptureVideoOrientation` code. How do I implement the new way though seeing as this is deprecated in IOS 6? Still have the issue of flipped image though. – Bobster4300 Jan 01 '13 at 05:22

0 Answers0