zoukankan      html  css  js  c++  java
  • ipad使用AVFoundation捕捉StillImage,图片比率问题

    AVCaptureSession设置的是

    AVCaptureSessionPresetPhoto,也就是New iPad摄像头最大分辨率2592*1936.但是capture得到的比率确实1936*2592.

    这里描述了相同的问题:

    http://stackoverflow.com/questions/6976213/why-does-capturing-images-with-avfoundation-give-me-480x640-images-when-the-pres

    我参照http://blog.logichigh.com/2008/06/05/uiimage-fix/得到了解决

    转换代码如下:

      1 UIImage *scaleAndRotateImage(UIImage *image)  
      2 {  
      3     int kMaxResolution = 2592; // Or whatever  
      4     
      5     CGImageRef imgRef = image.CGImage;  
      6     
      7     CGFloat width = CGImageGetWidth(imgRef);  
      8     CGFloat height = CGImageGetHeight(imgRef);  
      9     
     10     CGAffineTransform transform = CGAffineTransformIdentity;  
     11     CGRect bounds = CGRectMake(0, 0, width, height);  
     12     if (width > kMaxResolution || height > kMaxResolution) {  
     13         CGFloat ratio = width/height;  
     14         if (ratio > 1) {  
     15             bounds.size.width = kMaxResolution;  
     16             bounds.size.height = bounds.size.width / ratio;  
     17         }  
     18         else {  
     19             bounds.size.height = kMaxResolution;  
     20             bounds.size.width = bounds.size.height * ratio;  
     21         }  
     22     }  
     23     
     24     CGFloat scaleRatio = bounds.size.width / width;  
     25     CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));  
     26     CGFloat boundHeight;  
     27     UIImageOrientation orient = image.imageOrientation;  
     28     switch(orient) {  
     29             
     30         case UIImageOrientationUp: //EXIF = 1  
     31             transform = CGAffineTransformIdentity;  
     32             break;  
     33             
     34         case UIImageOrientationUpMirrored: //EXIF = 2  
     35             transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);  
     36             transform = CGAffineTransformScale(transform, -1.0, 1.0);  
     37             break;  
     38             
     39         case UIImageOrientationDown: //EXIF = 3  
     40             transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);  
     41             transform = CGAffineTransformRotate(transform, M_PI);  
     42             break;  
     43             
     44         case UIImageOrientationDownMirrored: //EXIF = 4  
     45             transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);  
     46             transform = CGAffineTransformScale(transform, 1.0, -1.0);  
     47             break;  
     48             
     49         case UIImageOrientationLeftMirrored: //EXIF = 5  
     50             boundHeight = bounds.size.height;  
     51             bounds.size.height = bounds.size.width;  
     52             bounds.size.width = boundHeight;  
     53             transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);  
     54             transform = CGAffineTransformScale(transform, -1.0, 1.0);  
     55             transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);  
     56             break;  
     57             
     58         case UIImageOrientationLeft: //EXIF = 6  
     59             boundHeight = bounds.size.height;  
     60             bounds.size.height = bounds.size.width;  
     61             bounds.size.width = boundHeight;  
     62             transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);  
     63             transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);  
     64             break;  
     65             
     66         case UIImageOrientationRightMirrored: //EXIF = 7  
     67             boundHeight = bounds.size.height;  
     68             bounds.size.height = bounds.size.width;  
     69             bounds.size.width = boundHeight;  
     70             transform = CGAffineTransformMakeScale(-1.0, 1.0);  
     71             transform = CGAffineTransformRotate(transform, M_PI / 2.0);  
     72             break;  
     73             
     74         case UIImageOrientationRight: //EXIF = 8  
     75             boundHeight = bounds.size.height;  
     76             bounds.size.height = bounds.size.width;  
     77             bounds.size.width = boundHeight;  
     78             transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);  
     79             transform = CGAffineTransformRotate(transform, M_PI / 2.0);  
     80             break;  
     81             
     82         default:  
     83             [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];  
     84             
     85     }  
     86     
     87     UIGraphicsBeginImageContext(bounds.size);  
     88     
     89     CGContextRef context = UIGraphicsGetCurrentContext();  
     90     
     91     if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {  
     92         CGContextScaleCTM(context, -scaleRatio, scaleRatio);  
     93         CGContextTranslateCTM(context, -height, 0);  
     94     }  
     95     else {  
     96         CGContextScaleCTM(context, scaleRatio, -scaleRatio);  
     97         CGContextTranslateCTM(context, 0, -height);  
     98     }  
     99     
    100     CGContextConcatCTM(context, transform);  
    101     
    102     CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);  
    103     UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();  
    104     UIGraphicsEndImageContext();  
    105     
    106     return imageCopy;  
    107 }  
  • 相关阅读:
    Springboot操作域对象
    Thymeleaf的条件判断和迭代遍历
    Thymeleaf的字符串与变量输出的操作
    window 2003 实现多用户远程登录
    非常漂亮js动态球型云标签特效代码
    Echarts运用
    sass、less和stylus的安装使用和入门实践
    java,js 解unicode
    Java上传下载excel、解析Excel、生成Excel
    小知识点应用
  • 原文地址:https://www.cnblogs.com/cokecoffe/p/2594736.html
Copyright © 2011-2022 走看看