2010-12-14 6 views
6

Je me suis cogné la tête pendant quelques jours maintenant.Dessiner un rectangle au-dessus d'un AVCaptureVideoPreviewLayer, possible?

Je souhaite dessiner un rectangle au-dessus d'un CALayer (AVCaptureVideoPreviewLayer), qui se trouve être le flux vidéo de l'appareil photo sur un iPhone4.

Voici une partie de ma configuration;

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

Est-ce encore possible? À partir de mon code actuel, j'obtiens l'impression «Bonjour couche», mais le flux de la caméra n'a pas de rectangle rempli.

Toute aide serait géniale. :)

Répondre

1

Je pense que vous devriez ajouter une autre couche à AVCaptureVideoPreviewLayer et je modifie l'exemple de code pour vous. Tu peux l'essayer.

//(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

Ou vous pouvez simplement insérer une image - vous pouvez utiliser AVCaptureVideoPreviewLayer pour la capture vidéo évidemment, et créer un autre CALayer() et utiliser layer.insertSublayer (..., au-dessus ...) pour insérer votre " couche personnalisée » au-dessus de la couche vidéo, et par la coutume je veux dire tout de suite une autre CALayer avec laisser dire

layer.contents = spinner.cgImage 

est ici un peu plus détaillée instructions for Swift

Questions connexes