【问题标题】:How to set frame size using Avcapture如何使用 Avcapture 设置帧大小
【发布时间】:2013-01-14 09:27:02
【问题描述】:

我正在使用AVCaptureSession 拍照。这是我的代码:

AVCaptureSession * newSession = [[AVCaptureSession alloc] init];

// Configure our capturesession
newSession.sessionPreset = AVCaptureSessionPresetMedium;

我得到了尺寸为 360*480 的图像。但这是我的问题,我想要大小为 280*200 的图像。我对裁剪图像不感兴趣。有什么方法可以设置AVCaptureSession 拍摄的图像大小?提前谢谢..

【问题讨论】:

    标签: ios ios5 ios4


    【解决方案1】:

    使用 AVCapturePresets,您可以明确地捕捉视频或照片的大小。拍摄照片后,您可以进行操作(裁剪、调整大小)以适合您想要的尺寸,但您无法设置预定的拍摄尺寸。

    有可接受的预设:

    NSString *const AVCaptureSessionPresetPhoto;
    NSString *const AVCaptureSessionPresetHigh;
    NSString *const AVCaptureSessionPresetMedium;
    NSString *const AVCaptureSessionPresetLow;
    NSString *const AVCaptureSessionPreset320x240;
    NSString *const AVCaptureSessionPreset352x288;
    NSString *const AVCaptureSessionPreset640x480;
    NSString *const AVCaptureSessionPreset960x540;
    NSString *const AVCaptureSessionPreset1280x720;
    

    来源:https://developer.apple.com/library/mac/#documentation/AVFoundation/Reference/AVCaptureSession_Class/Reference/Reference.html

    我使用的这种方法应该可以帮助您缩放到所需的大小:

    -(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
    {
        UIGraphicsBeginImageContext( newSize );
        [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return newImage;
    }
    

    初始化会话和设置预设:

    // create a capture session
    if(session == nil){
        session = [[AVCaptureSession alloc] init];
    }
    if ([session canSetSessionPreset:AVCaptureSessionPreset320x240]) {
        session.sessionPreset = AVCaptureSessionPreset320x240;
    }
    else {
        NSLog(@"Cannot set session preset");
    }
    

    【讨论】:

    • 如何使用 AVCaptureSessionPreset320x240..当我使用这个时,我得到了这样的错误“使用未声明的标识符”
    • 错误具体说了什么?我修改了我的答案,以包含我通常如何设置预设的示例。由于设备各不相同,因此最好检查以确保设备可以在所选预设下运行。
    • 很遗憾,AVCaptureSessionPreset320x240 仅适用于 Mac OS X。
    • 如果目标是捕获全屏视频,那么对于 5S 等尺寸与可用设备不同的设备,这如何工作?
    猜你喜欢
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    相关资源
    最近更新 更多