Capture picture from video using AVFoundation

2019-04-16 22:39发布

I am trying to capture a picture from a video on my iPad. I used Apple's AVCam example as a starting point.

I was able to see the video in my application and to take pictures from it. My problem is that the pixel size of the result image is wrong. I want a fullscreen picture (1024x768) but I get a smaller one (1024x720).

Those are my instance variables:

@property (retain) AVCaptureStillImageOutput *stillImageOutput;
@property (retain) AVCaptureVideoPreviewLayer *previewLayer;
@property (retain) AVCaptureSession *captureSession;
@property (retain) AVCaptureConnection *captureConnection;
@property (retain) UIImage *stillImage;

Here the code to take pictures:

- (void)takePicture
{
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in [[self 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: %@", [self stillImageOutput]);
    [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
                                                         completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
                                                             CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
                                                             if (exifAttachments) {
                                                                 NSLog(@"attachements: %@", exifAttachments);
                                                             } else {
                                                                 NSLog(@"no attachments");
                                                             }
                                                             NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
                                                             UIImage *image = [[UIImage alloc] initWithData:imageData];
                                                             [self setStillImage:image];
                                                             [image release];
                                                             [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];
                                                         }];
}

I thought of resizing the final picture but this solution would decrease the quality of the image. I also realized that the CFDictionaryRef exifAttachments dictionary contains a value PixelYDimension = 720; but I can't seem to find a way to interact with it.

Any help would be very much appreciated. Thank you in advance and have a nice day,

Alex.

EDIT: I'd like to point out that when I say "taking a picture from a video" I meant that the video is coming live from the iPad's camera and it is not a recording.

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-04-16 23:22

I found a solution to my problem. Here it goes in case someone looks for this in the future.

To interact with the camera using AVFoundation we need to initiate a AVCaptureSession variable. After doing this we can modify the sessionPreset indicating the quality level or bitrate of the output. There are a set of different constants. To take 1024x768 pictures I used AVCaptureSessionPresetPhoto

查看更多
登录 后发表回答