ios 自定义拍照页面_iOS 自定义相机拍照,手动对焦和自动对焦

  • Post author:
  • Post category:其他

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

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

#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;

}];

}];

}

代码贴完,有待修改


版权声明:本文为weixin_42309773原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。