-
最近在做畢設(shè)奏路,需要固定相機(jī)焦距,但是固定死了也不好斜脂,想著怎么自己去調(diào)整相機(jī)的焦距触机,翻了半天只發(fā)現(xiàn)AVCaptureDevice有設(shè)置焦距模式的玷或,可是沒看見調(diào)整焦距的呀庐椒?看起來有點(diǎn)關(guān)聯(lián)的adjustingFocus竟然是個(gè)BOOL型
??
AVCaptureDevice Focus章節(jié) 正準(zhǔn)備放棄在AVCaptureDevice類中找的時(shí)候在focusMode的枚舉定義里發(fā)現(xiàn)了這樣一個(gè)關(guān)鍵詞
focusMode枚舉
-
英文太菜并不認(rèn)識這個(gè)詞,但是它說被鎖定在len`s當(dāng)前的位置犁钟,那肯定與這個(gè)len`s有關(guān)泼橘,查一查詞典是“鏡頭”的意思??炬灭。再翻一遍文檔發(fā)現(xiàn)了重點(diǎn):
設(shè)置焦距的耶 測試一把重归,搞定收工,代碼如下:
#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, @"請使用真機(jī)測試");
#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
{
//創(chuàng)建session會話
_avSession = [[AVCaptureSession alloc] init];
[_avSession beginConfiguration];
_avSession.sessionPreset = AVCaptureSessionPreset640x480;
//通過capture對象創(chuàng)建輸入設(shè)備對象
NSError *error = nil;
_backCameraDevice = [self backCamera];
[_backCameraDevice lockForConfiguration:&error];
_backCameraDevice.focusMode = AVCaptureFocusModeLocked;
[_backCameraDevice unlockForConfiguration];
AVCaptureDeviceInput *inputDevice = [AVCaptureDeviceInput deviceInputWithDevice:_backCameraDevice error:&error];
//將輸入設(shè)備添加到會話
if ([_avSession canAddInput:inputDevice]) {
[_avSession addInput:inputDevice];
}else{
NSLog(@"不能添加視頻輸入設(shè)備");
return;
}
//添加一個(gè)輸出設(shè)備
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(@"不能添加視頻輸出設(shè)備");
return;
}
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_avSession];
//只有設(shè)置GravityResizeAspectFill或GravityResize椎木,然后設(shè)置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