【问题标题】:Cropping image captured by AVCaptureSession裁剪由 AVCaptureSession 捕获的图像
【发布时间】:2011-04-25 16:37:18
【问题描述】:

我正在编写一个 iPhone 应用程序,它使用 AVFoundation 来拍照并裁剪它。 该应用程序类似于 QR 码阅读器:它使用带有叠加层的 AVCaptureVideoPreviewLayer。 覆盖层有一个正方形。我想裁剪图像,因此裁剪后的图像正是用户在正方形内放置的内容。

预览层有重力 AVLayerVideoGravityResizeAspectFill。

看起来相机实际捕捉到的内容与用户在预览层中看到的不完全一样。这意味着我需要从预览坐标系移动到捕获的图像坐标系,以便裁剪图像。为此,我认为我需要以下参数: 1. 视图大小和捕获的图像大小之间的比例。 2. 说明捕获图像的哪个部分与预览层中显示的内容相匹配的信息。

有谁知道我如何获得此信息,或者是否有其他方法来裁剪图像。

(p.s. 不能选择截取预览截图,据我了解,这可能会导致应用被拒绝)。

提前谢谢你

【问题讨论】:

    标签: iphone avfoundation


    【解决方案1】:

    希望这符合您的要求

    - (UIImage *)cropImage:(UIImage *)image to:(CGRect)cropRect andScaleTo:(CGSize)size {
        UIGraphicsBeginImageContext(size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGImageRef subImage = CGImageCreateWithImageInRect([image CGImage], cropRect);
        NSLog(@"---------");     
        NSLog(@"*cropRect.origin.y=%f",cropRect.origin.y);
        NSLog(@"*cropRect.origin.x=%f",cropRect.origin.x);
    
        NSLog(@"*cropRect.size.width=%f",cropRect.size.width);     
        NSLog(@"*cropRect.size.height=%f",cropRect.size.height);     
    
        NSLog(@"---------");     
    
        NSLog(@"*size.width=%f",size.width);     
        NSLog(@"*size.height=%f",size.height);     
    
        CGRect myRect = CGRectMake(0.0f, 0.0f, size.width, size.height);
        CGContextScaleCTM(context, 1.0f, -1.0f);
        CGContextTranslateCTM(context, 0.0f, -size.height);
        CGContextDrawImage(context, myRect, subImage);
        UIImage* croppedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        CGImageRelease(subImage);     
    
        return croppedImage;
    }
    

    【讨论】:

    【解决方案2】:

    你可以使用 AVFoundation 的这个 api:AVMakeRectWithAspectRatioInsideRect

    它将返回边界区域中图像的裁剪区域,苹果文档在这里: https://developer.apple.com/library/ios/Documentation/AVFoundation/Reference/AVFoundation_Functions/Reference/reference.html

    【讨论】:

      【解决方案3】:

      我觉得就这么简单

      - (CGRect)computeCropRect:(CGImageRef)cgImageRef
      {
          static CGFloat cgWidth = 0;
          static CGFloat cgHeight = 0;
      
          static CGFloat viewWidth = 320;
      
          if(cgWidth == 0)
              cgWidth = CGImageGetWidth(cgImageRef);
      
          if(cgHeight == 0)
              cgHeight = CGImageGetHeight(cgImageRef);
      
          CGRect cropRect;
      
          // Only based on width
          cropRect.origin.x = cropRect.origin.y = kMargin * cgWidth / viewWidth;
          cropRect.size.width = cropRect.size.height = kSquareSize * cgWidth / viewWidth;
      
          return cropRect;
      }
      

      kMargin 和 kSquareSize(在我的例子中是 20 点和 280 点)分别是边距和扫描区域

      然后进行裁剪

      CGRect cropRect = [self computeCropRect:cgCapturedImageRef];
      CGImageRef croppedImageRef = CGImageCreateWithImageInRect(cgCapturedImageRef, cropRect);
      

      【讨论】:

        猜你喜欢
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-06
        • 2022-06-14
        • 1970-01-01
        • 1970-01-01
        • 2012-08-15
        相关资源
        最近更新 更多