AVCaptureSession with multiple previews

后端 未结 5 1070
花落未央
花落未央 2020-11-28 21:48

I have an AVCaptureSession running with an AVCaptureVideoPreviewLayer.

I can see the video so I know it\'s working.

However, I\'d like to have a collection v

5条回答
  •  春和景丽
    2020-11-28 22:28

    implement the AVCaptureSession delegate method which is

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

    using this you can get the sample buffer output of each and every video frame. Using the buffer output you can create an image using the method below.

    - (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);
    }
    

    so you can add several imageViews to your view and add these lines inside the delegate method that i have mentioned before:

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

提交回复
热议问题