ios - 使用 AVFoundation 从视频中捕获图片

标签 ios camera avfoundation

我正在尝试从 iPad 上的视频中截取图片。我使用 Apple 的 AVCam 示例作为起点。

我能够在我的应用程序中看到视频并从中拍照。我的问题是结果图像的像素大小错误。我想要一张全屏图片 (1024x768),但我得到一张较小的图片 (1024x720)。

这些是我的实例变量:

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

这里是拍照代码:

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

我想调整最终图片的大小,但此解决方案会降低图片质量。 我还意识到 CFDictionaryRef exifAttachments 字典包含一个值 PixelYDimension = 720; 但我似乎找不到与之交互的方法。

如有任何帮助,我们将不胜感激。 提前谢谢你,祝你有美好的一天,

亚历克斯。

编辑:我想指出的是,当我说“从视频中拍摄照片”时,我的意思是视频是从 iPad 的摄像头实时拍摄的,它不是 一段录音。

最佳答案

我找到了解决问题的方法。保存在这里,以防将来有人查找此内容。

要使用 AVFoundation 与相机交互,我们需要启动一个 AVCaptureSession 变量。 这样做之后我们可以修改 sessionPreset指示输出的质量级别或比特率。 有一个set of different constants .为了拍摄 1024x768 的照片,我使用了 AVCaptureSessionPresetPhoto

关于ios - 使用 AVFoundation 从视频中捕获图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9683916/

相关文章:

ios - SKVideoNode 中的视频在 Xcode 模拟器中工作正常,但无法在大多数设备上播放

java - Spring FacebookTemplate 引发用户配置文件的权限异常

ios - 在 iOS 7 中,如何更改 "More"选项卡栏项目的颜色?

ios - 独立渲染到多采样缓冲区和解析帧缓冲区

javascript - 如何从android应用程序的webview访问设备摄像头

ios - iOS7 中的 AVAssetImageGenerator 随机失败

ios - Cordova iOS 启动画面

c++ - 如何在 OpenGL 中使用鼠标在相机周围移动?

opencv - 视频捕捉设置参数范围?

ios - AVPlayer 在添加缩略图时监听