LLSimpleCamera+Helper.m 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // LLSimpleCamera+Helper.m
  3. // LLSimpleCameraExample
  4. //
  5. // Created by Ömer Faruk Gül on 20/02/16.
  6. // Copyright © 2016 Ömer Faruk Gül. All rights reserved.
  7. //
  8. #import "LLSimpleCamera+Helper.h"
  9. @implementation LLSimpleCamera (Helper)
  10. - (CGPoint)convertToPointOfInterestFromViewCoordinates:(CGPoint)viewCoordinates
  11. previewLayer:(AVCaptureVideoPreviewLayer *)previewLayer
  12. ports:(NSArray<AVCaptureInputPort *> *)ports
  13. {
  14. CGPoint pointOfInterest = CGPointMake(.5f, .5f);
  15. CGSize frameSize = previewLayer.frame.size;
  16. if ( [previewLayer.videoGravity isEqualToString:AVLayerVideoGravityResize] ) {
  17. pointOfInterest = CGPointMake(viewCoordinates.y / frameSize.height, 1.f - (viewCoordinates.x / frameSize.width));
  18. } else {
  19. CGRect cleanAperture;
  20. for (AVCaptureInputPort *port in ports) {
  21. if (port.mediaType == AVMediaTypeVideo) {
  22. cleanAperture = CMVideoFormatDescriptionGetCleanAperture([port formatDescription], YES);
  23. CGSize apertureSize = cleanAperture.size;
  24. CGPoint point = viewCoordinates;
  25. CGFloat apertureRatio = apertureSize.height / apertureSize.width;
  26. CGFloat viewRatio = frameSize.width / frameSize.height;
  27. CGFloat xc = .5f;
  28. CGFloat yc = .5f;
  29. if ( [previewLayer.videoGravity isEqualToString:AVLayerVideoGravityResizeAspect] ) {
  30. if (viewRatio > apertureRatio) {
  31. CGFloat y2 = frameSize.height;
  32. CGFloat x2 = frameSize.height * apertureRatio;
  33. CGFloat x1 = frameSize.width;
  34. CGFloat blackBar = (x1 - x2) / 2;
  35. if (point.x >= blackBar && point.x <= blackBar + x2) {
  36. xc = point.y / y2;
  37. yc = 1.f - ((point.x - blackBar) / x2);
  38. }
  39. } else {
  40. CGFloat y2 = frameSize.width / apertureRatio;
  41. CGFloat y1 = frameSize.height;
  42. CGFloat x2 = frameSize.width;
  43. CGFloat blackBar = (y1 - y2) / 2;
  44. if (point.y >= blackBar && point.y <= blackBar + y2) {
  45. xc = ((point.y - blackBar) / y2);
  46. yc = 1.f - (point.x / x2);
  47. }
  48. }
  49. } else if ([previewLayer.videoGravity isEqualToString:AVLayerVideoGravityResizeAspectFill]) {
  50. if (viewRatio > apertureRatio) {
  51. CGFloat y2 = apertureSize.width * (frameSize.width / apertureSize.height);
  52. xc = (point.y + ((y2 - frameSize.height) / 2.f)) / y2;
  53. yc = (frameSize.width - point.x) / frameSize.width;
  54. } else {
  55. CGFloat x2 = apertureSize.height * (frameSize.height / apertureSize.width);
  56. yc = 1.f - ((point.x + ((x2 - frameSize.width) / 2)) / x2);
  57. xc = point.y / frameSize.height;
  58. }
  59. }
  60. pointOfInterest = CGPointMake(xc, yc);
  61. break;
  62. }
  63. }
  64. }
  65. return pointOfInterest;
  66. }
  67. - (UIImage *)cropImage:(UIImage *)image usingPreviewLayer:(AVCaptureVideoPreviewLayer *)previewLayer
  68. {
  69. CGRect previewBounds = previewLayer.bounds;
  70. CGRect outputRect = [previewLayer metadataOutputRectOfInterestForRect:previewBounds];
  71. CGImageRef takenCGImage = image.CGImage;
  72. size_t width = CGImageGetWidth(takenCGImage);
  73. size_t height = CGImageGetHeight(takenCGImage);
  74. CGRect cropRect = CGRectMake(outputRect.origin.x * width, outputRect.origin.y * height,
  75. outputRect.size.width * width, outputRect.size.height * height);
  76. CGImageRef cropCGImage = CGImageCreateWithImageInRect(takenCGImage, cropRect);
  77. image = [UIImage imageWithCGImage:cropCGImage scale:1 orientation:image.imageOrientation];
  78. CGImageRelease(cropCGImage);
  79. return image;
  80. }
  81. @end