多个AVCaptureVideoPreviewLayers

时间:2012-07-16 22:47:59

标签: avfoundation avcapturesession

如何一次在屏幕上显示多个相机预览? 如果我开始一个新的AVCaptureSession,另一个停止。如果我使用与另一个会话相同的会话启动新的AVCaptureVideoPreviewLayer,则另一个AVCaptureVideoPreviewLayer将停止显示相机源。如何克服此问题并在屏幕上的两个不同位置显示相机进纸?

1 个答案:

答案 0 :(得分:0)

// in this method get CameraView Buffer Image and set Preview view

 - (void)captureOutput:(AVCaptureOutput *)captureOutput
   didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
   fromConnection:(AVCaptureConnection *)connection
{
@autoreleasepool
{
    NSLog(@"Capturecall");
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    /*Lock the image buffer*/
    CVPixelBufferLockBaseAddress(imageBuffer,0);
    /*Get information about the image*/
    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);

    /*Create a CGImageRef from the CVImageBufferRef*/
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    CGImageRef newImage = CGBitmapContextCreateImage(newContext);
    /*We release some components*/
    CGContextRelease(newContext);
    CGColorSpaceRelease(colorSpace);
    UIImage *image= [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationRight];
    CGImageRelease(newImage);
    dispatch_async(dispatch_get_main_queue(),
                   ^{

                     UIImage * blurredImage=[imagefilters GaussianBlurWithImage:image AndRadius:_brightnessValue];
                       [self.backgroundpreview setImage:blurredImage];

                    });


    CVPixelBufferUnlockBaseAddress(imageBuffer,0);
}}