Skip to content

cameraOperation

yulin6666 edited this page Apr 12, 2017 · 10 revisions

摄像头相关操作

本SDK中将一些常用的操作封装了API,比如切换前后摄像头,闪光灯等。
而一些进阶的操作,则可以通过获取iOS系统的摄像头对象,然后调用系统API完成。
一些常见的需求可以在demo中找到实现的样例代码。

切换前后摄像头

- (BOOL) switchCamera;

当切换不成功时,会返回NO。切换不成功的情况主要是,当配置的参数,另一个摄像头不支持时。
比如: 有的设备上, 前置摄像头要弱于后置,高分辨率,仅仅后置摄像头支持。当设置高分辨率时,无法从后置切换至前置。

开关闪光灯

- (BOOL) isTorchSupported;
- (void) toggleTorch;

注意需要判断是否支持闪光灯,通常只有后置摄像头支持闪光灯。

获取摄像头对象

使用kit类时,可以通过如下方法获取摄像头对象

AVCaptureDevice *dev =[ kit getCurrentCameraDevices];

如果使用底层类,则可以通过KSYGPUCamera的inputCamera属性来获取摄像头对象。 获得 inputCamera 之后可以参考Apple的文档

下面是一些常用的对摄像头操作的方法

设置摄像头的对焦位置 (可以设置自动对焦/持续自动对焦/手动对焦模式)

  • 首先获得用户在取景画面上点击的位置。
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    bool ret;
    UITouch *touch = [touches anyObject];
    CGPoint current = [touch locationInView:self.view];
    CGPoint point = [self convertToPointOfInterestFromViewCoordinates:current];//坐标映射
    _foucsCursor.center = current;
    _foucsCursor.transform = CGAffineTransformMakeScale(1.5, 1.5);
    _foucsCursor.alpha=1.0;
    ret = [_kit exposureAtPoint:point];
    if (!ret){
        NSLog(@"Camera set exposure fail");
    }
    ret = [_kit focusAtPoint:point];
    if (ret){
        [UIView animateWithDuration:1.0 animations:^{
            _foucsCursor.transform=CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            _foucsCursor.alpha=0;
            
        }];
    }
}
  • 通过调用AVCaptureDevice的setFocusPointOfInterest方法(该方法在kit里面),可以改变对焦的位置, 三种对焦模式:
  1. AVCaptureFocusModeAutoFocus为自动对焦,
  2. AVCaptureFocusModeContinuousAutoFocus为持续自动对焦,
  3. AVCaptureFocusModeLocked为手动对焦。
- (BOOL)focusAtPoint:(CGPoint )point{
    AVCaptureDevice *dev = _vCapDev.inputCamera;
    NSError *error;
    if ([dev isFocusPointOfInterestSupported] && [dev isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
        if ([dev lockForConfiguration:&error]) {
            [dev setFocusPointOfInterest:point];
            [dev setFocusMode:AVCaptureFocusModeAutoFocus];
            [dev unlockForConfiguration];
            return YES;
        }
    }
    return NO;
}
  • 通过调用AVCaptureDevice的setExposurePointOfInterest方法(该方法在kit里面),可以改变曝光的位置。
- (BOOL)exposureAtPoint:(CGPoint )point{
    AVCaptureDevice *dev = _vCapDev.inputCamera;
    NSError *error;
    if ([dev isExposurePointOfInterestSupported] && [dev isExposureModeSupported:AVCaptureExposureModeAutoExpose]) {
        if ([dev lockForConfiguration:&error]) {
            [dev setExposurePointOfInterest:point];  // 曝光点
            [dev setExposureMode:AVCaptureExposureModeAutoExpose];
            [dev unlockForConfiguration];
            return YES;
        }
    }
    return NO;
}

设置摄像头的焦距 (缩放)

模拟系统相机的双指缩放取景画面内容的操作。

  • 添加双指缩放的动作
-(void)addPinchGestureRecognizer{
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchDetected:)];
}

- (void)pinchDetected:(UIPinchGestureRecognizer *)recognizer{
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        _currentPinchZoomFactor = _kit.pinchZoomFactor;
    }
    CGFloat zoomFactor = _currentPinchZoomFactor * recognizer.scale;//当前触摸缩放因子*坐标比例
    [_kit setPinchZoomFactor:zoomFactor];
}
  • 修改采集设备的pinchZoomFactor属性(该属性在kit里面)
@synthesize pinchZoomFactor =_pinchZoomFactor;

- (CGFloat) pinchZoomFactor {
    _pinchZoomFactor = _vCapDev.inputCamera.videoZoomFactor;
    return _pinchZoomFactor;
}

//设置新的触摸缩放因子
- (void)setPinchZoomFactor:(CGFloat)zoomFactor{
    AVCaptureDevice *captureDevice=_vCapDev.inputCamera;
    NSError *error = nil;
    [captureDevice lockForConfiguration:&error];
    if (!error) {
        CGFloat videoMaxZoomFactor = captureDevice.activeFormat.videoMaxZoomFactor;
        if (zoomFactor < 1.0f)
            zoomFactor = 1.0f;
        if (zoomFactor > videoMaxZoomFactor)
            zoomFactor = videoMaxZoomFactor;
        [captureDevice rampToVideoZoomFactor:zoomFactor withRate:1.0];
        captureDevice.videoZoomFactor = zoomFactor;
        [captureDevice unlockForConfiguration];
    }
}

设置摄像头的白平衡模式

通过setWhiteBalanceMode:设置摄像头的白平衡模式

  • 需要使用isWhiteBalanceModeSupported:方法判断摄像头是否支持该白平衡模式
- (BOOL)setDeviceBalanceMode:(AVCaptureWhiteBalanceMode)mode{
    AVCaptureDevice *device = [_kit getCurrentCameraDevices];
    
    if ([device isExposurePointOfInterestSupported] && [device isExposureModeSupported:AVCaptureExposureModeAutoExpose]){
        if ([device isWhiteBalanceModeSupported:mode]){
            NSError *error;
            if ([device lockForConfiguration:&error]) {
                [device setWhiteBalanceMode:mode];
                [device unlockForConfiguration];
                return YES;
            }
        }else{
            NSLog(@"not support current mode");
        }
    }
    return NO;
}

1. 推流环节说明

2. 特色功能说明

2.1 采集

2.2 音频处理

2.3 视频处理

2.4 编码

2.5 推流

2.6 输入多样化

2.7 集成

3. 第三方功能

4. 技术专栏

5. 已知问题

8. FAQ

金山云计算

Clone this wiki locally