美文网首页
iOS 自定义相机拍照,手动对焦和自动对焦

iOS 自定义相机拍照,手动对焦和自动对焦

作者: 闻醉山清风 | 来源:发表于2018-06-01 18:08 被阅读0次

天下虚怀接空谷,何处高峰不入云。

一、相机界面绘制需要的一些宏

#define kScreenBounds   [UIScreen mainScreen].bounds
#define kPhotographWidth  100   //拍摄区域宽度
#define kPhotographHeight  400   //拍摄区域高度
#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;
    }
    //注意添加区域改变捕获通知必须首先设置设备允许捕获
    [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(@"设置设备属性过程发生错误,错误信息:%@",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;
        }];
    }];
    
}

代码贴完,有待修改

相关文章

  • iOS 自定义相机拍照,手动对焦和自动对焦

    天下虚怀接空谷,何处高峰不入云。 一、相机界面绘制需要的一些宏 二、属性的申明 三、正文开始 自定义相机代码 改变...

  • 入门单反

    1. 对焦 AF:自动对焦,半按快门,对焦完成会有提示音,按下快门完成拍照;MF:手动对焦 对焦模式(3种):按住...

  • 对焦

    对焦概念 对焦是指使用照相机时调整好焦点距离,英文学名为Focus,一般有两种对焦方式,自动对焦、手动对焦。 对焦...

  • 对焦模式

    AF自动对焦,MF手动对焦。 MF手动对焦转动对焦环,变清晰,任何情况下都可以对焦。 Af自动对焦可以快速对焦,分...

  • 数码单反摄影轻松入门(2.7)

    巧设AF自动对焦和对焦模式 数码单方相机通常具备多个自动对焦点,可以对位于画面中心和四周的景物进行自动对焦,但正是...

  • iOS 自定义相机自动对焦拍照求助

    最近在做一个自定义相机的Demo, Demo的需求是相机对着某一处,当自动对焦成功后,然后拍摄图片。 自动对焦的功...

  • 摄影--对焦那些简单的事

    什么是对焦 对焦是指调节相机镜头镜片,使被拍摄对象能清晰成像的过程。数码单反相机的对焦方式则分为自动对焦(AF)和...

  • iOS自动对焦拍照

    最新有拍照的需求是这样的 ~ 手动点击屏幕聚焦,聚焦完成之后就自动拍照 于是开始各种脑补,在别人的基本拍照框架上进...

  • 2018-03-28

    最近经常用的app,是胶卷相机。这个app在照相的时候不能手动对焦,只能是软件自动对焦。不过这并不是它的缺点,我觉...

  • 摄影 || 教你一步拍出浪漫星斑

    先看看星斑效果: 正常拍摄效果 拍光斑需要支持MF手动对焦的相机,如果你的相机不是单反,请检查一下是否支持手动对焦...

网友评论

      本文标题:iOS 自定义相机拍照,手动对焦和自动对焦

      本文链接:https://www.haomeiwen.com/subject/qfvnaxtx.html