iOS使用AVCaptureSession自定义相机
轉自:http://www.2cto.com/kf/201409/335951.html
關于iOS調用攝像機來獲取照片,通常我們都會調用UIImagePickerController來調用系統提供的相機來拍照,這個控件非常好 用。但是有時UIImagePickerController控件無法滿足我們的需求,例如我們需要更加復雜的OverlayerView,這時候我們就 要自己構造一個攝像機控件了。
這需要使用AVFoundation.framework這個framework里面的組件了,所以我們先要導入這個頭文件,另外還需要的組件官方文檔是這么說的:
● An instance of AVCaptureDevice to represent the input device, such as a camera or microphone
● An instance of a concrete subclass of AVCaptureInput to configure the ports from the input device
● An instance of a concrete subclass of AVCaptureOutput to manage the output to a movie file or still image
● An instance of AVCaptureSession to coordinate the data flow from the input to the output
這里我只構造了一個具有拍照功能的照相機,至于錄影和錄音功能這里就不加說明了。
總結下來,我們需要以下的對象:
?
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @property (nonatomic, strong)?????? AVCaptureSession??????????? * session; //AVCaptureSession對象來執行輸入設備和輸出設備之間的數據傳遞 @property (nonatomic, strong)?????? AVCaptureDeviceInput??????? * videoInput; //AVCaptureDeviceInput對象是輸入流 @property (nonatomic, strong)?????? AVCaptureStillImageOutput?? * stillImageOutput; //照片輸出流對象,當然我的照相機只有拍照功能,所以只需要這個對象就夠了 @property (nonatomic, strong)?????? AVCaptureVideoPreviewLayer? * previewLayer; //預覽圖層,來顯示照相機拍攝到的畫面 @property (nonatomic, strong)?????? UIBarButtonItem???????????? * toggleButton; //切換前后鏡頭的按鈕 @property (nonatomic, strong)?????? UIButton??????????????????? * shutterButton; //拍照按鈕 @property (nonatomic, strong)?????? UIView????????????????????? * cameraShowView; //放置預覽圖層的View |
?
我的習慣是在init方法執行的時候創建這些對象,然后在viewWillAppear方法里加載預覽圖層。現在就讓我們看一下代碼就清楚了。
?
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | - (void) initialSession { ????//這個方法的執行我放在init方法里了 ????self.session = [[AVCaptureSession alloc] init]; ????self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self frontCamera] error:nil]; ????//[self fronCamera]方法會返回一個AVCaptureDevice對象,因為我初始化時是采用前攝像頭,所以這么寫,具體的實現方法后面會介紹 ????self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; ????NSDictionary * outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil]; ????//這是輸出流的設置參數AVVideoCodecJPEG參數表示以JPEG的圖片格式輸出圖片 ????[self.stillImageOutput setOutputSettings:outputSettings]; ????? ????if ([self.session canAddInput:self.videoInput]) { ????????[self.session addInput:self.videoInput]; ????} ????if ([self.session canAddOutput:self.stillImageOutput]) { ????????[self.session addOutput:self.stillImageOutput]; ????} ????? } |
這是獲取前后攝像頭對象的方法
?
?
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | - (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition) position { ????NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; ????for (AVCaptureDevice *device in devices) { ????????if ([device position] == position) { ????????????return device; ????????} ????} ????return nil; } - (AVCaptureDevice *)frontCamera { ????return [self cameraWithPosition:AVCaptureDevicePositionFront]; } - (AVCaptureDevice *)backCamera { ????return [self cameraWithPosition:AVCaptureDevicePositionBack]; } |
接下來在viewWillAppear方法里執行加載預覽圖層的方法
?
?
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | - (void) setUpCameraLayer { ????if (_cameraAvaible == NO) return; ????? ????if (self.previewLayer == nil) { ????????self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session]; ????????UIView * view = self.cameraShowView; ????????CALayer * viewLayer = [view layer]; ????????[viewLayer setMasksToBounds:YES]; ????????? ????????CGRect bounds = [view bounds]; ????????[self.previewLayer setFrame:bounds]; ????????[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspect]; ????????? ????????[viewLayer insertSublayer:self.previewLayer below:[[viewLayer sublayers] objectAtIndex:0]]; ????????? ????} } |
注意以下的方法,在viewDidAppear和viewDidDisappear方法中啟動和關閉session
?
?
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | - (void) viewDidAppear:(BOOL)animated { ????[super viewDidAppear:animated]; ????if (self.session) { ????????[self.session startRunning]; ????} } - (void) viewDidDisappear:(BOOL)animated { ????[super viewDidDisappear: animated]; ????if (self.session) { ????????[self.session stopRunning]; ????} } |
接著我們就來實現切換前后鏡頭的按鈕,按鈕的創建我就不多說了
?
?
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | - (void)toggleCamera { ????NSUInteger cameraCount = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo] count]; ????if (cameraCount > 1) { ????????NSError *error; ????????AVCaptureDeviceInput *newVideoInput; ????????AVCaptureDevicePosition position = [[_videoInput device] position]; ????????? ????????if (position == AVCaptureDevicePositionBack) ????????????newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self frontCamera] error:&error]; ????????else if (position == AVCaptureDevicePositionFront) ????????????newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self backCamera] error:&error]; ????????else ????????????return; ????????? ????????if (newVideoInput != nil) { ????????????[self.session beginConfiguration]; ????????????[self.session removeInput:self.videoInput]; ????????????if ([self.session canAddInput:newVideoInput]) { ????????????????[self.session addInput:newVideoInput]; ????????????????[self setVideoInput:newVideoInput]; ????????????} else { ????????????????[self.session addInput:self.videoInput]; ????????????} ????????????[self.session commitConfiguration]; ????????} else if (error) { ????????????NSLog(@"toggle carema failed, error = %@", error); ????????} ????} } |
這是切換鏡頭的按鈕方法
?
?
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | - (void) shutterCamera { ????AVCaptureConnection * videoConnection = [self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo]; ????if (!videoConnection) { ????????NSLog(@"take photo failed!"); ????????return; ????} ????? ????[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { ????????if (imageDataSampleBuffer == NULL) { ????????????return; ????????} ????????NSData * imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; ????????UIImage * image = [UIImage imageWithData:imageData]; ????????NSLog(@"image size = %@",NSStringFromCGSize(image.size)); ????}]; } |
這是拍照按鈕的方法
?
這樣自定義照相機的簡單功能就完成了,如果你想要再添加其他復雜的功能,可以參考一下下面這篇文章,希望對你們有所幫助。
http://course.gdou.com/blog/Blog.pzs/archive/2011/12/14/10882.html
轉載于:https://www.cnblogs.com/feiyu-mdm/p/5568460.html
總結
以上是生活随笔為你收集整理的iOS使用AVCaptureSession自定义相机的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WPF 之 线程使用
- 下一篇: C++中cin、cin.get()、ci