亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用iOS開發自定義相機

發布時間:2021-09-27 14:37:32 來源:億速云 閱讀:157 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關如何使用iOS開發自定義相機的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

1、首先聲明以下對象

#import <AVFoundation/AVFoundation.h>//捕獲設備,通常是前置攝像頭,后置攝像頭,麥克風(音頻輸入)@property (nonatomic, strong) AVCaptureDevice *device;//AVCaptureDeviceInput 代表輸入設備,他使用AVCaptureDevice 來初始化@property (nonatomic, strong) AVCaptureDeviceInput *input;//輸出圖片@property (nonatomic ,strong) AVCaptureStillImageOutput *imageOutput;//session:由他把輸入輸出結合在一起,并開始啟動捕獲設備(攝像頭)@property (nonatomic, strong) AVCaptureSession *session;//圖像預覽層,實時顯示捕獲的圖像@property (nonatomic ,strong) AVCaptureVideoPreviewLayer *previewLayer;

2、初始化各個對象

- (void)cameraDistrict{// AVCaptureDevicePositionBack 后置攝像頭// AVCaptureDevicePositionFront 前置攝像頭 self.device = [self cameraWithPosition:AVCaptureDevicePositionFront]; self.input = [[AVCaptureDeviceInput alloc] initWithDevice:self.device error:nil]; self.imageOutput = [[AVCaptureStillImageOutput alloc] init]; self.session = [[AVCaptureSession alloc] init]; //  拿到的圖像的大小可以自行設定 // AVCaptureSessionPreset320x240 // AVCaptureSessionPreset352x288 // AVCaptureSessionPreset640x480 // AVCaptureSessionPreset960x540 // AVCaptureSessionPreset1280x720 // AVCaptureSessionPreset1920x1080 // AVCaptureSessionPreset3840x2160 self.session.sessionPreset = AVCaptureSessionPreset640x480; //輸入輸出設備結合 if ([self.session canAddInput:self.input]) {  [self.session addInput:self.input]; } if ([self.session canAddOutput:self.imageOutput]) {  [self.session addOutput:self.imageOutput]; } //預覽層的生成 self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session]; self.previewLayer.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64); self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; [self.view.layer addSublayer:self.previewLayer]; //設備取景開始 [self.session startRunning]; if ([_device lockForConfiguration:nil]) { //自動閃光燈,  if ([_device isFlashModeSupported:AVCaptureFlashModeAuto]) {   [_device setFlashMode:AVCaptureFlashModeAuto];  }  //自動白平衡,但是好像一直都進不去  if ([_device isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeAutoWhiteBalance]) {   [_device setWhiteBalanceMode:AVCaptureWhiteBalanceModeAutoWhiteBalance];  }  [_device unlockForConfiguration]; }}

根據前后置位置拿到相應的攝像頭:

- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position{ NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; for ( AVCaptureDevice *device in devices )  if ( device.position == position ){   return device;  } return nil;}

3、拍照拿到相應圖片:

- (void)photoBtnDidClick{ AVCaptureConnection *conntion = [self.imageOutput connectionWithMediaType:AVMediaTypeVideo];  if (!conntion) {   NSLog(@"拍照失敗!");   return;   } [self.imageOutput captureStillImageAsynchronouslyFromConnection:conntion completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {  if (imageDataSampleBuffer == nil) {   return ;   }  NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];  self.image = [UIImage imageWithData:imageData];  [self.session stopRunning];  [self.view addSubview:self.cameraImageView];}

4、保存照片到相冊:

#pragma - 保存至相冊- (void)saveImageToPhotoAlbum:(UIImage*)savedImage{ UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);}// 指定回調方法- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo{ NSString *msg = nil ; if(error != NULL){  msg = @"保存圖片失敗" ; }else{  msg = @"保存圖片成功" ; } UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存圖片結果提示"           message:msg           delegate:self           cancelButtonTitle:@"確定"           otherButtonTitles:nil]; [alert show];}

5、前后置攝像頭的切換

- (void)changeCamera{ NSUInteger cameraCount = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo] count]; if (cameraCount > 1) {  NSError *error;  //給攝像頭的切換添加翻轉動畫  CATransition *animation = [CATransition animation];  animation.duration = .5f;  animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];  animation.type = @"oglFlip";  AVCaptureDevice *newCamera = nil;  AVCaptureDeviceInput *newInput = nil; //拿到另外一個攝像頭位置  AVCaptureDevicePosition position = [[_input device] position];  if (position == AVCaptureDevicePositionFront){   newCamera = [self cameraWithPosition:AVCaptureDevicePositionBack];   animation.subtype = kCATransitionFromLeft;//動畫翻轉方向  }  else {   newCamera = [self cameraWithPosition:AVCaptureDevicePositionFront];   animation.subtype = kCATransitionFromRight;//動畫翻轉方向  }  //生成新的輸入  newInput = [AVCaptureDeviceInput deviceInputWithDevice:newCamera error:nil];  [self.previewLayer addAnimation:animation forKey:nil];  if (newInput != nil) {   [self.session beginConfiguration];   [self.session removeInput:self.input];   if ([self.session canAddInput:newInput]) {    [self.session addInput:newInput];    self.input = newInput;   } else {    [self.session addInput:self.input];   }   [self.session commitConfiguration];  } else if (error) {   NSLog(@"toggle carema failed, error = %@", error);  } }}

6、相機的其它參數設置

//AVCaptureFlashMode 閃光燈//AVCaptureFocusMode 對焦//AVCaptureExposureMode 曝光//AVCaptureWhiteBalanceMode 白平衡//閃光燈和白平衡可以在生成相機時候設置//曝光要根據對焦點的光線狀況而決定,所以和對焦一塊寫//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];  }  //曝光模式和曝光點  if ([self.device isExposureModeSupported:AVCaptureExposureModeAutoExpose ]) {   [self.device setExposurePointOfInterest:focusPoint];   [self.device setExposureMode:AVCaptureExposureModeAutoExpose];  }  [self.device unlockForConfiguration];  //設置對焦動畫  _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;   }];  }]; }}

7、遇到的一些坑和解決辦法

1) 前后置攝像頭的切換

前后值不能切換,各種嘗試找了半天沒找到有原因。后來發現我在設置圖片尺寸的時候設置為1080P [self.session canSetSessionPreset: AVCaptureSessionPreset1920x1080] ,前置攝像頭并不支持這么大的尺寸,所以就不能切換前置攝像頭。我驗證了下 前置攝像頭最高支持720P,720P以內可自由切換。  

當然也可以在前后置攝像頭切換的時候,根據前后攝像頭來設置不同的尺寸,這里不在贅述。

2)焦點位置

CGPoint focusPoint = CGPointMake( point.y /size.height ,1-point.x/size.width );setExposurePointOfInterest:focusPoint 函數后面Point取值范圍是取景框左上角(0,0)到取景框右下角(1,1)之間。官方是這么寫的:

  The value of this property is a CGPoint that determines the receiver's focus point of interest, if it has one. A value of (0,0) indicates that the camera should focus on the top left corner of the image, while a value of (1,1) indicates that it should focus on the bottom right. The default value is (0.5,0.5).

我也試了按這個來但位置就是不對,只能按上面的寫法才可以。前面是點擊位置的y/PreviewLayer的高度,后面是1-點擊位置的x/PreviewLayer的寬度

3)對焦和曝光

  我在設置對焦是 先設置了模式setFocusMode,后設置對焦位置,就會導致很奇怪的現象,對焦位置是你上次點擊的位置。所以一定要先設置位置,再設置對焦模式。  曝光同上

感謝各位的閱讀!關于“如何使用iOS開發自定義相機”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

ios
AI

吉林省| 临武县| 科尔| 嘉义县| 阿拉善盟| 东方市| 安西县| 色达县| 大埔县| 太湖县| 伊吾县| 平潭县| 津南区| 富顺县| 上犹县| 尼玛县| 项城市| 正阳县| 大英县| 宁城县| 广丰县| 金坛市| 阿图什市| 益阳市| 高唐县| 泌阳县| 耒阳市| 沈丘县| 栖霞市| 滨州市| 石台县| 盘锦市| 辽阳市| 天长市| 景东| 浦县| 天祝| 云龙县| 通州市| 威海市| 南平市|