ios 设置相机手动对焦(滑块调整焦距)

  • 最近在做毕设,需要固定相机焦距,但是固定死了也不好,想着怎么自己去调整相机的焦距,翻了半天只发现AVCaptureDevice有设置焦距模式的,可是没看见调整焦距的呀?看起来有点关联的adjustingFocus竟然是个BOOL型
    😲


    AVCaptureDevice Focus章节
  • 正准备放弃在AVCaptureDevice类中找的时候在focusMode的枚举定义里发现了这样一个关键词

focusMode枚举
  • 英文太菜并不认识这个词,但是它说被锁定在len`s当前的位置,那肯定与这个len`s有关,查一查词典是“镜头”的意思😂。再翻一遍文档发现了重点:


    设置焦距的耶
  • 测试一把,搞定收工,代码如下:


#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MobileCoreServices/MobileCoreServices.h>

#define XBScreenWidth   [UIScreen mainScreen].bounds.size.width
#define XBScreenHeight   [UIScreen mainScreen].bounds.size.height

@interface ViewController () <AVCaptureVideoDataOutputSampleBufferDelegate>

@property (strong, nonatomic) AVCaptureSession *avSession;
@property (strong, nonatomic) AVCaptureDevice *backCameraDevice;

@end

@implementation ViewController

- (void)viewDidLoad {
#if TARGET_OS_SIMULATOR
    NSAssert(0, @"请使用真机测试");
#endif
    [super viewDidLoad];
    [self setupSession];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (!_avSession.isRunning) {
        [_avSession startRunning];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (!_avSession.isRunning) {
        [_avSession stopRunning];
    }
}

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

- (void)setupSession
{
    //创建session会话
    _avSession = [[AVCaptureSession alloc] init];
    [_avSession beginConfiguration];
    _avSession.sessionPreset = AVCaptureSessionPreset640x480;
    //通过capture对象创建输入设备对象
    NSError *error = nil;
    _backCameraDevice = [self backCamera];
    [_backCameraDevice lockForConfiguration:&error];
    _backCameraDevice.focusMode = AVCaptureFocusModeLocked;
    [_backCameraDevice unlockForConfiguration];
    AVCaptureDeviceInput *inputDevice = [AVCaptureDeviceInput deviceInputWithDevice:_backCameraDevice error:&error];
    //将输入设备添加到会话
    if ([_avSession canAddInput:inputDevice]) {
        [_avSession addInput:inputDevice];
    }else{
        NSLog(@"不能添加视频输入设备");
        return;
    }
    //添加一个输出设备
    AVCaptureVideoDataOutput *videoOutput = [[AVCaptureVideoDataOutput alloc] init];
    videoOutput.videoSettings = @{(__bridge NSString *)kCVPixelBufferPixelFormatTypeKey : [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]};
    
    videoOutput.alwaysDiscardsLateVideoFrames = YES;
    
    if ([_avSession canAddOutput:videoOutput]) {
        [_avSession addOutput:videoOutput];
    }else{
        NSLog(@"不能添加视频输出设备");
        return;
    }
    
    AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_avSession];
    //只有设置GravityResizeAspectFill或GravityResize,然后设置frame才有效,图像不会按照frame的大小显示
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    previewLayer.frame = CGRectMake(0, 20, 320, 240);
    [self.view.layer addSublayer:previewLayer];

    [_avSession commitConfiguration];
}

#pragma mark -
#pragma mark IBAction

- (IBAction)foucsChange:(UISlider *)sender {
    
    NSError *error = nil;
    [_backCameraDevice lockForConfiguration:&error];
    [_backCameraDevice setFocusModeLockedWithLensPosition:sender.value completionHandler:nil];
    [_backCameraDevice unlockForConfiguration];
    
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容