2010-12-14 5 views
6

나는 며칠 동안 내 머리를 두드렸다.AVCaptureVideoPreviewLayer 위에 직사각형을 그릴 수 있습니까?

CALayer (AVCaptureVideoPreviewLayer) 위에 직사각형을 그려야하는데, 이것은 iPhone4의 카메라에서 가져온 비디오 피드 일뿐입니다.

여기가 내 설정의 일부입니다.

//(in function for initialization) 

     -(void)initDevices { 
      AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput     deviceInputWithDevice:[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo] error:nil]; 

      AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init]; 
      captureOutput.alwaysDiscardsLateVideoFrames = YES; 
      captureOutput.minFrameDuration = CMTimeMake(1, 30); 
      dispatch_queue_t queue; 
      queue = dispatch_queue_create("cameraQueue", NULL); 
      [captureOutput setSampleBufferDelegate:self queue:queue]; 
      dispatch_release(queue); 

      NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey; 
      NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]; 
      NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key]; 
      [captureOutput setVideoSettings:videoSettings]; 
      self.captureSession = [[AVCaptureSession alloc] init]; 
      [self.captureSession addInput:captureInput]; 
      [self.captureSession addOutput:captureOutput]; 
      [self.captureSession setSessionPreset:AVCaptureSessionPresetHigh]; 
      self.prevLayer = [AVCaptureVideoPreviewLayer layerWithSession: self.captureSession]; 
      self.prevLayer.frame = CGRectMake(0, 0, 400, 400); 
      self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
      self.prevLayer.delegate = self; 
      [self.view.layer addSublayer: self.prevLayer]; 
    } 

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

     [self performSelectorOnMainThread:@selector(drawme) withObject:nil waitUntilDone:YES]; 
    } 

    - (void)drawme { 
[self.prevLayer setNeedsDisplay]; 
    } 

    //delegate function that draws to a CALayer 
    - (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx { 
    NSLog(@"hello layer!"); 
    CGContextSetRGBFillColor (ctx, 1, 0, 0, 1); 
      CGContextFillRect (ctx, CGRectMake (0, 0, 200, 100)); 
    } 

이 경우에도 가능합니까? 현재 코드에서 "hello layer"인쇄를 얻지 만 카메라 피드에 채워진 사각형이 없습니다.

도움이 될 것입니다. :)

답변

1

다른 레이어를 AVCaptureVideoPreviewLayer에 추가해야하고 예제 코드를 수정해야한다고 생각합니다. 너도해볼 수있어.

//(in function for initialization) 

    -(void)initDevices { 
     AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput     deviceInputWithDevice:[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo] error:nil]; 

     AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init]; 
     captureOutput.alwaysDiscardsLateVideoFrames = YES; 
     captureOutput.minFrameDuration = CMTimeMake(1, 30); 
     dispatch_queue_t queue; 
     queue = dispatch_queue_create("cameraQueue", NULL); 
     [captureOutput setSampleBufferDelegate:self queue:queue]; 
     dispatch_release(queue); 

     NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey; 
     NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]; 
     NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key]; 
     [captureOutput setVideoSettings:videoSettings]; 
     self.captureSession = [[AVCaptureSession alloc] init]; 
     [self.captureSession addInput:captureInput]; 
     [self.captureSession addOutput:captureOutput]; 
     [self.captureSession setSessionPreset:AVCaptureSessionPresetHigh]; 
     self.prevLayer = [AVCaptureVideoPreviewLayer layerWithSession: self.captureSession]; 
     self.prevLayer.frame = CGRectMake(0, 0, 400, 400); 
     self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
     [self.view.layer addSublayer:self.prevLayer]; 

     self.drawLayer = [CAShapeLayer layer]; 
     CGRect parentBox = [self.captureVideoPreviewLayer frame]; 
     [self.drawLayer setFrame:parentBox]; 
     [self.drawLayer setDelegate:self]; 
     [self.drawLayer setNeedsDisplay]; 
     [self.captureVideoPreviewLayer addSublayer:self.drawLayer]; 
} 

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

    [self performSelectorOnMainThread:@selector(drawme) withObject:nil waitUntilDone:YES]; 
} 

- (void)drawme { 
    [self.drawLayer setNeedsDisplay]; 
} 

//delegate function that draws to a CALayer 
- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx { 
    NSLog(@"hello layer!"); 
    CGContextSetRGBFillColor (ctx, 1, 0, 0, 1); 
    CGContextFillRect (ctx, CGRectMake (0, 0, 200, 100)); 
} 
0

아니면 그냥 이미지를 삽입 할 수 있습니다 - 당신은 비디오 분명 캡처 AVCaptureVideoPreviewLayer을 사용하고, 또 다른의 CALayer을 (만들기)과 (... 위 : ...) layer.insertSublayer을 사용할 수 있습니다 삽입하기 위해 " 비디오 레이어 위에, 및 사용자 정의하여 사용자 정의 "계층은 내가하자 그냥 또 다른의 CALayer 여기

layer.contents = spinner.cgImage 

더 자세한 조금 말할 의미 instructions for Swift

관련 문제