2012-12-20 1 views
5

비디오 피드 (기본적으로 일시 중지 또는 '스냅 샷'기능)에서 정지 이미지를 가져 오려고합니다. 내 프로젝트는 Benjamin Loulier's template을 사용하여 설정됩니다. 제 문제는 prevLayer (AVCaptureVideoPreviewLayer)을 통해 화면에 컬러 비디오를 표시하고 있는데, 비디오 설정을 그레이 스케일로 설정했기 때문에 UIImage를 customLayer (일반 CALayer)에서 가져올 수 없습니다.AVCaptureVideoPreviewLayer의 UIImage

here 주어진이 기능을 사용해 보았습니다. 그러나 이것은 어리숙한 이유로 AVCaptureVideoPreviewLayer에서 작동하지 않습니다 (투명/투명 표시). 누군가 UIImage로 AVCaptureVideoPreviewLayer의 내용을 저장하는 방법을 알고 있습니까?

+0

나는 이것도 다루고 있습니다. 팀의 대답은 정확할 수도 있지만 여전히 레이어가 "깜박"하지 않고 레이어가 비어 있지 않은 시점이 있어야합니다. 이거 알아 냈어? '- (void) captureOutput : (AVCaptureOutput *) captureOutput didOutputSampleBuffer :(CMSampleBufferRef) sampleBuffer fromConnection : (AVCaptureConnection *) connection'에서 이미지 데이터를 가져 오려고 시도한 행운은 없었지만, 대답을 게시하십시오. – Jonny

+0

좋습니다. 캡쳐 아웃에서 'UIImage'를 올바르게 캡쳐해야합니다. https://developer.apple.com/library/ios/#qa/qa1702/_index.html 답변으로 게시 됨. – Jonny

+0

어떻게 'AVCaptureVideoPreviewLayer'를 사용하여 그레이 스케일을 맞춤형 카메라로 설정할 수 있습니까? –

답변

3

확인이 내 대답, https://developer.apple.com/library/ios/#qa/qa1702/_index.html

한 노트의 예의입니다. minFrameDuration은 iOS 5.0부터 사용되지 않습니다. 그 이유가 확실하지 않거나 교체가있을 경우.

#import <AVFoundation/AVFoundation.h> 

// Create and configure a capture session and start it running 
- (void)setupCaptureSession 
{ 
    NSError *error = nil; 

    // Create the session 
    AVCaptureSession *session = [[AVCaptureSession alloc] init]; 

    // Configure the session to produce lower resolution video frames, if your 
    // processing algorithm can cope. We'll specify medium quality for the 
    // chosen device. 
    session.sessionPreset = AVCaptureSessionPresetMedium; 

    // Find a suitable AVCaptureDevice 
    AVCaptureDevice *device = [AVCaptureDevice 
          defaultDeviceWithMediaType:AVMediaTypeVideo]; 

    // Create a device input with the device and add it to the session. 
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device 
                    error:&error]; 
    if (!input) { 
     // Handling the error appropriately. 
    } 
    [session addInput:input]; 

    // Create a VideoDataOutput and add it to the session 
    AVCaptureVideoDataOutput *output = [[[AVCaptureVideoDataOutput alloc] init] autorelease]; 
    [session addOutput:output]; 

    // Configure your output. 
    dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL); 
    [output setSampleBufferDelegate:self queue:queue]; 
    dispatch_release(queue); 

    // Specify the pixel format 
    output.videoSettings = 
       [NSDictionary dictionaryWithObject: 
        [NSNumber numberWithInt:kCVPixelFormatType_32BGRA] 
        forKey:(id)kCVPixelBufferPixelFormatTypeKey]; 


    // If you wish to cap the frame rate to a known value, such as 15 fps, set 
    // minFrameDuration. 
    output.minFrameDuration = CMTimeMake(1, 15); 

    // Start the session running to start the flow of data 
    [session startRunning]; 

    // Assign session to an ivar. 
    [self setSession:session]; 
} 

// Delegate routine that is called when a sample buffer was written 
- (void)captureOutput:(AVCaptureOutput *)captureOutput 
     didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
     fromConnection:(AVCaptureConnection *)connection 
{ 
    // Create a UIImage from the sample buffer data 
    UIImage *image = [self imageFromSampleBuffer:sampleBuffer]; 

    < Add your code here that uses the image > 

} 

// Create a UIImage from sample buffer data 
- (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]; 

    // Release the Quartz image 
    CGImageRelease(quartzImage); 

    return (image); 
}