CvPhotoCamera2.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // CvPhotoCamera2.mm
  3. //
  4. // Created by Giles Payne on 2020/04/01.
  5. //
  6. #import "CvCamera2.h"
  7. #pragma mark - Private Interface
  8. @interface CvPhotoCamera2 ()
  9. {
  10. id<CvPhotoCameraDelegate2> _delegate;
  11. }
  12. @property (nonatomic, strong) AVCaptureStillImageOutput* stillImageOutput;
  13. @end
  14. #pragma mark - Implementation
  15. @implementation CvPhotoCamera2
  16. #pragma mark Public
  17. - (void)setDelegate:(id<CvPhotoCameraDelegate2>)newDelegate {
  18. _delegate = newDelegate;
  19. }
  20. - (id<CvPhotoCameraDelegate2>)delegate {
  21. return _delegate;
  22. }
  23. #pragma mark - Public interface
  24. - (void)takePicture
  25. {
  26. if (self.cameraAvailable == NO) {
  27. return;
  28. }
  29. self.cameraAvailable = NO;
  30. [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:self.videoCaptureConnection
  31. completionHandler:
  32. ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
  33. {
  34. if (error == nil && imageSampleBuffer != NULL)
  35. {
  36. // TODO check
  37. // NSNumber* imageOrientation = [UIImage cgImageOrientationForUIDeviceOrientation:currentDeviceOrientation];
  38. // CMSetAttachment(imageSampleBuffer, kCGImagePropertyOrientation, imageOrientation, 1);
  39. NSData *jpegData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
  40. dispatch_async(dispatch_get_main_queue(), ^{
  41. [self.captureSession stopRunning];
  42. // Make sure we create objects on the main thread in the main context
  43. UIImage* newImage = [UIImage imageWithData:jpegData];
  44. //UIImageOrientation orientation = [newImage imageOrientation];
  45. // TODO: only apply rotation, don't scale, since we can set this directly in the camera
  46. /*
  47. switch (orientation) {
  48. case UIImageOrientationUp:
  49. case UIImageOrientationDown:
  50. newImage = [newImage imageWithAppliedRotationAndMaxSize:CGSizeMake(640.0, 480.0)];
  51. break;
  52. case UIImageOrientationLeft:
  53. case UIImageOrientationRight:
  54. newImage = [newImage imageWithMaxSize:CGSizeMake(640.0, 480.0)];
  55. default:
  56. break;
  57. }
  58. */
  59. // We have captured the image, we can allow the user to take another picture
  60. self.cameraAvailable = YES;
  61. NSLog(@"CvPhotoCamera2 captured image");
  62. [self.delegate photoCamera:self capturedImage:newImage];
  63. [self.captureSession startRunning];
  64. });
  65. }
  66. }];
  67. }
  68. - (void)stop;
  69. {
  70. [super stop];
  71. self.stillImageOutput = nil;
  72. }
  73. #pragma mark - Private Interface
  74. - (void)createStillImageOutput;
  75. {
  76. // setup still image output with jpeg codec
  77. self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
  78. NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil];
  79. [self.stillImageOutput setOutputSettings:outputSettings];
  80. [self.captureSession addOutput:self.stillImageOutput];
  81. for (AVCaptureConnection *connection in self.stillImageOutput.connections) {
  82. for (AVCaptureInputPort *port in [connection inputPorts]) {
  83. if ([port.mediaType isEqual:AVMediaTypeVideo]) {
  84. self.videoCaptureConnection = connection;
  85. break;
  86. }
  87. }
  88. if (self.videoCaptureConnection) {
  89. break;
  90. }
  91. }
  92. NSLog(@"[Camera] still image output created");
  93. }
  94. - (void)createCaptureOutput;
  95. {
  96. [self createStillImageOutput];
  97. }
  98. - (void)createCustomVideoPreview;
  99. {
  100. //do nothing, always use AVCaptureVideoPreviewLayer
  101. }
  102. @end