|
- //
- // Utility.m
- // lpr
- //
- // Created by baotim on 2018/10/26.
- // Copyright © 2018 lprSample. All rights reserved.
- //
-
- #import "Utility.h"
-
- @implementation Utility
-
- + (UIImage *)UIImageFromCVMat:(cv::Mat)cvMat
- {
- NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
- CGColorSpaceRef colorSpace;
-
- if (cvMat.elemSize() == 1) {
- colorSpace = CGColorSpaceCreateDeviceGray();
- } else {
- colorSpace = CGColorSpaceCreateDeviceRGB();
- }
-
- CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
-
- // Creating CGImage from cv::Mat
- CGImageRef imageRef = CGImageCreate(cvMat.cols, //width
- cvMat.rows, //height
- 8, //bits per component
- 8 * cvMat.elemSize(), //bits per pixel
- cvMat.step[0], //bytesPerRow
- colorSpace, //colorspace
- kCGImageAlphaNone|kCGBitmapByteOrderDefault,// bitmap info
- provider, //CGDataProviderRef
- NULL, //decode
- false, //should interpolate
- kCGRenderingIntentDefault //intent
- );
-
- // Getting UIImage from CGImage
- UIImage *finalImage = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:UIImageOrientationUp];
- CGImageRelease(imageRef);
- CGDataProviderRelease(provider);
- CGColorSpaceRelease(colorSpace);
-
- return finalImage;
- }
-
- //缩放调整图片
- + (UIImage *)scaleAndRotateImageBackCamera:(UIImage *)image
- {
- static int kMaxResolution = 480;
- CGImageRef imgRef = image.CGImage;
- CGFloat width = CGImageGetWidth(imgRef);
- CGFloat height = CGImageGetHeight(imgRef);
-
- CGAffineTransform transform = CGAffineTransformIdentity;
- CGRect bounds = CGRectMake(0, 0, width, height);
- if (width > kMaxResolution || height > kMaxResolution) {
- CGFloat ratio = width/height;
- if (ratio > 1) {
- bounds.size.width = kMaxResolution;
- bounds.size.height = bounds.size.width / ratio;
- } else {
- bounds.size.height = kMaxResolution;
- bounds.size.width = bounds.size.height * ratio;
- }
- }
- CGFloat scaleRatio = bounds.size.width / width;
- CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
- CGFloat boundHeight;
- UIImageOrientation orient = image.imageOrientation;
- switch(orient) {
- case UIImageOrientationUp:
- transform = CGAffineTransformIdentity;
- break;
- case UIImageOrientationUpMirrored:
- transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
- transform = CGAffineTransformScale(transform, -1.0, 1.0);
- break;
- case UIImageOrientationDown:
- transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
- transform = CGAffineTransformRotate(transform, M_PI);
- break;
- case UIImageOrientationDownMirrored:
- transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
- transform = CGAffineTransformScale(transform, 1.0, -1.0);
- break;
- case UIImageOrientationLeftMirrored:
- boundHeight = bounds.size.height;
- bounds.size.height = bounds.size.width;
- bounds.size.width = boundHeight;
- transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
- transform = CGAffineTransformScale(transform, -1.0, 1.0);
- transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
- break;
- case UIImageOrientationLeft:
- boundHeight = bounds.size.height;
- bounds.size.height = bounds.size.width;
- bounds.size.width = boundHeight;
- transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
- transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
- break;
- case UIImageOrientationRightMirrored:
- boundHeight = bounds.size.height;
- bounds.size.height = bounds.size.width;
- bounds.size.width = boundHeight;
- transform = CGAffineTransformMakeScale(-1.0, 1.0);
- transform = CGAffineTransformRotate(transform, M_PI / 2.0);
- break;
- case UIImageOrientationRight:
- boundHeight = bounds.size.height;
- bounds.size.height = bounds.size.width;
- bounds.size.width = boundHeight;
- transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
- transform = CGAffineTransformRotate(transform, M_PI / 2.0);
- break;
- default:
- [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
- }
-
- UIGraphicsBeginImageContext(bounds.size);
- CGContextRef context = UIGraphicsGetCurrentContext();
- if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
- CGContextScaleCTM(context, -scaleRatio, scaleRatio);
- CGContextTranslateCTM(context, -height, 0);
- } else {
- CGContextScaleCTM(context, scaleRatio, -scaleRatio);
- CGContextTranslateCTM(context, 0, -height);
- }
- CGContextConcatCTM(context, transform);
- CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
- UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- NSLog(@"resize w%f,H%f",returnImage.size.width,returnImage.size.height);
- return returnImage;
- }
-
- + (UIImage*)imageWithMat:(const cv::Mat&) image andDeviceOrientation: (UIDeviceOrientation)orientation
- {
- UIImageOrientation imgOrientation = UIImageOrientationUp;
-
- switch (orientation)
- {
- case UIDeviceOrientationLandscapeLeft:
- imgOrientation =UIImageOrientationLeftMirrored; break;
-
- case UIDeviceOrientationLandscapeRight:
- imgOrientation = UIImageOrientationDown; break;
-
- case UIDeviceOrientationPortraitUpsideDown:
- imgOrientation = UIImageOrientationRightMirrored; break;
- case UIDeviceOrientationFaceUp:
- imgOrientation = UIImageOrientationRightMirrored; break;
-
- default:
- case UIDeviceOrientationPortrait:
- imgOrientation = UIImageOrientationRight; break;
- };
-
- return [Utility imageWithMat:image andImageOrientation:imgOrientation];
- }
-
- + (UIImage*)imageWithMat:(const cv::Mat&)image andImageOrientation:(UIImageOrientation)orientation;
- {
- cv::Mat rgbaView;
-
- if (image.channels() == 3)
- {
- cv::cvtColor(image, rgbaView, COLOR_BGR2BGRA);
- }
- else if (image.channels() == 4)
- {
- cv::cvtColor(image, rgbaView, COLOR_BGR2BGRA);
- }
- else if (image.channels() == 1)
- {
- cv::cvtColor(image, rgbaView, COLOR_GRAY2RGBA);
- }
-
- NSData *data = [NSData dataWithBytes:rgbaView.data length:rgbaView.elemSize() * rgbaView.total()];
-
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
-
- CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
-
- CGBitmapInfo bmInfo = kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big;
-
- // Creating CGImage from cv::Mat
- CGImageRef imageRef = CGImageCreate(rgbaView.cols, //width
- rgbaView.rows, //height
- 8, //bits per component
- 8 * rgbaView.elemSize(), //bits per pixel
- rgbaView.step.p[0], //bytesPerRow
- colorSpace, //colorspace
- bmInfo,// bitmap info
- provider, //CGDataProviderRef
- NULL, //decode
- false, //should interpolate
- kCGRenderingIntentDefault //intent
- );
-
- // Getting UIImage from CGImage
- UIImage *finalImage = [UIImage imageWithCGImage:imageRef scale:1 orientation:orientation];
- CGImageRelease(imageRef);
- CGDataProviderRelease(provider);
- CGColorSpaceRelease(colorSpace);
-
- return finalImage;
- }
-
- + (cv::Mat)cvMatFromUIImage:(UIImage *)image
- {
- CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
- CGFloat cols = image.size.width;
- CGFloat rows = image.size.height;
-
- cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels
-
- CGContextRef contextRef = CGBitmapContextCreate(cvMat.data, // Pointer to data
- cols, // Width of bitmap
- rows, // Height of bitmap
- 8, // Bits per component
- cvMat.step[0], // Bytes per row
- colorSpace, // Colorspace
- kCGImageAlphaNoneSkipLast |
- kCGBitmapByteOrderDefault); // Bitmap info flags
-
- CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
- CGContextRelease(contextRef);
- CGColorSpaceRelease(colorSpace);
- cv::Mat cvMat3(rows, cols, CV_8UC3); // 8 bits per component, 4 channels
- cv::cvtColor(cvMat, cvMat3,COLOR_RGBA2RGB);
-
- return cvMat3;
- }
-
- + (UIImage *)scaleAndRotateImageFrontCamera:(UIImage *)image
- {
- static int kMaxResolution = 640;
- CGImageRef imgRef = image.CGImage;
- CGFloat width = CGImageGetWidth(imgRef);
- CGFloat height = CGImageGetHeight(imgRef);
- CGAffineTransform transform = CGAffineTransformIdentity;
- CGRect bounds = CGRectMake( 0, 0, width, height);
- if (width > kMaxResolution || height > kMaxResolution) {
- CGFloat ratio = width/height;
- if (ratio > 1) {
- bounds.size.width = kMaxResolution;
- bounds.size.height = bounds.size.width / ratio;
- } else {
- bounds.size.height = kMaxResolution;
- bounds.size.width = bounds.size.height * ratio;
- }
- }
-
- CGFloat scaleRatio = bounds.size.width / width;
- CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
- CGFloat boundHeight;
- UIImageOrientation orient = image.imageOrientation;
- switch(orient) {
- case UIImageOrientationUp:
- transform = CGAffineTransformIdentity;
- break;
- case UIImageOrientationUpMirrored:
- transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
- transform = CGAffineTransformScale(transform, -1.0, 1.0);
- break;
- case UIImageOrientationDown:
- transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
- transform = CGAffineTransformRotate(transform, M_PI);
- break;
- case UIImageOrientationDownMirrored:
- transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
- transform = CGAffineTransformScale(transform, 1.0, -1.0);
- break;
- case UIImageOrientationLeftMirrored:
- boundHeight = bounds.size.height;
- bounds.size.height = bounds.size.width;
- bounds.size.width = boundHeight;
- transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
- transform = CGAffineTransformScale(transform, -1.0, 1.0);
- transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
- break;
- case UIImageOrientationLeft:
- boundHeight = bounds.size.height;
- bounds.size.height = bounds.size.width;
- bounds.size.width = boundHeight;
- transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
- transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
- break;
- case UIImageOrientationRight:
- case UIImageOrientationRightMirrored:
- boundHeight = bounds.size.height;
- bounds.size.height = bounds.size.width;
- bounds.size.width = boundHeight;
- transform = CGAffineTransformMakeScale(-1.0, 1.0);
- transform = CGAffineTransformRotate(transform, M_PI / 2.0);
- break;
- default:
- [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
- }
- UIGraphicsBeginImageContext( bounds.size );
- CGContextRef context = UIGraphicsGetCurrentContext();
- if ( orient == UIImageOrientationRight || orient == UIImageOrientationLeft ) {
- CGContextScaleCTM(context, -scaleRatio, scaleRatio);
- CGContextTranslateCTM(context, -height, 0);
- }
- else {
- CGContextScaleCTM(context, scaleRatio, -scaleRatio);
- CGContextTranslateCTM(context, 0, -height);
- }
- CGContextConcatCTM( context, transform );
- CGContextDrawImage( UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef );
- UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return returnImage;
- }
-
- @end
|