【问题标题】:AVCaptureSession with multiple previews具有多个预览的 AVCaptureSession
【发布时间】:2013-05-14 12:24:49
【问题描述】:

我有一个使用 AVCaptureVideoPreviewLayer 运行的 AVCaptureSession。

我可以看到视频,所以我知道它正在工作。

但是,我想要一个集合视图,并在每个单元格中添加一个预览层,以便每个单元格显示视频的预览。

如果我尝试将预览层传递到单元格中并将其添加为子层,那么它将从其他单元格中删除该层,因此它一次只显示在一个单元格中。

还有其他(更好的)方法吗?

【问题讨论】:

    标签: ios avfoundation


    【解决方案1】:

    我遇到了需要同时显示多个实时视图的问题。上面使用 UIImage 的答案对于我需要的东西来说太慢了。以下是我找到的两个解决方案:

    1。 CAReplicatorLayer

    第一个选项是使用 CAReplicatorLayer 自动复制图层。正如文档所说,它将自动创建“......其子层(源层)的指定数量的副本,每个副本都可能应用几何、时间和颜色转换。”

    如果除了简单的几何或颜色转换(Think Photo Booth)之外没有很多与实时预览的交互,这将非常有用。我经常看到 CAReplicatorLayer 被用作创建“反射”效果的一种方式。

    这里是一些复制 CACaptureVideoPreviewLayer 的示例代码:

    初始化 AVCaptureVideoPreviewLayer

    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    [previewLayer setFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height / 4)];
    

    初始化 CAReplicatorLayer 并设置属性

    注意:这将复制实时预览层 次。

    NSUInteger replicatorInstances = 4;
    
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    replicatorLayer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / replicatorInstances);
    replicatorLayer.instanceCount = instances;
    replicatorLayer.instanceTransform = CATransform3DMakeTranslation(0.0, self.view.bounds.size.height / replicatorInstances, 0.0);
    

    添加图层

    注意:根据我的经验,您需要将要复制的层添加到 CAReplicatorLayer 作为子层。

    [replicatorLayer addSublayer:previewLayer];
    [self.view.layer addSublayer:replicatorLayer];
    

    缺点

    使用 CAReplicatorLayer 的一个缺点是它处理所有层复制的放置。因此,它将对每个实例应用任何集合转换,并且它都将包含在其自身中。 例如没有办法在两个单独的单元格上复制 AVCaptureVideoPreviewLayer。


    2。手动渲染 SampleBuffer

    这种方法虽然有点复杂,但解决了上面提到的 CAReplicatorLayer 的缺点。通过手动渲染实时预览,您可以渲染任意数量的视图。当然,性能可能会受到影响。

    注意:可能还有其他方法可以渲染 SampleBuffer,但我选择 OpenGL 是因为它的性能。代码的灵感来自CIFunHouse

    这是我的实现方式:

    2.1 上下文和会话

    设置 OpenGL 和 CoreImage 上下文

    _eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    
    // Note: must be done after the all your GLKViews are properly set up
    _ciContext = [CIContext contextWithEAGLContext:_eaglContext
                                           options:@{kCIContextWorkingColorSpace : [NSNull null]}];
    

    调度队列

    此队列将用于会话和委托。

    self.captureSessionQueue = dispatch_queue_create("capture_session_queue", NULL);
    

    初始化你的 AVSession 和 AVCaptureVideoDataOutput

    注意:我已删除所有设备功能检查以使其更具可读性。

    dispatch_async(self.captureSessionQueue, ^(void) {
        NSError *error = nil;
    
        // get the input device and also validate the settings
        NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    
        AVCaptureDevice *_videoDevice = nil;
        if (!_videoDevice) {
            _videoDevice = [videoDevices objectAtIndex:0];
        }
    
        // obtain device input
        AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:self.videoDevice error:&error];
    
        // obtain the preset and validate the preset
        NSString *preset = AVCaptureSessionPresetMedium;
    
        // CoreImage wants BGRA pixel format
        NSDictionary *outputSettings = @{(id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA)};
    
        // create the capture session
        self.captureSession = [[AVCaptureSession alloc] init];
        self.captureSession.sessionPreset = preset;
        :
    

    注意:以下代码是“魔法代码”。这是我们创建并向 AVSession 添加 DataOutput 的地方,因此我们可以使用委托截取相机帧。这是我需要弄清楚如何解决问题的突破。

        :
        // create and configure video data output
        AVCaptureVideoDataOutput *videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
        videoDataOutput.videoSettings = outputSettings;
        [videoDataOutput setSampleBufferDelegate:self queue:self.captureSessionQueue];
    
        // begin configure capture session
        [self.captureSession beginConfiguration];
    
        // connect the video device input and video data and still image outputs
        [self.captureSession addInput:videoDeviceInput];
        [self.captureSession addOutput:videoDataOutput];
    
        [self.captureSession commitConfiguration];
    
        // then start everything
        [self.captureSession startRunning];
    });
    

    2.2 OpenGL 视图

    我们正在使用 GLKView 来呈现我们的实时预览。所以如果你想要 4 个实时预览,那么你需要 4 个 GLKView。

    self.livePreviewView = [[GLKView alloc] initWithFrame:self.bounds context:self.eaglContext];
    self.livePreviewView = NO;
    

    由于后置摄像头的原生视频图像位于 UIDeviceOrientationLandscapeLeft 中(即主页按钮在右侧),我们需要应用顺时针 90 度变换,以便我们可以像在风景中一样绘制视频预览面向视图;如果您使用前置摄像头并且想要进行镜像预览(以便用户在镜子中看到自己),则需要应用额外的水平翻转(通过将 CGAffineTransformMakeScale(-1.0, 1.0) 连接到旋转变换)

    self.livePreviewView.transform = CGAffineTransformMakeRotation(M_PI_2);
    self.livePreviewView.frame = self.bounds;    
    [self addSubview: self.livePreviewView];
    

    绑定帧缓冲区以获取帧缓冲区的宽度和高度。 CIContext 在绘制到 GLKView 时使用的边界以像素(而不是点)为单位,因此需要从帧缓冲区的宽度和高度中读取。

    [self.livePreviewView bindDrawable];
    

    此外,由于我们将访问另一个队列 (_captureSessionQueue) 中的边界,因此我们希望获得这条信息,这样我们就不会从另一个线程/队列访问 _videoPreviewView 的属性。

    _videoPreviewViewBounds = CGRectZero;
    _videoPreviewViewBounds.size.width = _videoPreviewView.drawableWidth;
    _videoPreviewViewBounds.size.height = _videoPreviewView.drawableHeight;
    
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_2);        
    
        // *Horizontally flip here, if using front camera.*
    
        self.livePreviewView.transform = transform;
        self.livePreviewView.frame = self.bounds;
    });
    

    注意:如果您使用前置摄像头,您可以像这样水平翻转实时预览:

    transform = CGAffineTransformConcat(transform, CGAffineTransformMakeScale(-1.0, 1.0));
    

    2.3 委托实现

    在我们设置好 Contexts、Sessions 和 GLKViews 之后,我们现在可以从 AVCaptureVideoDataOutputSampleBufferDelegate 方法 captureOutput:didOutputSampleBuffer:fromConnection:

    渲染到我们的视图
    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
    {
        CMFormatDescriptionRef formatDesc = CMSampleBufferGetFormatDescription(sampleBuffer);
    
        // update the video dimensions information
        self.currentVideoDimensions = CMVideoFormatDescriptionGetDimensions(formatDesc);
    
        CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
        CIImage *sourceImage = [CIImage imageWithCVPixelBuffer:(CVPixelBufferRef)imageBuffer options:nil];
    
        CGRect sourceExtent = sourceImage.extent;
        CGFloat sourceAspect = sourceExtent.size.width / sourceExtent.size.height;
    

    您需要对每个 GLKView 及其 videoPreviewViewBounds 有一个引用。为方便起见,我假设它们都包含在 UICollectionViewCell 中。您将需要针对您自己的用例进行更改。

        for(CustomLivePreviewCell *cell in self.livePreviewCells) {
            CGFloat previewAspect = cell.videoPreviewViewBounds.size.width  / cell.videoPreviewViewBounds.size.height;
    
            // To maintain the aspect radio of the screen size, we clip the video image
            CGRect drawRect = sourceExtent;
            if (sourceAspect > previewAspect) {
                // use full height of the video image, and center crop the width
                drawRect.origin.x += (drawRect.size.width - drawRect.size.height * previewAspect) / 2.0;
                drawRect.size.width = drawRect.size.height * previewAspect;
            } else {
                // use full width of the video image, and center crop the height
                drawRect.origin.y += (drawRect.size.height - drawRect.size.width / previewAspect) / 2.0;
                drawRect.size.height = drawRect.size.width / previewAspect;
            }
    
            [cell.livePreviewView bindDrawable];
    
            if (_eaglContext != [EAGLContext currentContext]) {
                [EAGLContext setCurrentContext:_eaglContext];
            }
    
            // clear eagl view to grey
            glClearColor(0.5, 0.5, 0.5, 1.0);
            glClear(GL_COLOR_BUFFER_BIT);
    
            // set the blend mode to "source over" so that CI will use that
            glEnable(GL_BLEND);
            glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    
            if (sourceImage) {
                [_ciContext drawImage:sourceImage inRect:cell.videoPreviewViewBounds fromRect:drawRect];
            }
    
            [cell.livePreviewView display];
        }
    }
    

    此解决方案允许您使用 OpenGL 渲染从 AVCaptureVideoDataOutputSampleBufferDelegate 接收到的图像缓冲区,从而获得任意数量的实时预览。

    3。示例代码

    这是我和两个灵魂一起扔的一个 github 项目:https://github.com/JohnnySlagle/Multiple-Camera-Feeds

    【讨论】:

    • @souvickcse 希望对您有所帮助! :)
    • 经过几个小时的搜索,终于找到了这个。谢谢。
    • @Johnny 这真的很有帮助。但我想在右上角显示 1 个全屏相机视图和一个(100,100)。我相信我可以使用 OpenGL 实现它,但还没有成功。有什么想法吗?
    • @Akshit Zaveri 我有同样的问题,如果你有任何解决方案,请分享。谢谢
    • 在最后一段代码的 for 循环开头添加 [cell.livePreviewView deleteDrawable],这样您就可以拥有多个大小的实时预览。 @AkshitZaveri
    【解决方案2】:

    只需将预览层的内容设置为另一个CALayer:

    CGImageRef cgImage = (__bridge CGImage)self.previewLayer.contents; self.duplicateLayer.contents = (__bridge id)cgImage;

    您可以使用任何 Metal 或 OpenGL 层的内容来执行此操作。我的内存使用量或 CPU 负载也没有增加。你没有复制任何东西,只是一个小指针。这些其他“解决方案”并非如此。

    我有一个示例项目,您可以下载该项目,它可以从单个相机源同时显示 20 个预览图层。每一层都有不同的效果应用于我们的。

    您可以观看应用运行的视频,也可以在以下网址下载源代码:

    https://demonicactivity.blogspot.com/2017/05/developer-iphone-video-camera-wall.html?m=1

    【讨论】:

    • 这种技术在 Swift 中是否可行,你知道吗?
    • @MichaelForrest 你可以;但是,你不需要用 Swift 重写它,即使你的应用程序的其余部分是用 Swift 编写的。 “Swift 与 Objective-C 完全兼容,因此开发人员可以在两种语言之间进行交互,创建混合语言应用程序,并利用 Swift 的 Cocoa Touch 类和 Objective-C 的 Swift 类。” upwork.com/resources/…
    【解决方案3】:

    在 iOS 13 上使用 Swift 5 时,我实现了 @Ushan87 答案的一个更简单的版本。出于测试目的,我在现有的 AVCaptureVideoPreviewLayer 上拖了一个新的小型 UIImageView。在该窗口的 ViewController 中,我为新视图添加了一个 IBOutlet 和一个变量来描述正在使用的相机的正确方向:

        @IBOutlet var testView: UIImageView!
        private var extOrientation: UIImage.Orientation = .up
    

    然后我实现了 AVCaptureVideoDataOutputSampleBufferDelegate,如下所示:

    // MARK: - AVCaptureVideoDataOutputSampleBufferDelegate
    extension CameraViewController: AVCaptureVideoDataOutputSampleBufferDelegate {
        func captureOutput(_ captureOutput: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
    
            let imageBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!
            let ciimage : CIImage = CIImage(cvPixelBuffer: imageBuffer)
            let image : UIImage = self.convert(cmage: ciimage)
    
            DispatchQueue.main.sync(execute: {() -> Void in
                testView.image = image
            })
    
        }
    
        // Convert CIImage to CGImage
        func convert(cmage:CIImage) -> UIImage
        {
            let context:CIContext = CIContext.init(options: nil)
            let cgImage:CGImage = context.createCGImage(cmage, from: cmage.extent)!
            let image:UIImage = UIImage.init(cgImage: cgImage, scale: 1.0, orientation: extOrientation)
            return image
        }
    

    就我而言,性能很好。我没有注意到新视图有任何滞后。

    【讨论】:

      【解决方案4】:

      实现 AVCaptureSession 委托方法

      - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
      

      使用它,您可以获得每个视频帧的样本缓冲区输出。使用缓冲区输出,您可以使用以下方法创建图像。

      - (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer 
      {
          // Get a CMSampleBuffer's Core Video image buffer for the media data
          CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
          // Lock the base address of the pixel buffer
          CVPixelBufferLockBaseAddress(imageBuffer, 0); 
      
          // Get the number of bytes per row for the pixel buffer
          void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer); 
      
          // Get the number of bytes per row for the pixel buffer
          size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
          // Get the pixel buffer width and height
          size_t width = CVPixelBufferGetWidth(imageBuffer); 
          size_t height = CVPixelBufferGetHeight(imageBuffer); 
      
          // Create a device-dependent RGB color space
          CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
      
          // Create a bitmap graphics context with the sample buffer data
          CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, 
                                                       bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 
          // Create a Quartz image from the pixel data in the bitmap graphics context
          CGImageRef quartzImage = CGBitmapContextCreateImage(context); 
          // Unlock the pixel buffer
          CVPixelBufferUnlockBaseAddress(imageBuffer,0);
      
          // Free up the context and color space
          CGContextRelease(context); 
          CGColorSpaceRelease(colorSpace);
      
          // Create an image object from the Quartz image
            UIImage *image = [UIImage imageWithCGImage:quartzImage scale:1.0 orientation:UIImageOrientationRight];
      
          // Release the Quartz image
          CGImageRelease(quartzImage);
      
          return (image);
      }
      

      所以你可以在你的视图中添加几个 imageViews 并在我之前提到的委托方法中添加这些行:

      UIImage *image = [self imageFromSampleBuffer:sampleBuffer];
      imageViewOne.image = image;
      imageViewTwo.image = image;
      

      【讨论】:

      • 太棒了!谢谢!我没有使用这种确切的方法。我已经有多个使用委托方法显示输出的视图。您的帖子帮助我意识到我可以使用这种方法并设置静止图像输出来拍照。因此,现在我与一位代表进行了一次会议,该代表发布了更新“预览”的通知和拍摄完整照片的输出:D 太棒了!谢谢。
      • 实际上该方法不是来自 AVCaptureSession 的委托方法,而是来自 AVCaptureVideoDataOutputSampleBufferDelegate 的委托。我必须添加这些行才能使其正常工作:AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init]; [captureOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()]; [_captureSession addOutput:captureOutput];
      【解决方案5】:

      您不能有多个预览。正如 Apple AVFundation 所说,只有一个输出流。我尝试了很多方法,但你就是做不到。

      【讨论】:

      • @Fogmeister 检查我的答案,伙计
      • +1 表示正确但不被接受,因为有一种方法可以使用会话的数据委托方法。
      • 虽然您可能无法拥有多个 AVCaptureVideoPreviewLayers 或 AVCaptureSessions,但您可以使用 AVCaptureAudioDataOutputSampleBufferDelegate 来操作您选择的样本缓冲区。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多