天下虛懷接空谷,何處高峰不入云。
一、相機(jī)界面繪制需要的一些宏
#define kScreenBounds [UIScreen mainScreen].bounds
#define kPhotographWidth 100 //拍攝區(qū)域?qū)挾?#define kPhotographHeight 400 //拍攝區(qū)域高度
#define kBackgroudColor [UIColor colorWithWhite:0 alpha:.7] //遮罩顏色
#define kTopBackgroudColor [UIColor colorWithWhite:0 alpha:.9] //遮罩顏色
#define kShadeTopHeight StatusBarAndNavigationBarHeight//導(dǎo)航欄高度
#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; //聚焦光標(biāo)
三、正文開始
- (void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:YES];
if (self.session) {
[self.session stopRunning];
}
}
自定義相機(jī)代碼
- (void)customCamera{
//對(duì)焦手勢,方法在下面
[self addGenstureRecognizer];
AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//在修改devicce之前一定要調(diào)用lock方法,否則會(huì)引起崩潰
[device lockForConfiguration:nil];
if ([device isFlashModeSupported:AVCaptureFlashModeAuto]) {
[device setFlashMode:AVCaptureFlashModeAuto];
}
if ([device isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeAutoWhiteBalance]) {
[device setWhiteBalanceMode:AVCaptureWhiteBalanceModeAutoWhiteBalance];
}
//設(shè)置完成后調(diào)用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;
}
//注意添加區(qū)域改變捕獲通知必須首先設(shè)置設(shè)備允許捕獲
[self changeDeviceProperty:^(AVCaptureDevice *captureDevice) {
captureDevice.subjectAreaChangeMonitoringEnabled=YES;
}];
//自動(dòng)對(duì)象,蘋果提供了對(duì)應(yīng)的通知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];
//開始啟動(dòng)
[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];
});
}
改變?cè)O(shè)備屬性的方法
//通過給屏幕上的view添加手勢,獲取手勢的坐標(biāo).將坐標(biāo)用setFocusPointOfInterest方法賦值給device
-(void)changeDeviceProperty:(PropertyChangeBlock)propertyChange{
AVCaptureDevice *captureDevice= [self.input device];
NSError *error;
//注意改變?cè)O(shè)備屬性前一定要首先調(diào)用lockForConfiguration:調(diào)用完之后使用unlockForConfiguration方法解鎖
if ([captureDevice lockForConfiguration:&error]) {
propertyChange(captureDevice);
[captureDevice unlockForConfiguration];
}else{
NSLog(@"設(shè)置設(shè)備屬性過程發(fā)生錯(cuò)誤,錯(cuò)誤信息:%@",error.localizedDescription);
}
}
手動(dòng)對(duì)焦的方法
-(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];
}
自動(dòng)對(duì)焦的方法
- (void)subjectAreaDidChange:(NSNotification *)notification
{
//先進(jìn)行判斷是否支持控制對(duì)焦
if (_device.isFocusPointOfInterestSupported &&[_device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
NSError *error =nil;
//對(duì)cameraDevice進(jìn)行操作前,需要先鎖定,防止其他線程訪問,
[_device lockForConfiguration:&error];
[_device setFocusMode:AVCaptureFocusModeAutoFocus];
[self focusAtPoint:_middleView.center];
//操作完成后,記得進(jìn)行unlock。
[_device unlockForConfiguration];
}
}
-(void)setFocusCursorWithPoint:(CGPoint)point{
//下面是手觸碰屏幕后對(duì)焦的效果
_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;
}];
}];
}
代碼貼完,有待修改