【问题标题】:Get image from AVCaptureVideoPreviewLayer从 AVCaptureVideoPreviewLayer 获取图像
【发布时间】:2014-08-04 08:36:56
【问题描述】:

我基本上是在克隆Cropping a captured image exactly to how it looks in AVCaptureVideoPreviewLayer,因为询问原始发帖人是否找到了解决方案不是“答案”,我还无法发表评论,因为我没有足够的声誉......

我正在构建的应用程序将始终处于纵向模式,因为在这种情况下旋转并不重要。 我有一个 AVCaptureSession,其 AVCaptureVideoPreviewLayer 连接到大小为 320x240 的 UIView,该 UIView 位于顶部布局指南中。

我已经捕获了输入,但我收到的图像是倾斜的,并且显示的内容比我正在显示的部分要多得多。如何仅捕获 AVCaptureVideoPreviewLayer 中显示的区域?

【问题讨论】:

    标签: objective-c ios7 avcapturesession


    【解决方案1】:

    看看AVCaptureVideoPreviewLayers

    -(CGRect)metadataOutputRectOfInterestForRect:(CGRect)layerRect
    

    此方法让您可以轻松地将图层的可见 CGRect 转换为实际的相机输出。

    一个警告:物理相机不是“顶面朝上”安装的,而是向右旋转 90 度。 (因此,如果您将 iPhone - Home 按钮放在右侧,相机实际上是正面朝上的)。

    记住这一点,您必须转换上述方法提供的 CGRect,才能将图像裁剪成屏幕上的样子。

    例子:

    CGRect visibleLayerFrame = THE ACTUAL VISIBLE AREA IN THE LAYER FRAME
    CGRect metaRect = [self.previewView.layer metadataOutputRectOfInterestForRect:visibleLayerFrame];
    
    
    CGSize originalSize = [originalImage size];
    
    if (UIInterfaceOrientationIsPortrait(_snapInterfaceOrientation)) {
        // For portrait images, swap the size of the image because
        // here, the output image is actually rotated relative to what you see on screen.
    
        CGFloat temp = originalSize.width;
        originalSize.width = originalSize.height;
        originalSize.height = temp;
    }
    
    // metaRect is fractional, that's why we multiply here
    
    CGRect cropRect;
    
    cropRect.origin.x = metaRect.origin.x * originalSize.width;
    cropRect.origin.y = metaRect.origin.y * originalSize.height;
    cropRect.size.width = metaRect.size.width * originalSize.width;
    cropRect.size.height = metaRect.size.height * originalSize.height;
    
    cropRect = CGRectIntegral(cropRect);
    

    这可能有点令人困惑,但让我真正理解的是:

    按住您的设备“Home Button right” -> 您会看到 x 轴实际上位于 iPhone 的“高度”上,而 y 轴位于 iPhone 的“宽度”上。这就是为什么对于肖像图像,您必须交换大小;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-18
      相关资源
      最近更新 更多