-
Notifications
You must be signed in to change notification settings - Fork 270
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里面),可以改变对焦的位置, 三种对焦模式:
- AVCaptureFocusModeAutoFocus为自动对焦,
- AVCaptureFocusModeContinuousAutoFocus为持续自动对焦,
- 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;
}