//
//  Camera.m
//  ZZYWeiXinShortMovie
//
//  Created by zhangziyi on 16/3/23.
//  Copyright 漏 2016骞?GLaDOS. All rights reserved.
//

#import "Camera.h"

@implementation Camera
-(instancetype)init{
    if (self = [super init]) {
        _session = [[AVCaptureSession alloc]init];
        [_session setSessionPreset:AVCaptureSessionPresetMedium];
        _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        NSError *error;
        //娣诲姞杈撳叆
        _deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error];
        if ([_session canAddInput:_deviceInput]) {
            [_session addInput:_deviceInput];
        }
        //娣诲姞杈撳嚭
        _deviceVideoOutput = [[AVCaptureVideoDataOutput alloc]init];
        [_deviceVideoOutput setVideoSettings:
         [NSDictionary dictionaryWithObject:
          [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
                                     forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
        _deviceVideoOutput.alwaysDiscardsLateVideoFrames = YES;
        if ([_session canAddOutput:_deviceVideoOutput]) {
            [_session addOutput:_deviceVideoOutput];
        }
        //閰嶇疆榛樿甯ф暟1绉?0甯?        [_session beginConfiguration];
        if ([_device lockForConfiguration:&error]) {
            [_device setActiveVideoMaxFrameDuration:CMTimeMake(1, 10)];
            [_device setActiveVideoMinFrameDuration:CMTimeMake(1, 10)];
            [_device unlockForConfiguration];
        }
        [_session commitConfiguration];
        [self focusAtPoint:_cameraView.center];
    }
    return self;
}
//棰勮灞傚祵鍏?-(void)embedLayerWithView:(UIView *)view{
    _cameraView = view;
    if (_session == nil) {
        return;
    }
    _videoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
    //璁剧疆layer澶у皬
    _videoPreviewLayer.frame = view.layer.bounds;
    //layer濉厖鐘舵€?    _videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    //璁剧疆鎽勫儚澶存湞鍚?    _videoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortrait;
    [view.layer addSublayer:_videoPreviewLayer];
    //鍒涘缓瀵圭劍鎵嬪娍
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapToFocus:)];
    [view addGestureRecognizer:tapGesture];
    //鍒濆鍖栧鐒︽彁绀烘
    focusView = [[UIView alloc]init];
    focusView.layer.borderWidth = 2;
    focusView.layer.borderColor = [UIColor greenColor].CGColor;
    [_videoPreviewLayer addSublayer:focusView.layer];
    [self focusViewAnimation:view.center];
}
//鐐瑰嚮鎵嬪娍鍝嶅簲鏂规硶
-(void)tapToFocus:(UITapGestureRecognizer*)gestureRecognizer{
    CGPoint point = [gestureRecognizer locationInView:_cameraView];
    CGPoint focusPoint = CGPointMake(point.x/_cameraView.frame.size.width, point.y/_cameraView.frame.size.height);
    [self focusAtPoint:focusPoint];
    //    [self continuousFocusAtPoint:focusPoint];
    [self focusViewAnimation:point];
    NSLog(@"x%f,y%f",focusView.frame.origin.x,focusView.frame.origin.y);
}
//璁剧疆鑷姩瀵圭劍
-(void)focusAtPoint:(CGPoint)point{
    if ([_device isFocusPointOfInterestSupported] && [_device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
        NSError *error = nil;
        if ([_device lockForConfiguration:&error]) {
            [_device setFocusPointOfInterest:point];
            [_device setFocusMode:AVCaptureFocusModeAutoFocus];
            [_device unlockForConfiguration];
        }
    }
}
//璁剧疆杩炵画瀵圭劍
-(void)continuousFocusAtPoint:(CGPoint)point{
    if ([_device isFocusPointOfInterestSupported] && [_device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
        NSError *error = nil;
        if ([_device lockForConfiguration:&error]) {
            [_device setFocusPointOfInterest:point];
            [_device setFocusMode:AVCaptureFocusModeContinuousAutoFocus];
            [_device unlockForConfiguration];
        }
    }
}
//瀵圭劍鎻愮ず妗嗗姩鐢?-(void)focusViewAnimation:(CGPoint)point{
    focusView.frame = CGRectMake(0, 0, 80, 80);
    focusView.center = point;
    focusView.alpha = 1;
    [UIView animateWithDuration:0.5 animations:^{
        focusView.frame = CGRectMake(0, 0, 60, 60);
        focusView.center = point;
    } completion:^(BOOL finished) {
        focusView.alpha = 0;
        [UIView animateWithDuration:0.1 animations:^{
            focusView.alpha = 1;
        } completion:^(BOOL finished) {
            focusView.alpha = 0;
        }];
    }];
}
//閰嶇疆鑷畾涔夋媿鎽勫抚鏁?- (void)setFrameNum:(NSInteger)frameNum{
    _frameNum = frameNum;
    [_session beginConfiguration];
    NSError *error;
    if ([_device lockForConfiguration:&error]) {
        [_device setActiveVideoMaxFrameDuration:CMTimeMake(1, (int)_frameNum)];
        [_device setActiveVideoMinFrameDuration:CMTimeMake(1, (int)_frameNum)];
        [_device unlockForConfiguration];
    }
    [_session commitConfiguration];
}
//寮€濮嬫媿鎽?-(void)startCamera{
    [_session startRunning];
}
//鍋滄鎷嶆憚
-(void)stopCamera{
    [_session stopRunning];
}
@end
// 鐗堟潈灞炰簬鍘熶綔鑰?// http://code4app.com (cn) http://code4app.net (en)
// 鍙戝竷浠g爜浜庢渶涓撲笟鐨勬簮鐮佸垎浜綉绔? Code4App.com