日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

iOS使用AVCaptureSession自定义相机

發(fā)布時(shí)間:2025/4/14 c/c++ 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS使用AVCaptureSession自定义相机 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn)自:http://www.2cto.com/kf/201409/335951.html

關(guān)于iOS調(diào)用攝像機(jī)來(lái)獲取照片,通常我們都會(huì)調(diào)用UIImagePickerController來(lái)調(diào)用系統(tǒng)提供的相機(jī)來(lái)拍照,這個(gè)控件非常好 用。但是有時(shí)UIImagePickerController控件無(wú)法滿足我們的需求,例如我們需要更加復(fù)雜的OverlayerView,這時(shí)候我們就 要自己構(gòu)造一個(gè)攝像機(jī)控件了。

這需要使用AVFoundation.framework這個(gè)framework里面的組件了,所以我們先要導(dǎo)入這個(gè)頭文件,另外還需要的組件官方文檔是這么說(shuō)的:

● 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


這里我只構(gòu)造了一個(gè)具有拍照功能的照相機(jī),至于錄影和錄音功能這里就不加說(shuō)明了。

總結(jié)下來(lái),我們需要以下的對(duì)象:

?

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @property (nonatomic, strong)?????? AVCaptureSession??????????? * session; //AVCaptureSession對(duì)象來(lái)執(zhí)行輸入設(shè)備和輸出設(shè)備之間的數(shù)據(jù)傳遞 @property (nonatomic, strong)?????? AVCaptureDeviceInput??????? * videoInput; //AVCaptureDeviceInput對(duì)象是輸入流 @property (nonatomic, strong)?????? AVCaptureStillImageOutput?? * stillImageOutput; //照片輸出流對(duì)象,當(dāng)然我的照相機(jī)只有拍照功能,所以只需要這個(gè)對(duì)象就夠了 @property (nonatomic, strong)?????? AVCaptureVideoPreviewLayer? * previewLayer; //預(yù)覽圖層,來(lái)顯示照相機(jī)拍攝到的畫面 @property (nonatomic, strong)?????? UIBarButtonItem???????????? * toggleButton; //切換前后鏡頭的按鈕 @property (nonatomic, strong)?????? UIButton??????????????????? * shutterButton; //拍照按鈕 @property (nonatomic, strong)?????? UIView????????????????????? * cameraShowView; //放置預(yù)覽圖層的View

?

我的習(xí)慣是在init方法執(zhí)行的時(shí)候創(chuàng)建這些對(duì)象,然后在viewWillAppear方法里加載預(yù)覽圖層。現(xiàn)在就讓我們看一下代碼就清楚了。

?

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 - (void) initialSession { ????//這個(gè)方法的執(zhí)行我放在init方法里了 ????self.session = [[AVCaptureSession alloc] init]; ????self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self frontCamera] error:nil]; ????//[self fronCamera]方法會(huì)返回一個(gè)AVCaptureDevice對(duì)象,因?yàn)槲页跏蓟瘯r(shí)是采用前攝像頭,所以這么寫,具體的實(shí)現(xiàn)方法后面會(huì)介紹 ????self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; ????NSDictionary * outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil]; ????//這是輸出流的設(shè)置參數(shù)AVVideoCodecJPEG參數(shù)表示以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]; ????} ????? }

這是獲取前后攝像頭對(duì)象的方法

?

?

?
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]; }


接下來(lái)在viewWillAppear方法里執(zhí)行加載預(yù)覽圖層的方法

?

?

?
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方法中啟動(dòng)和關(guān)閉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]; ????} }


接著我們就來(lái)實(shí)現(xiàn)切換前后鏡頭的按鈕,按鈕的創(chuàng)建我就不多說(shuō)了

?

?

?
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)); ????}]; }

這是拍照按鈕的方法

?

這樣自定義照相機(jī)的簡(jiǎn)單功能就完成了,如果你想要再添加其他復(fù)雜的功能,可以參考一下下面這篇文章,希望對(duì)你們有所幫助。

http://course.gdou.com/blog/Blog.pzs/archive/2011/12/14/10882.html

轉(zhuǎn)載于:https://www.cnblogs.com/feiyu-mdm/p/5568460.html

總結(jié)

以上是生活随笔為你收集整理的iOS使用AVCaptureSession自定义相机的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。