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