-
最近在做毕设,需要固定相机焦距,但是固定死了也不好,想着怎么自己去调整相机的焦距,翻了半天只发现AVCaptureDevice有设置焦距模式的,可是没看见调整焦距的呀?看起来有点关联的adjustingFocus竟然是个BOOL型
😲
正准备放弃在AVCaptureDevice类中找的时候在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