RACameraViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. @property (strong, nonatomic) IBOutlet UIButton *takePhotoBtn;
  15. @property (strong, nonatomic) IBOutlet UIButton *backBtn;
  16. @property (nonatomic,assign) BOOL barHidden;
  17. #pragma mark - Camera
  18. @property (nonatomic,strong) AVCaptureDevice *captureDevice;
  19. @property (nonatomic,strong) AVCaptureSession *captureSession;
  20. @property (nonatomic,strong) AVCaptureDeviceInput *captureInput;
  21. @property (nonatomic,strong) AVCaptureStillImageOutput *captureOutput;
  22. @property (nonatomic,strong) AVCaptureVideoPreviewLayer *previewLayer;
  23. @property (nonatomic,assign) BOOL cameraInitial;
  24. @property (nonatomic,assign) AVLayerVideoGravity gravity;
  25. @end
  26. @implementation RACameraViewController
  27. + (NSString *)storyboardID {
  28. return NSStringFromClass([self class]);
  29. }
  30. + (instancetype)viewControllerFromStoryboard {
  31. RACameraViewController *cameraVC = [[UIStoryboard storyboardWithName:@"Camera" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
  32. cameraVC.takeMode = RACameraTakeModeTakeOnce;
  33. cameraVC.gravity = AVLayerVideoGravityResizeAspectFill;
  34. return cameraVC;
  35. }
  36. + (instancetype)showCameraFromViewController:(UIViewController *)from withTakeMode:(RACameraTakeMode)mode videoGravity:(AVLayerVideoGravity)gravity completion:(void(^)(UIImage *image))completion {
  37. RACameraViewController *vc = [self viewControllerFromStoryboard];
  38. vc.takeMode = mode;
  39. if (!gravity) {
  40. gravity = AVLayerVideoGravityResizeAspectFill;
  41. }
  42. vc.gravity = gravity;
  43. vc.completion = completion;
  44. return vc;
  45. }
  46. - (void)viewDidLoad {
  47. [super viewDidLoad];
  48. // Do any additional setup after loading the view.
  49. UIImage *normal_img = [self.class imageWithColor:[UIColor redColor] Size:CGSizeMake(60, 60)];
  50. UIImage *highlight_img = [self.class imageWithColor:[UIColor whiteColor] Size:CGSizeMake(60, 60)];
  51. [self.takePhotoBtn setImage:normal_img forState:UIControlStateNormal];
  52. [self.takePhotoBtn setImage:highlight_img forState:UIControlStateHighlighted];
  53. // [self initCapture];
  54. // [self initTakePicture];
  55. if ([self camerAuthorization]) {
  56. [self initCapture];
  57. [self initTakePicture];
  58. } else {
  59. // fix:第一次打开相机的时候授权成功后,未初始化相机
  60. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
  61. dispatch_async(dispatch_get_main_queue(), ^{
  62. if (granted) {
  63. [self initCapture];
  64. [self initTakePicture];
  65. CGRect frame = self.previewContainer.bounds;
  66. [self resetPreview:CGRectOffset(frame, CGRectGetWidth(frame), 0)];
  67. [UIView animateWithDuration:0.3 animations:^{
  68. self.previewLayer.frame = frame;
  69. }];
  70. if (self.cameraInitial && !self.captureSession.isRunning) {
  71. [self.captureSession startRunning];
  72. }
  73. } else {
  74. NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  75. NSString *appName = [infoDict objectForKey:@"CFBundleDisplayName"];
  76. if (!appName) {
  77. appName = [infoDict objectForKey:@"CFBundleName"];
  78. }
  79. __weak typeof(self) weakSelf = self;
  80. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Warning" message:[NSString stringWithFormat:@"Camera access denied, please change %@ setting, allow App use camera. (setting -> privacy -> camera enable %@)",[UIDevice currentDevice].model,appName] preferredStyle:UIAlertControllerStyleAlert];
  81. UIAlertAction *action = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  82. [weakSelf dismissViewControllerAnimated:YES completion:nil];
  83. }];
  84. [alert addAction:action];
  85. [self presentViewController:alert animated:YES completion:nil];
  86. }
  87. });
  88. }];
  89. }
  90. /**
  91. * 管理导航条
  92. * 在viewDidload的时候隐藏导航条
  93. * 恢复导航条的显示/隐藏有两个地方:1.退出的时候;2.确认使用照片的时候
  94. */
  95. if (self.navigationController) {
  96. BOOL navBarHidden = self.navigationController.navigationBar.hidden;
  97. self.barHidden = navBarHidden;
  98. if (!navBarHidden) {
  99. [self.navigationController setNavigationBarHidden:YES animated:YES];
  100. }
  101. }
  102. }
  103. - (void)didReceiveMemoryWarning {
  104. [super didReceiveMemoryWarning];
  105. // Dispose of any resources that can be recreated.
  106. }
  107. - (void)viewWillAppear:(BOOL)animated {
  108. [super viewWillAppear:animated];
  109. if (self.cameraInitial && !self.captureSession.isRunning) {
  110. [self.captureSession startRunning];
  111. }
  112. }
  113. - (void)viewWillDisappear:(BOOL)animated {
  114. [super viewWillDisappear:animated];
  115. if ([self.captureSession isRunning]) {
  116. [self.captureSession stopRunning];
  117. }
  118. }
  119. - (BOOL)prefersStatusBarHidden {
  120. return YES;
  121. }
  122. - (void)viewDidLayoutSubviews {
  123. [super viewDidLayoutSubviews];
  124. // 强制布局一次,不然 takePhotoBtn frame还没发生改变
  125. [self.controlContanier layoutIfNeeded];
  126. if ([self camerAuthorization] && self.cameraInitial) {
  127. CGRect frame = self.previewContainer.bounds;
  128. BOOL isPortrait = self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown;
  129. // aspect ratio 3:4
  130. if (self.gravity == AVLayerVideoGravityResizeAspect) {
  131. CGRect backBtnFrame = [self.previewContainer convertRect:self.backBtn.frame fromView:self.controlContanier];
  132. CGRect captureBtnFrame = [self.previewContainer convertRect:self.takePhotoBtn.frame fromView:self.controlContanier];
  133. if (isPortrait) {
  134. CGFloat left = 0;
  135. CGFloat top = CGRectGetMaxY(backBtnFrame);
  136. CGFloat bottom = CGRectGetMinY(captureBtnFrame);
  137. CGFloat width = CGRectGetWidth(self.controlContanier.bounds);
  138. CGFloat height = bottom - top;
  139. frame = CGRectMake(left, top, width, height);
  140. } else {
  141. CGFloat left = CGRectGetMaxX(backBtnFrame);
  142. CGFloat right = CGRectGetMinX(captureBtnFrame);
  143. CGFloat top = 0;
  144. CGFloat width = right - left;
  145. CGFloat bottom = CGRectGetHeight(self.controlContanier.bounds);
  146. CGFloat height = bottom - top;
  147. frame = CGRectMake(left, top, width, height);
  148. }
  149. }
  150. [self resetPreview:frame];
  151. }
  152. }
  153. #pragma mark - Capture
  154. - (void)resetPreview:(CGRect)frame {
  155. self.previewLayer.frame = frame;
  156. if (self.previewLayer.connection.isVideoOrientationSupported) {
  157. self.previewLayer.connection.videoOrientation = [self captureVideoOrientation];
  158. }
  159. if ([self.captureOutput connectionWithMediaType:AVMediaTypeVideo].isVideoOrientationSupported) {
  160. [self.captureOutput connectionWithMediaType:AVMediaTypeVideo].videoOrientation = [self captureVideoOrientation];
  161. }
  162. }
  163. - (BOOL)camerAuthorization {
  164. AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  165. if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied) {
  166. return NO;
  167. }
  168. return YES;
  169. }
  170. - (void)initCapture {
  171. self.cameraInitial = NO;
  172. if (![self camerAuthorization]) {
  173. NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  174. NSString *appName = [infoDict objectForKey:@"CFBundleDisplayName"];
  175. if (!appName) {
  176. appName = [infoDict objectForKey:@"CFBundleName"];
  177. }
  178. __weak typeof(self) weakSelf = self;
  179. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Warning" message:[NSString stringWithFormat:@"Camera access denied, please change %@ setting, allow App use camera. (setting -> privacy -> camera enable %@)",[UIDevice currentDevice].model,appName] preferredStyle:UIAlertControllerStyleAlert];
  180. UIAlertAction *action = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  181. [weakSelf dismissViewControllerAnimated:YES completion:nil];
  182. }];
  183. [alert addAction:action];
  184. [self presentViewController:alert animated:YES completion:nil];
  185. return;
  186. }
  187. self.captureSession = [[AVCaptureSession alloc] init];
  188. self.captureDevice = [self videoDevicePosition:AVCaptureDevicePositionBack];
  189. if (!self.captureDevice) {
  190. NSLog(@"there is no capture device while init camera");
  191. return;
  192. }
  193. NSError *error;
  194. self.captureInput = [[AVCaptureDeviceInput alloc] initWithDevice:self.captureDevice error:&error];
  195. if (error) {
  196. NSLog(@"init camera error: %@",error);
  197. return;
  198. }
  199. [self.captureSession beginConfiguration];
  200. if ([_captureSession canSetSessionPreset:AVCaptureSessionPresetPhoto]) {
  201. _captureSession.sessionPreset = AVCaptureSessionPresetPhoto;
  202. }
  203. if ([self.captureDevice isFlashAvailable] && [_captureDevice isFlashModeSupported:AVCaptureFlashModeAuto]) {
  204. NSError *configErr;
  205. [self.captureDevice lockForConfiguration:&configErr];
  206. [self.captureDevice setFlashMode:AVCaptureFlashModeAuto];
  207. [self.captureDevice unlockForConfiguration];
  208. }
  209. // if ([self.captureDevice isAdjustingWhiteBalance] && [self.captureDevice isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeAutoWhiteBalance]) {
  210. // NSError *configErr;
  211. // [self.captureDevice lockForConfiguration:&configErr];
  212. // [self.captureDevice setWhiteBalanceMode:AVCaptureWhiteBalanceModeAutoWhiteBalance];
  213. // [self.captureDevice unlockForConfiguration];
  214. // }
  215. if ([self.captureSession canAddInput:self.captureInput]) {
  216. [self.captureSession addInput:self.captureInput];
  217. // if ([self.captureSession canSetSessionPreset:AVCaptureSessionPreset1920x1080]) {
  218. // self.captureSession.sessionPreset = AVCaptureSessionPreset1920x1080;
  219. // } else {
  220. // if ([self.captureSession canSetSessionPreset:AVCaptureSessionPresetHigh]) {
  221. // self.captureSession.sessionPreset = AVCaptureSessionPresetHigh;
  222. // }
  223. // }
  224. } else {
  225. NSLog(@"init camera can't add input");
  226. return;
  227. }
  228. [self.captureSession commitConfiguration];
  229. self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
  230. self.previewLayer.videoGravity = self.gravity;
  231. [self.previewContainer.layer addSublayer:self.previewLayer];
  232. self.cameraInitial = YES;
  233. }
  234. - (void)initTakePicture {
  235. if (!self.cameraInitial) {
  236. return;
  237. }
  238. self.captureOutput = [[AVCaptureStillImageOutput alloc] init];
  239. NSDictionary *setting = @{AVVideoCodecKey:AVVideoCodecJPEG};
  240. [self.captureOutput setOutputSettings:setting];
  241. if ([self.captureSession canAddOutput:self.captureOutput]) {
  242. [self.captureSession addOutput:self.captureOutput];
  243. }
  244. AVCaptureConnection *connection = [self.captureOutput connectionWithMediaType:AVMediaTypeVideo];
  245. if (connection.isVideoOrientationSupported) { // addOutput之后判断,否则一直都是false
  246. connection.videoOrientation = [self captureVideoOrientation];
  247. }
  248. }
  249. - (AVCaptureDevice *) videoDevicePosition:(AVCaptureDevicePosition)position {
  250. NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
  251. for (AVCaptureDevice *device in videoDevices) {
  252. if ([device position] == position) {
  253. return device;
  254. }
  255. }
  256. return nil;
  257. }
  258. - (AVCaptureVideoOrientation)captureVideoOrientation {
  259. AVCaptureVideoOrientation result;
  260. UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
  261. switch (deviceOrientation) {
  262. case UIDeviceOrientationPortrait:
  263. case UIDeviceOrientationFaceUp:
  264. case UIDeviceOrientationFaceDown:
  265. result = AVCaptureVideoOrientationPortrait;
  266. break;
  267. case UIDeviceOrientationPortraitUpsideDown:
  268. //如果这里设置成AVCaptureVideoOrientationPortraitUpsideDown,则视频方向和拍摄时的方向是相反的。
  269. result = AVCaptureVideoOrientationPortrait;
  270. break;
  271. case UIDeviceOrientationLandscapeLeft:
  272. result = AVCaptureVideoOrientationLandscapeRight;
  273. break;
  274. case UIDeviceOrientationLandscapeRight:
  275. result = AVCaptureVideoOrientationLandscapeLeft;
  276. break;
  277. default:
  278. result = AVCaptureVideoOrientationPortrait;
  279. break;
  280. }
  281. return result;
  282. }
  283. #pragma mark - Action
  284. - (IBAction)takePictureBtnClick:(UIButton *)sender {
  285. if (!self.cameraInitial) {
  286. return;
  287. }
  288. AVCaptureConnection *connection = [self.captureOutput connectionWithMediaType:AVMediaTypeVideo];
  289. __weak typeof(self) weakSelf = self;
  290. [self.captureOutput captureStillImageAsynchronouslyFromConnection:connection completionHandler:^(CMSampleBufferRef _Nullable imageDataSampleBuffer, NSError * _Nullable error) {
  291. if (imageDataSampleBuffer) {
  292. NSData *imageData=[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
  293. UIImage *image=[UIImage imageWithData:imageData];
  294. RATakePhotoPreviewController *preVC = [RATakePhotoPreviewController viewControllerFromStoryboard];
  295. preVC.photoHandler = ^(UIImage *img){
  296. if (weakSelf) {
  297. __strong typeof(weakSelf) strongSelf = weakSelf;
  298. if (strongSelf.completion) {
  299. strongSelf.completion(img);
  300. }
  301. }
  302. };
  303. preVC.preImage = image;
  304. if (weakSelf.takeMode == RACameraTakeModeTakeOnce) {
  305. preVC.popTo = weakSelf.fromVC;
  306. preVC.barHidden = weakSelf.barHidden;
  307. }
  308. [weakSelf.navigationController pushViewController:preVC animated:YES];
  309. } else {
  310. NSLog(@"take picture failed: %@",error);
  311. }
  312. }];
  313. }
  314. - (IBAction)returnButtonClick:(UIButton *)sender {
  315. if (self.navigationController) {
  316. [self.navigationController popViewControllerAnimated:YES];
  317. } else {
  318. [self dismissViewControllerAnimated:YES completion:nil];
  319. }
  320. if (self.navigationController) {
  321. [self.navigationController setNavigationBarHidden:self.barHidden animated:YES];
  322. }
  323. }
  324. #pragma mark - Utils
  325. + (UIImage *)imageWithColor:(UIColor *)color Size:(CGSize)size {
  326. UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
  327. CGContextRef ctx = UIGraphicsGetCurrentContext();
  328. CGContextAddEllipseInRect(ctx, CGRectMake(5, 5, size.width - 10, size.height - 10));
  329. CGContextSetFillColorWithColor(ctx, color.CGColor);
  330. CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
  331. CGContextSetLineWidth(ctx, 3.0f);
  332. CGContextDrawPath(ctx, kCGPathFillStroke);
  333. UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  334. UIGraphicsEndImageContext();
  335. return img;
  336. }
  337. @end