BDFaceImageUtils.m 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // BDFaceImageUtils.m
  3. // FaceSDKSample_IOS
  4. //
  5. // Created by 阿凡树 on 2017/5/24.
  6. // Copyright © 2017年 Baidu. All rights reserved.
  7. //
  8. #import "BDFaceImageUtils.h"
  9. #import <CoreGraphics/CoreGraphics.h>
  10. @implementation BDFaceImageUtils
  11. + (CGRect)convertRectFrom:(CGRect)imageRect imageSize:(CGSize)imageSize detectRect:(CGRect)detectRect {
  12. CGPoint imageTopLeft = imageRect.origin;
  13. CGPoint imageBottomRight = CGPointMake(CGRectGetMaxX(imageRect),CGRectGetMaxY(imageRect));
  14. CGPoint viewTopLeft = [self convertPointFrom:imageTopLeft imageSize:imageSize detectSize:detectRect.size];
  15. CGPoint viewBottomRight = [self convertPointFrom:imageBottomRight imageSize:imageSize detectSize:detectRect.size];
  16. return CGRectMake(viewTopLeft.x, viewTopLeft.y, viewBottomRight.x - viewTopLeft.x, viewBottomRight.y - viewTopLeft.y);
  17. }
  18. + (CGPoint)convertPointFrom:(CGPoint)imagePoint imageSize:(CGSize)imageSize detectSize:(CGSize)detectSize {
  19. CGPoint viewPoint = imagePoint;
  20. CGFloat scale = MAX(detectSize.width / imageSize.width, detectSize.height / imageSize.height);
  21. viewPoint.x *= scale;
  22. viewPoint.y *= scale;
  23. viewPoint.x += (detectSize.width - imageSize.width * scale) / 2.0;
  24. viewPoint.y += (detectSize.height - imageSize.height * scale) / 2.0;
  25. return viewPoint;
  26. }
  27. + (UIImage *)getImageResourceForName:(NSString *)name {
  28. NSString* bundlepath = [[NSBundle mainBundle] pathForResource:@"com.baidu.idl.face.faceSDK.bundle" ofType:nil];
  29. NSBundle* modelBundle = [NSBundle bundleWithPath:bundlepath];
  30. NSString* imagePath = [modelBundle pathForResource:name ofType:@"png"];
  31. return [[UIImage alloc] initWithContentsOfFile:imagePath];
  32. }
  33. @end