RACameraViewController.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // RACameraViewController.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/6/5.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RACameraViewController.h"
  9. #import <AVFoundation/AVFoundation.h>
  10. #import "RATakePhotoPreviewController.h"
  11. @interface RACameraViewController ()
  12. @property (nonatomic,strong) IBOutlet UIView *previewContainer;
  13. @property (nonatomic,strong) IBOutlet UIView *controlContanier;
  14. #pragma mark - Camera
  15. @property (nonatomic,strong) AVCaptureDevice *captureDevice;
  16. @property (nonatomic,strong) AVCaptureSession *captureSession;
  17. @property (nonatomic,strong) AVCaptureDeviceInput *captureInput;
  18. @property (nonatomic,strong) AVCaptureStillImageOutput *captureOutput;
  19. @property (nonatomic,strong) AVCaptureVideoPreviewLayer *previewLayer;
  20. @property (nonatomic,assign) BOOL cameraInitial;
  21. @end
  22. @implementation RACameraViewController
  23. + (instancetype)viewControllerFromStoryboard {
  24. RACameraViewController *cameraVC = [[UIStoryboard storyboardWithName:@"Camera" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
  25. return cameraVC;
  26. }
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. // Do any additional setup after loading the view.
  30. [self initCapture];
  31. [self initTakePicture];
  32. }
  33. - (void)didReceiveMemoryWarning {
  34. [super didReceiveMemoryWarning];
  35. // Dispose of any resources that can be recreated.
  36. }
  37. - (void)viewWillAppear:(BOOL)animated {
  38. [super viewWillAppear:animated];
  39. if (self.cameraInitial) {
  40. [self.captureSession startRunning];
  41. }
  42. }
  43. - (void)viewWillDisappear:(BOOL)animated {
  44. [super viewWillDisappear:animated];
  45. if ([self.captureSession isRunning]) {
  46. [self.captureSession stopRunning];
  47. }
  48. }
  49. - (void)viewDidLayoutSubviews {
  50. [super viewDidLayoutSubviews];
  51. self.previewLayer.frame = self.previewContainer.bounds;
  52. }
  53. #pragma mark - Capture
  54. - (void)initCapture {
  55. self.cameraInitial = NO;
  56. self.captureSession = [[AVCaptureSession alloc] init];
  57. self.captureDevice = [self videoDevicePosition:AVCaptureDevicePositionBack];
  58. if (!self.captureDevice) {
  59. NSLog(@"there is no capture device while init camera");
  60. return;
  61. }
  62. NSError *error;
  63. self.captureInput = [[AVCaptureDeviceInput alloc] initWithDevice:self.captureDevice error:&error];
  64. if (error) {
  65. NSLog(@"init camera error: %@",error);
  66. return;
  67. }
  68. [self.captureSession beginConfiguration];
  69. if ([_captureSession canSetSessionPreset:AVCaptureSessionPresetHigh]) {
  70. _captureSession.sessionPreset = AVCaptureSessionPresetHigh;
  71. }
  72. if ([self.captureSession canAddInput:self.captureInput]) {
  73. [self.captureSession addInput:self.captureInput];
  74. // if ([self.captureSession canSetSessionPreset:AVCaptureSessionPreset1920x1080]) {
  75. // self.captureSession.sessionPreset = AVCaptureSessionPreset1920x1080;
  76. // } else {
  77. // if ([self.captureSession canSetSessionPreset:AVCaptureSessionPresetHigh]) {
  78. // self.captureSession.sessionPreset = AVCaptureSessionPresetHigh;
  79. // }
  80. // }
  81. } else {
  82. NSLog(@"init camera can't add input");
  83. return;
  84. }
  85. [self.captureSession commitConfiguration];
  86. self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
  87. self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
  88. [self.previewContainer.layer addSublayer:self.previewLayer];
  89. self.cameraInitial = YES;
  90. }
  91. - (void)initTakePicture {
  92. if (!self.cameraInitial) {
  93. return;
  94. }
  95. self.captureOutput = [[AVCaptureStillImageOutput alloc] init];
  96. AVCaptureConnection *connection = [self.captureOutput connectionWithMediaType:AVMediaTypeVideo];
  97. if (connection.supportsVideoOrientation) {
  98. connection.videoOrientation = [self.previewLayer connection].videoOrientation;
  99. // connection.videoOrientation = [self captureVideoOrientation];
  100. }
  101. NSDictionary *setting = @{AVVideoCodecKey:AVVideoCodecJPEG};
  102. [self.captureOutput setOutputSettings:setting];
  103. if ([self.captureSession canAddOutput:self.captureOutput]) {
  104. [self.captureSession addOutput:self.captureOutput];
  105. }
  106. }
  107. - (AVCaptureDevice *) videoDevicePosition:(AVCaptureDevicePosition)position {
  108. NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
  109. for (AVCaptureDevice *device in videoDevices) {
  110. if ([device position] == position) {
  111. return device;
  112. }
  113. }
  114. return nil;
  115. }
  116. - (AVCaptureVideoOrientation)captureVideoOrientation {
  117. AVCaptureVideoOrientation result;
  118. UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
  119. switch (deviceOrientation) {
  120. case UIDeviceOrientationPortrait:
  121. case UIDeviceOrientationFaceUp:
  122. case UIDeviceOrientationFaceDown:
  123. result = AVCaptureVideoOrientationPortrait;
  124. break;
  125. case UIDeviceOrientationPortraitUpsideDown:
  126. //如果这里设置成AVCaptureVideoOrientationPortraitUpsideDown,则视频方向和拍摄时的方向是相反的。
  127. result = AVCaptureVideoOrientationPortrait;
  128. break;
  129. case UIDeviceOrientationLandscapeLeft:
  130. result = AVCaptureVideoOrientationLandscapeRight;
  131. break;
  132. case UIDeviceOrientationLandscapeRight:
  133. result = AVCaptureVideoOrientationLandscapeLeft;
  134. break;
  135. default:
  136. result = AVCaptureVideoOrientationPortrait;
  137. break;
  138. }
  139. return result;
  140. }
  141. #pragma mark - Action
  142. - (IBAction)takePictureBtnClick:(UIButton *)sender {
  143. if (!self.cameraInitial) {
  144. return;
  145. }
  146. AVCaptureConnection *connection = [self.captureOutput connectionWithMediaType:AVMediaTypeVideo];
  147. [self.captureOutput captureStillImageAsynchronouslyFromConnection:connection completionHandler:^(CMSampleBufferRef _Nullable imageDataSampleBuffer, NSError * _Nullable error) {
  148. if (imageDataSampleBuffer) {
  149. NSData *imageData=[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
  150. UIImage *image=[UIImage imageWithData:imageData];
  151. __weak typeof(self) weakSelf = self;
  152. RATakePhotoPreviewController *preVC = [RATakePhotoPreviewController viewControllerFromStoryboard];
  153. preVC.photoHandler = ^(UIImage *img){
  154. if (weakSelf) {
  155. __strong typeof(weakSelf) strongSelf = weakSelf;
  156. if (strongSelf.completion) {
  157. strongSelf.completion(img);
  158. }
  159. }
  160. };
  161. preVC.preImage = image;
  162. preVC.popTo = self.fromVC;
  163. [self.navigationController pushViewController:preVC animated:YES];
  164. } else {
  165. NSLog(@"take picture failed: %@",error);
  166. }
  167. }];
  168. }
  169. @end