天下虛懷接空谷领虹,何處高峰不入云烹俗。
一、相機界面繪制需要的一些宏
#define kScreenBounds [UIScreen mainScreen].bounds
#define kPhotographWidth 100 //拍攝區(qū)域寬度
#define kPhotographHeight 400 //拍攝區(qū)域高度
#define kBackgroudColor [UIColor colorWithWhite:0 alpha:.7] //遮罩顏色
#define kTopBackgroudColor [UIColor colorWithWhite:0 alpha:.9] //遮罩顏色
#define kShadeTopHeight StatusBarAndNavigationBarHeight//導航欄高度
#define kShadeBottomHeight 84//底部拍攝按鈕高度
#define kTopHeight ((SCREEN_HEIGHT-kPhotographHeight-kShadeTopHeight-kShadeBottomHeight)/2)
#define kLeftWidth ((SCREEN_WIDTH-kPhotographWidth)/2)
typedef void(^PropertyChangeBlock)(AVCaptureDevice *captureDevice);
二嘹锁、屬性的申明
@property (nonatomic,strong)AVCaptureDevice* device;
@property (nonatomic,strong)AVCaptureStillImageOutput *ImageOutPut;
@property (nonatomic,strong)AVCaptureSession *session;
@property (nonatomic,strong)AVCaptureDeviceInput* input;
@property (strong,nonatomic) UIImageView *focusCursor; //聚焦光標
三、正文開始
- (void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:YES];
if (self.session) {
[self.session stopRunning];
}
}
自定義相機代碼
- (void)customCamera{
//對焦手勢,方法在下面
[self addGenstureRecognizer];
AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//在修改devicce之前一定要調用lock方法,否則會引起崩潰
[device lockForConfiguration:nil];
if ([device isFlashModeSupported:AVCaptureFlashModeAuto]) {
[device setFlashMode:AVCaptureFlashModeAuto];
}
if ([device isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeAutoWhiteBalance]) {
[device setWhiteBalanceMode:AVCaptureWhiteBalanceModeAutoWhiteBalance];
}
//設置完成后調用unlock
[device unlockForConfiguration];
_device=device;
//captureDeviceInput
self.input = [[AVCaptureDeviceInput alloc]initWithDevice:device error:nil];
self.ImageOutPut = [[AVCaptureStillImageOutput alloc] init];
self.session = [[AVCaptureSession alloc]init];
if ([self.session canSetSessionPreset:AVCaptureSessionPresetHigh]) {
self.session.sessionPreset = AVCaptureSessionPresetHigh;
}
//注意添加區(qū)域改變捕獲通知必須首先設置設備允許捕獲
[self changeDeviceProperty:^(AVCaptureDevice *captureDevice) {
captureDevice.subjectAreaChangeMonitoringEnabled=YES;
}];
//自動對象,蘋果提供了對應的通知api接口,可以直接添加通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(subjectAreaDidChange:) name:AVCaptureDeviceSubjectAreaDidChangeNotification object:self.device];
if ([self.session canAddInput:self.input]) {
[self.session addInput:self.input];
}
if ([self.session canAddOutput:self.ImageOutPut]) {
[self.session addOutput:self.ImageOutPut];
}
[self.session commitConfiguration];
//開始啟動
[self.session startRunning];
dispatch_async(dispatch_get_main_queue(), ^{
AVCaptureVideoPreviewLayer* previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:self.session];
previewLayer.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
previewLayer.videoGravity = AVLayerVideoGravityResize;
[self.view.layer insertSublayer:previewLayer atIndex:0];
});
}
改變設備屬性的方法
//通過給屏幕上的view添加手勢,獲取手勢的坐標.將坐標用setFocusPointOfInterest方法賦值給device
-(void)changeDeviceProperty:(PropertyChangeBlock)propertyChange{
AVCaptureDevice *captureDevice= [self.input device];
NSError *error;
//注意改變設備屬性前一定要首先調用lockForConfiguration:調用完之后使用unlockForConfiguration方法解鎖
if ([captureDevice lockForConfiguration:&error]) {
propertyChange(captureDevice);
[captureDevice unlockForConfiguration];
}else{
NSLog(@"設置設備屬性過程發(fā)生錯誤着裹,錯誤信息:%@",error.localizedDescription);
}
}
手動對焦的方法
-(void)addGenstureRecognizer{
UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapScreen:)];
[_middleView addGestureRecognizer:tapGesture];
}
- (void)tapScreen:(UITapGestureRecognizer*)gesture{
CGPoint point = [gesture locationInView:gesture.view];
[self focusAtPoint:point];
}
- (void)focusAtPoint:(CGPoint)point{
CGSize size = self.view.bounds.size;
CGPoint focusPoint = CGPointMake( point.y /size.height ,1-point.x/size.width );
NSError *error;
if ([self.device lockForConfiguration:&error]) {
if ([self.device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
[self.device setFocusPointOfInterest:focusPoint];
[self.device setFocusMode:AVCaptureFocusModeAutoFocus];
}
[self.device unlockForConfiguration];
}
[self setFocusCursorWithPoint:point];
}
自動對焦的方法
- (void)subjectAreaDidChange:(NSNotification *)notification
{
//先進行判斷是否支持控制對焦
if (_device.isFocusPointOfInterestSupported &&[_device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
NSError *error =nil;
//對cameraDevice進行操作前领猾,需要先鎖定,防止其他線程訪問骇扇,
[_device lockForConfiguration:&error];
[_device setFocusMode:AVCaptureFocusModeAutoFocus];
[self focusAtPoint:_middleView.center];
//操作完成后摔竿,記得進行unlock。
[_device unlockForConfiguration];
}
}
-(void)setFocusCursorWithPoint:(CGPoint)point{
//下面是手觸碰屏幕后對焦的效果
_focusView.center = point;
_focusView.hidden = NO;
[UIView animateWithDuration:0.3 animations:^{
_focusView.transform = CGAffineTransformMakeScale(1.25, 1.25);
}completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 animations:^{
_focusView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
_focusView.hidden = YES;
}];
}];
}
代碼貼完,有待修改